SHARE
TWEET

Untitled

a guest Jan 21st, 2020 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MqttHelper {
  2.         public MqttAndroidClient mqttAndroidClient;
  3.  
  4.         final String serverUri = "ws://10.3.186.12:9001";
  5.  
  6.         final String clientId = "ExampleAndroidClient";
  7.         final String subscriptionTopic = "MQTT";
  8.  
  9.         final String username = "xxxxxxx";
  10.         final String password = "yyyyyyyyyy";
  11.  
  12.         public MqttHelper(Context context){
  13.             mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
  14.             mqttAndroidClient.setCallback(new MqttCallbackExtended() {
  15.                 @Override
  16.                 public void connectComplete(boolean b, String s) {
  17.                     Log.w("MQTT", s);
  18.                 }
  19.  
  20.                 @Override
  21.                 public void connectionLost(Throwable throwable) {
  22.  
  23.                 }
  24.  
  25.                 @Override
  26.                 public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
  27.                     Log.w("MQTT", mqttMessage.toString());
  28.                 }
  29.  
  30.                 @Override
  31.                 public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
  32.  
  33.                 }
  34.             });
  35.             connect();
  36.         }
  37.  
  38.         public void setCallback(MqttCallbackExtended callback) {
  39.             mqttAndroidClient.setCallback(callback);
  40.         }
  41.  
  42.         private void connect(){
  43.             MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
  44.             mqttConnectOptions.setAutomaticReconnect(true);
  45.             mqttConnectOptions.setCleanSession(false);
  46.             mqttConnectOptions.setUserName(username);
  47.             mqttConnectOptions.setPassword(password.toCharArray());
  48.  
  49.             try {
  50.  
  51.                 mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
  52.                     @Override
  53.                     public void onSuccess(IMqttToken asyncActionToken) {
  54.  
  55.                         DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
  56.                         disconnectedBufferOptions.setBufferEnabled(true);
  57.                         disconnectedBufferOptions.setBufferSize(100);
  58.                         disconnectedBufferOptions.setPersistBuffer(false);
  59.                         disconnectedBufferOptions.setDeleteOldestMessages(false);
  60.                         mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
  61.                         subscribeToTopic();
  62.                     }
  63.  
  64.                     @Override
  65.                     public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  66.                         Log.w("MQTT", "Failed to connect to: " + serverUri + exception.toString());
  67.                     }
  68.                 });
  69.  
  70.  
  71.             } catch (MqttException ex){
  72.                 ex.printStackTrace();
  73.             }
  74.         }
  75.  
  76.  
  77.         private void subscribeToTopic() {
  78.             try {
  79.                 mqttAndroidClient.subscribe(subscriptionTopic, 2, null, new IMqttActionListener() {
  80.                     @Override
  81.                     public void onSuccess(IMqttToken asyncActionToken) {
  82.                         Log.w("MQTT","Subscribed!");
  83.                     }
  84.  
  85.                     @Override
  86.                     public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  87.                         Log.w("MQTT", "Subscribed fail!");
  88.                     }
  89.                 });
  90.  
  91.             } catch (MqttException ex) {
  92.                 System.err.println("Exceptionst subscribing");
  93.                 ex.printStackTrace();
  94.             }
  95.         }
  96.     }
  97.  
  98.  
  99.     MqttHelper mqttHelper;
  100.     TextView dataReceived;
  101.  
  102.     @Override
  103.  
  104.     protected void onCreate(Bundle savedInstanceState) {
  105.         super.onCreate(savedInstanceState);
  106.         setContentView(R.layout.activity_main);
  107.  
  108.         dataReceived = (TextView) findViewById(R.id.dataReceived);
  109.  
  110.         startMqtt();
  111.     }
  112.  
  113.     private void startMqtt() {
  114.         mqttHelper = new MqttHelper(getApplicationContext());
  115.         mqttHelper.setCallback(new MqttCallbackExtended() {
  116.             @Override
  117.             public void connectComplete(boolean b, String s) {
  118.  
  119.             }
  120.  
  121.             @Override
  122.             public void connectionLost(Throwable throwable) {
  123.  
  124.             }
  125.  
  126.             @Override
  127.             public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
  128.                 Log.w("Debug", mqttMessage.toString());
  129.                 dataReceived.setText(mqttMessage.toString());
  130.             }
  131.  
  132.             @Override
  133.             public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
  134.  
  135.             }
  136.         });
  137.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top