Advertisement
Guest User

Untitled

a guest
Jul 17th, 2014
229
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MemoryPersistence mPer = new MemoryPersistence();
  2. String clientId = UUID.randomUUID().toString();
  3. String brokerUrl = "tcp://192.168.1.102:1883";
  4. MqttAndroidClient c = new MqttAndroidClient(getApplicationContext(), brokerUrl, clientId, mPer);
  5. IMqttToken tok;
  6.  
  7. try {
  8.     tok = c.connect();
  9. } catch (MqttException e) {
  10.     String emsg = getResources().getString(R.string.err_mqtt_connect_1) + e.getMessage() ;
  11.     Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_SHORT).show();
  12.     return;
  13. }
  14.  
  15. try {
  16.     tok.waitForCompletion(3000);
  17. } catch (MqttException e) {
  18.     String emsg = getResources().getString(R.string.err_mqtt_connect_2) + e.getMessage() ;
  19.     Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_LONG).show();
  20.     return;
  21. }
  22.  
  23. Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
  24.  
  25. String pubTopic = "random/moreRandom";
  26. MqttMessage msg = new MqttMessage();
  27. String payload = "samepl message"
  28. msg.setPayload(payload.getBytes());
  29. msg.setQos(2);
  30. msg.setRetained(false);
  31.  
  32. try {
  33.     c.publish(pubTopic, msg);
  34. } catch (MqttPersistenceException e) {
  35.     String emsg = getResources().getString(R.string.err_mqtt_publish_1) + e.getMessage() ;
  36.     Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_SHORT).show();
  37.     return;
  38. } catch (MqttException e) {
  39.     String emsg = getResources().getString(R.string.err_mqtt_publish_2) + e.getMessage() ;
  40.     Toast.makeText(getApplicationContext(), emsg, Toast.LENGTH_SHORT).show();
  41.     return;
  42. } catch (NullPointerException e) {
  43.     e.printStackTrace();
  44. }
  45.  
  46. Toast.makeText(getApplicationContext(), "Published", Toast.LENGTH_SHORT).show();
Advertisement
RAW Paste Data Copied
Advertisement