DZone
IoT Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > IoT Zone > MQTT: Android Integration Using Eclipse Paho

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.

Sachin Thapa user avatar by
Sachin Thapa
·
Oct. 30, 17 · IoT Zone · Tutorial
Like (2)
Save
Tweet
12.57K Views

Join the DZone community and get the full member experience.

Join For Free

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.

MQTT Eclipse Integration Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Integrate Event Streaming Into Your Applications
  • Real-Time Supply Chain With Apache Kafka in the Food and Retail Industry
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance
  • Why Is Software Integration Important for Business?

Comments

IoT Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo