Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.davidbrodbeck.mqtt;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.text.Editable;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Switch;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.eclipse.paho.android.service.MqttAndroidClient;
- import org.eclipse.paho.client.mqttv3.IMqttActionListener;
- import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.IMqttToken;
- import org.eclipse.paho.client.mqttv3.MqttCallback;
- import org.eclipse.paho.client.mqttv3.MqttClient;
- import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
- import org.eclipse.paho.client.mqttv3.MqttException;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- public class MainActivity extends AppCompatActivity implements MqttCallback {
- static String MQTTHOST = "tcp://m20.cloudmqtt.com:19860";
- static String USERNAME = "phone";
- static String PASSWORD = "phonetest";
- //String topicStr = "test/raum1";
- public int QOS = 1;
- public boolean KA;
- MqttAndroidClient client;
- Switch switch_KA;
- Button btn_connect;
- Button btn_publish;
- Button btn_subscribe;
- EditText edit_wert;
- EditText edit_topic;
- EditText edit_subscription;
- TextView text_Status;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- String clientId = MqttClient.generateClientId();
- client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
- edit_topic = (EditText) findViewById(R.id.editText_topic);
- edit_wert = (EditText) findViewById(R.id.editText_wert);
- edit_subscription = (EditText) findViewById(R.id.editText_subscription);
- text_Status = (TextView) findViewById(R.id.textView_Status);
- switch_KA = (Switch) findViewById(R.id.switch1);
- if(switch_KA.isChecked()){
- KA = true;
- }else{
- KA = false;
- }
- btn_subscribe = (Button) findViewById(R.id.button_subscribe);
- btn_publish = (Button) findViewById(R.id.button_publish);
- btn_connect = (Button) findViewById(R.id.button_connect);
- btn_connect.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- connect();
- }
- });
- btn_publish.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String topic = String.valueOf(edit_topic.getText());
- String wert = String.valueOf(edit_wert.getText());
- publish(topic, wert);
- }
- });
- btn_subscribe.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- subscribe();
- }
- });
- switch_KA.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(switch_KA.isChecked()){
- KA = true;
- }else{
- KA = false;
- }
- }
- });
- }
- public void connect(){
- MqttConnectOptions options = new MqttConnectOptions();
- options.setUserName(USERNAME);
- options.setPassword(PASSWORD.toCharArray());
- try {
- IMqttToken token = client.connect(options);
- token.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Toast.makeText(MainActivity.this, "connected", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- // Something went wrong e.g. connection timeout or firewall problems
- Toast.makeText(MainActivity.this, "connection failed", Toast.LENGTH_LONG).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void publish(String topicStr, String message){
- //String topic = "foo/bar";
- //String message = "Hallo vom Handy";
- if (client.isConnected()){
- byte[] encodedPayload = new byte[0];
- try {
- client.publish(topicStr, message.getBytes(), QOS, KA);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }else {
- showSimplePopUp();
- }
- }
- public void subscribe(){
- if(client.isConnected()) {
- client.setCallback(MainActivity.this);
- final Editable topic = edit_subscription.getText();
- int qos = 1;
- try {
- IMqttToken subToken = client.subscribe(String.valueOf(topic), qos);
- subToken.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- // The message was published
- Toast.makeText(MainActivity.this, "Successfully subscribed to: " + topic, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken,
- Throwable exception) {
- // The subscription could not be performed, maybe the user was not
- // authorized to subscribe on the specified topic e.g. using wildcards
- Toast.makeText(MainActivity.this, "Couldn't subscribe to: " + topic, Toast.LENGTH_SHORT).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }else{
- showSimplePopUp();
- }
- }
- private void showSimplePopUp() {
- AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
- helpBuilder.setTitle("ERROR");
- helpBuilder.setMessage("Es besteht keine Verbindung \n zum Server \n \n Zum Server verbinden??");
- helpBuilder.setPositiveButton("Ja",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- connect();
- }
- });
- helpBuilder.setNegativeButton("Abbruch", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- }
- });
- // Remember, create doesn't show the dialog
- AlertDialog helpDialog = helpBuilder.create();
- helpDialog.show();
- }
- @Override
- public void connectionLost(Throwable cause) {
- }
- @Override
- public void messageArrived(String topic, MqttMessage message) throws Exception {
- //Toast.makeText(MainActivity.this, "Topic: "+topic+"\nMessage: "+message, Toast.LENGTH_LONG).show();
- String msg = message.toString();
- text_Status.setText(topic + " = " + msg);
- //Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
- }
- @Override
- public void deliveryComplete(IMqttDeliveryToken token) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement