Advertisement
Guest User

mqtt

a guest
Apr 7th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. package cl.inacap.antofagasta.ejemploappmtqq;
  2.  
  3. import android.content.Context;
  4. import android.util.Log;
  5.  
  6. import org.eclipse.paho.android.service.MqttAndroidClient;
  7. import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
  8. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  9. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  10. import org.eclipse.paho.client.mqttv3.IMqttToken;
  11. import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
  12. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  13. import org.eclipse.paho.client.mqttv3.MqttException;
  14. import org.eclipse.paho.client.mqttv3.MqttMessage;
  15.  
  16. public class MqttHelper {
  17. public MqttAndroidClient mqttAndroidClient;
  18.  
  19. final String serverUri = "tcp://m14.cloudmqtt.com:12750";
  20.  
  21. final String clientId = "ExampleAndroidClient";
  22. final String subscriptionTopic1 = "sensor/temp";
  23. final String subscriptionTopic2 = "sensor/hum";
  24.  
  25. final String username = "icfgwkgf";
  26. final String password = "Id-MlBDw_1N4";
  27.  
  28. public MqttHelper(Context context){
  29. mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
  30. mqttAndroidClient.setCallback(new MqttCallbackExtended() {
  31. @Override
  32. public void connectComplete(boolean b, String s) {
  33. Log.w("mqtt", s);
  34. }
  35.  
  36. @Override
  37. public void connectionLost(Throwable throwable) {
  38.  
  39. }
  40.  
  41. @Override
  42. public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
  43. Log.w("Mqtt", mqttMessage.toString());
  44. }
  45.  
  46. @Override
  47. public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
  48.  
  49. }
  50. });
  51. connect();
  52. }
  53.  
  54. public void setCallback(MqttCallbackExtended callback) {
  55. mqttAndroidClient.setCallback(callback);
  56. }
  57.  
  58. private void connect(){
  59. MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
  60. mqttConnectOptions.setAutomaticReconnect(true);
  61. mqttConnectOptions.setCleanSession(false);
  62. mqttConnectOptions.setUserName(username);
  63. mqttConnectOptions.setPassword(password.toCharArray());
  64.  
  65. try {
  66.  
  67. mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
  68. @Override
  69. public void onSuccess(IMqttToken asyncActionToken) {
  70.  
  71. DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
  72. disconnectedBufferOptions.setBufferEnabled(true);
  73. disconnectedBufferOptions.setBufferSize(100);
  74. disconnectedBufferOptions.setPersistBuffer(false);
  75. disconnectedBufferOptions.setDeleteOldestMessages(false);
  76. mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
  77.  
  78. subscribeToTopic(subscriptionTopic1);
  79. subscribeToTopic(subscriptionTopic2);
  80. }
  81.  
  82. @Override
  83. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  84. Log.w("Mqtt", "Failed to connect to: " + serverUri + exception.toString());
  85. }
  86. });
  87.  
  88. } catch (MqttException ex){
  89. ex.printStackTrace();
  90. }
  91. }
  92.  
  93.  
  94. private void subscribeToTopic(String topic) {
  95. try {
  96. mqttAndroidClient.subscribe(topic, 0, null, new IMqttActionListener() {
  97. @Override
  98. public void onSuccess(IMqttToken asyncActionToken) {
  99. Log.w("Mqtt","Subscribed!");
  100. }
  101.  
  102. @Override
  103. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  104. Log.w("Mqtt", "Subscribed fail!");
  105. }
  106. });
  107.  
  108. } catch (MqttException ex) {
  109. System.err.println("Exceptionst subscribing");
  110. ex.printStackTrace();
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement