Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MQTT: Android Integration Using Eclipse Paho

DZone's Guide to

MQTT: Android Integration Using Eclipse Paho

Developing an IoT app on Android that uses MQTT? Take a look at the open source Eclipse Paho project and see how you can integrate it into your work.

· IoT Zone
Free Resource

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

The Eclipse Paho project provides open-source client implementations of the MQTT and MQTT-SN messaging protocols aimed at new, existing, and emerging applications for the Internet of Things.

To get started, download the Paho library for Android from the following GitHub project.

Integration Steps

Step 1: Initialize a client, set required options, and connect:

MqttAndroidClient mqttClient = new MqttAndroidClient(BaseApplication.getAppContext(), broker, MQTT_CLIENT_ID);
//Set call back class
mqttClient.setCallback(new MqttCallbackHandler(BaseApplication.getAppContext()));
MqttConnectOptions connOpts = new MqttConnectOptions();
IMqttToken token = mqttClient.connect(connOpts);


Step 2: Subscribe to a topic:

token.setActionCallback(new IMqttActionListener() {
      @Override
      public void onSuccess(IMqttToken arg0) {
           mqttClient.subscribe("TOPIC_NAME" + userId, 2, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Log.d(LOG_TAG, "Successfully subscribed to topic.");
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.d(LOG_TAG, "Failed to subscribed to topic.");
                }
            });
      }

      @Override
      public void onFailure(IMqttToken arg0, Throwable arg1) {
           Log.d(LOG_TAG, errorMsg);
      }
    });


Step 3: Define the callback handler class:

public class MqttCallbackHandler implements MqttCallbackExtended {
        @Override
            public void connectComplete(boolean b, String s) {
                Log.w("mqtt", s);
            }

            @Override
            public void connectionLost(Throwable throwable) {

            }

            @Override
            public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                Log.w("Anjing", mqttMessage.toString());
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

            }
      }


Step 4: Add the entry in the manifest file:

<service android:name="org.eclipse.paho.android.service.MqttService" >
    </service>


The new library makes it so much easier to integrate and also includes an auto-reconnect feature, so if an application goes in and out of the network, it auto-reconnects to the server.

Build an open IoT platform with Red Hat—keep it flexible with open source software.

Topics:
iot ,android ,eclipse paho ,mqtt ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}