Wednesday, 24 August 2016

Android MQTT Client example Part-1


               Mqtt is lightweight messaging protocol,  GCM replacement for android push notification.

Mqtt

      MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks.


Mosquitto 

    Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model.



Add Mqtt jar file to your lib folder and right click on it and select Add as a library .Download


MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button fab = (Button) findViewById(R.id.subscribe);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(getApplicationContext(), MQTTService.class);
                startService(intent);
            }
        });
        Button fab1 = (Button) findViewById(R.id.publish);
        fab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                doPublish("test", "Android Dhina");


            }
        });

    }

    public void doPublish(String topic, String payload)

    {
      if (BuildConfig.DEBUG)
            if (MQTTService.mqttClient == null) {
                return;
            }
        IMqttToken token;
        try {
            byte[] encodedPayload;
            encodedPayload = payload.getBytes("UTF-8");
            MqttMessage message = new MqttMessage(encodedPayload);
            token = MQTTService.mqttClient.publish(topic, message);
            token.waitForCompletion(5000);
        } catch (MqttSecurityException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (MqttException e) {
            switch (e.getReasonCode()) {
                case MqttException.REASON_CODE_BROKER_UNAVAILABLE:
                case MqttException.REASON_CODE_CLIENT_TIMEOUT:
                case MqttException.REASON_CODE_CONNECTION_LOST:
                case MqttException.REASON_CODE_SERVER_CONNECT_ERROR:
                    if (BuildConfig.DEBUG)
                        e.printStackTrace();
                    break;
                case MqttException.REASON_CODE_FAILED_AUTHENTICATION:
                    Intent i = new Intent("ANDROIDDHINA");
                    i.putExtra("ALLARM", e);
                    if (BuildConfig.DEBUG)
                        break;
                default:
                    if (BuildConfig.DEBUG)
                        break;
            }
        }

    }
}


So for we have seen how to publish message using Mqtt.In next part we will discuss receiving message using MQTT