mercredi 14 août 2013

Mosquitto MQTT Push solution in Android


My Coding travel from Wassoulou to Bani monitié... 

Overview :

Mosquitto is an open source (BSD licensed) message broker that implements the MQ Telemetry Transport protocol version 3.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for "machine to machine" messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino. A good example of this is all of the work that Andy Stanford-Clark (one of the originators of MQTT) has done in home monitoring and automation with his twittering house and twittering ferry.
For more information  see  http://mosquitto.org/

In my trip, in Wassoulou, I started to developp Mosquitto Android Client to let other developper to easily developp their app using Mosquitto.

Mosquitto Client Android API:

The API available for Mosquitto Client Android is this class. All you need to push information or received information to or from broker.

Use of Mosquito Client API

As you know it’s better to use Android Service for background operations which takes a lot time. Use Mosquitto in service is really recommended but you can use it in your Activity.
In the following snipped of Code, there’s a sample use of Mosquitto API Client in the service. In the callback you


/**
 * Sample use of Mosquitto Client Service.
 *
 * @author jbromo
 *
 */
public class MosquittoDemoService extends Service implements
        MqttPushEventListener {

     private MqttAndroidClient mqttAndroidClient;
     private static final String HOST = "your host";

     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
          mqttAndroidClient = new MqttAndroidClient(this, HOST);
          mqttAndroidClient.addPushListener(this);
          mqttAndroidClient.bind(new MqttTopic("kalana"));
          mqttAndroidClient.start();
          return START_STICKY;
     }

     @Override
     public void onPushMessageReceived(MqttMessage mqttMessage) {

          // Do something with mqttMessage. You can broadcast to your application,
          // show as notification

     }

     @Override
     public void onConnectionStateChanged(MqttClientState connectionState) {

          // Do something with with connection state change.You can notify client
          // that connection was lost

     }

     @Override
     public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
     }

     @Override
     public void onDestroy() {
          super.onDestroy();
          // Not necessary to disconnect to the broker
          mqttAndroidClient.disconnect();
     }

}




Push a Message to a Broker

§  Subscription to a topic:
To subscribe to a topic, you have just to add topic at the client. Create your topic and call the method addTopic( MqttTopic) to your client.
§  Publish a Message           
To publish a message under a topic to broker or to specify client, there are many way to push message.

 

Use in your App:

1.    Create your Android Project no matter the API Version.
2.    Download the Mosquitto client Android jar
3.    Add Mosquitto Client Android jar in your build path.
4.    You need network and network state permission in your manifest.
5.    Enjoy with API….


 

Sample Code:

You can checkout AndroidMqttClientDemo at the following link and play with it.
http://mosquitto-mqtt-client-android.googlecode.com/svn/trunk/

Demo:

You can use the following link to send push from your browser to a mobile phone.

http://88.191.130.14:8080/MqttPushServerDemo/

Enter your message fill the topic field with your client id shown in the app

After pushing, you should see the notification with your message typed and all relate information.

Download page:

Actually this there is no place in the repository to publish the code...(Remember to Fermat demo). So now I publish the jar that can be easily integrate to Android Project.
In the following link and at the tab download page, you should find client jar and demo how to integrate it.

Download link:

Sample Code (you should find jar in the folder lib):

http://mosquitto-mqtt-client-android.googlecode.com/svn/trunk/


Documentation : Documentation Android