MQTT in Android

Ayush Gemini
3 min readMar 15, 2019
How MQTT Protocol works.

This story will make you aware of what is MQTT and how to implement this in Android Application with your local machine.

What is MQTT ?

Nowadays, MQTT protocol is getting popular just like HTTP Protocol in communications between client and server. MQTT works as Pub-Sub. MQTT uses very small bytes of data to express the details of the content being transported.

In general, MQTT is more suitable for IOT projects because of its reliability and small header footprint.

Implementation

In this project demo, I am using Eclipse open source library namely PAHO for MQTT .

At first, we need to set up the broker on our local machine (macbook in my case). I am using Mosquitto for macbook to setting up the broker in my local machine. If you have already installed brew in your macbook then type the following command in your terminal.

brew install mosquitto

The script finishes by providing startup instructions:

That’s all, Now time to test the installation by running the following command on different terminal window tab or window.

Run the following command on terminal 1, which will act as a subscriber :

mosquitto_sub -t topic/ayushgemini

Run the following command on terminal 2 , which will act as a publisher :

mosquitto_pub -t topic/ayushgemini -m “Hello From Ayush Gemini”

After above commands you will see the message “Hello From Ayush Gemini” in subscriber window (terminal 1)

Second, we need to integrate PAHO Java library in the Android Studio project.

Once you are done with this testing , Create a new project (if you don’t have one) and then we can start the code for MQTT demo.

  1. Add the following code to build.gradle file of the android studio project
repositories {
jcenter()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}

2. Add the PAHO dependency to app build.gradle

dependencies {
compileOnly 'com.google.android.things:androidthings:0.1-devpreview'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
}

3. Now, Implement the MqttCallback in your Activity class and write the following code in your onCreate method of Activity class.

try {
MqttClient client = new MqttClient("tcp://192.168.1.5:1883", "AndroidThingSub", new MemoryPersistence());
client.setCallback(this);
client.connect();
String topic = "topic/ayushgemini";
client.subscribe(topic);
} catch (MqttException e) {
e.printStackTrace();
}

Here 192.168.1.5 is the IP of your local machine on which you have installed and run the mosquitto and 1883 is the port at which broker is listening and topic/ayushgemini is the subscribed topic on which publisher will send the message.

4. Implement the three method of MqttCallback class in your Activity class and add the following code to these methods to receive input message and based on the data received do your action what you want to do.

@Override
public void connectionLost(Throwable cause) {
Log.d(TAG, "connectionLost");
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String payload = new String(message.getPayload());
Log.d(TAG, payload);
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG, "deliveryComplete");
}

5. Now, time to test the project. Open your terminal and type the following command :

mosquitto_pub -h 127.0.0.1 -t topic/ayushgemini -m "Ayush"
mosquitto_pub -h 127.0.0.1 -t topic/ayushgemini -m "Gemini"

Here 127.0.0.1 is the IP Address of MQTT broker running on my local machine macbook and topic/ayushgemini is the subscribed topic by client in android side.

Once you run the above command you will see the message “Ayush” and “Gemini” in the logcat in android studio.

See you in the next story. Stay tuned!

You can connect with me on linkedIn

--

--

Ayush Gemini

I’m a core Android developer with complimenting skills as a web developer from India.