Advertisement
Guest User

Untitled

a guest
Jan 14th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. package com.example.davidbrodbeck.mqtt;
  2.  
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.text.Editable;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Switch;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import org.eclipse.paho.android.service.MqttAndroidClient;
  16. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  17. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  18. import org.eclipse.paho.client.mqttv3.IMqttToken;
  19. import org.eclipse.paho.client.mqttv3.MqttCallback;
  20. import org.eclipse.paho.client.mqttv3.MqttClient;
  21. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  22. import org.eclipse.paho.client.mqttv3.MqttException;
  23. import org.eclipse.paho.client.mqttv3.MqttMessage;
  24.  
  25.  
  26. public class MainActivity extends AppCompatActivity implements MqttCallback {
  27.  
  28.  
  29. static String MQTTHOST = "tcp://m20.cloudmqtt.com:19860";
  30. static String USERNAME = "phone";
  31. static String PASSWORD = "phonetest";
  32. //String topicStr = "test/raum1";
  33. public int QOS = 1;
  34. public boolean KA;
  35.  
  36. MqttAndroidClient client;
  37.  
  38. Switch switch_KA;
  39. Button btn_connect;
  40. Button btn_publish;
  41. Button btn_subscribe;
  42. EditText edit_wert;
  43. EditText edit_topic;
  44. EditText edit_subscription;
  45. TextView text_Status;
  46.  
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_main);
  51.  
  52. String clientId = MqttClient.generateClientId();
  53. client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
  54.  
  55.  
  56. edit_topic = (EditText) findViewById(R.id.editText_topic);
  57. edit_wert = (EditText) findViewById(R.id.editText_wert);
  58. edit_subscription = (EditText) findViewById(R.id.editText_subscription);
  59. text_Status = (TextView) findViewById(R.id.textView_Status);
  60. switch_KA = (Switch) findViewById(R.id.switch1);
  61.  
  62. if(switch_KA.isChecked()){
  63. KA = true;
  64. }else{
  65. KA = false;
  66. }
  67.  
  68. btn_subscribe = (Button) findViewById(R.id.button_subscribe);
  69. btn_publish = (Button) findViewById(R.id.button_publish);
  70. btn_connect = (Button) findViewById(R.id.button_connect);
  71. btn_connect.setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View view) {
  74. connect();
  75. }
  76. });
  77. btn_publish.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View view) {
  80. String topic = String.valueOf(edit_topic.getText());
  81. String wert = String.valueOf(edit_wert.getText());
  82. publish(topic, wert);
  83. }
  84. });
  85. btn_subscribe.setOnClickListener(new View.OnClickListener() {
  86. @Override
  87. public void onClick(View view) {
  88. subscribe();
  89. }
  90. });
  91.  
  92. switch_KA.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View view) {
  95. if(switch_KA.isChecked()){
  96. KA = true;
  97. }else{
  98. KA = false;
  99. }
  100. }
  101. });
  102.  
  103. }
  104.  
  105. public void connect(){
  106.  
  107. MqttConnectOptions options = new MqttConnectOptions();
  108. options.setUserName(USERNAME);
  109. options.setPassword(PASSWORD.toCharArray());
  110.  
  111.  
  112.  
  113. try {
  114. IMqttToken token = client.connect(options);
  115. token.setActionCallback(new IMqttActionListener() {
  116. @Override
  117. public void onSuccess(IMqttToken asyncActionToken) {
  118. Toast.makeText(MainActivity.this, "connected", Toast.LENGTH_LONG).show();
  119. }
  120.  
  121.  
  122. @Override
  123. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  124. // Something went wrong e.g. connection timeout or firewall problems
  125. Toast.makeText(MainActivity.this, "connection failed", Toast.LENGTH_LONG).show();
  126. }
  127. });
  128. } catch (MqttException e) {
  129. e.printStackTrace();
  130. }
  131. }
  132.  
  133. public void publish(String topicStr, String message){
  134. //String topic = "foo/bar";
  135. //String message = "Hallo vom Handy";
  136. if (client.isConnected()){
  137. byte[] encodedPayload = new byte[0];
  138. try {
  139. client.publish(topicStr, message.getBytes(), QOS, KA);
  140. } catch (MqttException e) {
  141. e.printStackTrace();
  142. }
  143. }else {
  144. showSimplePopUp();
  145. }
  146.  
  147.  
  148. }
  149.  
  150. public void subscribe(){
  151.  
  152. if(client.isConnected()) {
  153. client.setCallback(MainActivity.this);
  154. final Editable topic = edit_subscription.getText();
  155. int qos = 1;
  156. try {
  157. IMqttToken subToken = client.subscribe(String.valueOf(topic), qos);
  158. subToken.setActionCallback(new IMqttActionListener() {
  159. @Override
  160. public void onSuccess(IMqttToken asyncActionToken) {
  161. // The message was published
  162. Toast.makeText(MainActivity.this, "Successfully subscribed to: " + topic, Toast.LENGTH_SHORT).show();
  163. }
  164.  
  165. @Override
  166. public void onFailure(IMqttToken asyncActionToken,
  167. Throwable exception) {
  168. // The subscription could not be performed, maybe the user was not
  169. // authorized to subscribe on the specified topic e.g. using wildcards
  170. Toast.makeText(MainActivity.this, "Couldn't subscribe to: " + topic, Toast.LENGTH_SHORT).show();
  171. }
  172. });
  173. } catch (MqttException e) {
  174. e.printStackTrace();
  175. }
  176. }else{
  177. showSimplePopUp();
  178. }
  179. }
  180.  
  181. private void showSimplePopUp() {
  182.  
  183. AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
  184. helpBuilder.setTitle("ERROR");
  185. helpBuilder.setMessage("Es besteht keine Verbindung \n zum Server \n \n Zum Server verbinden??");
  186.  
  187. helpBuilder.setPositiveButton("Ja",
  188. new DialogInterface.OnClickListener() {
  189.  
  190. public void onClick(DialogInterface dialog, int which) {
  191. connect();
  192. }
  193. });
  194. helpBuilder.setNegativeButton("Abbruch", new DialogInterface.OnClickListener() {
  195. @Override
  196. public void onClick(DialogInterface dialogInterface, int i) {
  197.  
  198. }
  199. });
  200.  
  201. // Remember, create doesn't show the dialog
  202. AlertDialog helpDialog = helpBuilder.create();
  203. helpDialog.show();
  204. }
  205.  
  206.  
  207. @Override
  208. public void connectionLost(Throwable cause) {
  209.  
  210. }
  211.  
  212. @Override
  213. public void messageArrived(String topic, MqttMessage message) throws Exception {
  214. //Toast.makeText(MainActivity.this, "Topic: "+topic+"\nMessage: "+message, Toast.LENGTH_LONG).show();
  215. String msg = message.toString();
  216. text_Status.setText(topic + " = " + msg);
  217. //Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
  218. }
  219.  
  220. @Override
  221. public void deliveryComplete(IMqttDeliveryToken token) {
  222.  
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement