Wednesday, August 1, 2012

Android: MQTT with ActiveMQ

I have been wanting to create a simple demo for a while that sends a message from an Android device to ActiveMQ. With ActiveMQ 5.6 the broker was enhanced with the MQTT protocol. The MQTT protocol is a very light weight publish/subscribe messaging protocol that is ideal for use in portable devices such as phones and tablets where a small footprint is needed and network bandwidth may be limited or unreliable. So I decided to have a look at the mqtt-client library that Hiram Chirino has been working on to build a little demo app that can be used to publish and subscribe to a JMS topic. This demo is just the basics and provides the starting point to building something such as a mobile chat application.

Building the Code:

The client library I used for my demo application is the FuseSource MQTT client. This is a very nice library that supports Blocking, Future, and Callback/Continuation Passing based APIs. The source for this library is available at the following github repo: mqtt-client. To run the Android MQTT demo you'll need to clone this repo and build the mqtt-client.

git clone https://github.com/fusesource/mqtt-client.git

For the purpose of this demo we are only interested in the mqtt-client so you can change into the mqtt-client directory and run:

mvn clean install

You should then see the build was completed successfully.

Now that you have the required library built, lets go a head and download the Android MQTT Client. The source for this demo is available in the following github repo: android-mqtt-demo. This repo can be cloned using the following command:

https://github.com/jsherman1/android-mqtt-demo.git

Once you have the android-mqtt-demo cloned you can build the source within eclipse using the Android SDK. To open the project in eclipse, use the new project wizard to create a new Android project and select "Android Project from Existing Code". Browse to the src files you just cloned and click finish.

Now you will need to add the mqtt-client library to a libs directory in the android-mqtt-demo project so the library will be deployed to the emulator or Android device. With the mqtt-client library added to libs directory the project should build successfully. You can now deploy this to an Android device or run it from the Android emulator. Note this may very depending on the ADT version you are currently running. I am running with version 20.0.1. In previous versions you could add the library to the build path and it would automatically get deployed. For more information on this see the thread on Dealing with dependencies in Android projects.

Running the Demo:

Okay, so now for the fun part; running the demo. The first thing we need to do is start an ActiveMQ 5.6 broker instance that has the MQTT protocol enabled. This can be done by modifying the broker's configuration file, conf/activemq.xml, as follows to add a mqtt transport connector:

Now you can start the broker and Android MQTT Demo. When the activity loads enter the URL Address for the MQTT connector, enter the User Name and Password if needed and click connect. Once connected enter a message and click send. The application is listening for messages on the same topic so you should see the message appear in the Received text box.

hi, I got the example running on my phone, but unable to connect to activemq broker,would like to know if there are any tools to debug.Or would there be anyway to know if the lib I've compiled using mvn is functioning correctly.

Thanks for sharing the demo application!I was wondering if you have tried using as-it-is the Callback interface of FuseSource MQTT client on Android in order to get the updates assynchronously? I got some problems when modifying your app to use callbacks and I have the impression that the issue is the different callback/thread handling on Android.

If you have experimented with it and have any hints on how to make it work, please share =D

Can you please help me understand how to create chat rooms (channels) using apache activemq? My use case is I need to create a chat application that can exchange messages with a single user or a group of users.