Hip Hop Evolution

Una historia del hip hop desde los inicios allá por los 70 hasta el gangsta rap de los 90. Una producción canadiense más que recomentable.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




MQTT Integration in Android Studio

Message Queuing Telemetry Transport(MQTT) is a light weight publish subscribe based communication protocol like HyperText Transfer Protocol(HTTP) specially designed for Machine to Machine communication in the field of IoT and IIoT. MQTT uses very small bytes of data to describe the content of the information being transported.

In this demo I will be Paho Android Service which is an interface to the Paho Java MQTT Client library for the Android Platform. Let’s get started.

About the installation

Android uses `Gradle` as a build system and dependency management, therefor this blog post describes how the Paho Android Service can be added to an application via Gradle. The most convenient way to start a new Android Application is to use Android Studio. To add the Paho Android Service as a dependency to you app add the following parts to your gradle file.

The first part adds the Paho release repository to the gradle config, so that Gradle is able to find the packaged Paho Android Service JAR. The second part adds the Paho Android Service as a dependency to the application. The exclusion of the Android Support library `exclude module: ‘support-v4’` is only necessary if the Android application is using the Android Support Library to backport newer Android features to older Android versions. To have the latest Snapshot release within your application the gradle config below can be used.

Establish the connection

As already mentioned, the Paho Android Service encapsulates the MQTT connection and offers an API for that. To be able to create a binding to the Paho Android Service, the service needs to be declared in the `AndroidManifest.xml`. Add the following within the `<application>` tag:

Permissions required in manifest tag

Connect

In the first line a helper Function of the Paho MQTT client is used to generate a random user id. The second line creates an instance of an Android MQTT client, that will bind to the Paho Android Service. By calling the `connect` method of the `MqttAndroidClient` the client will asynchronously try to connect to the MQTT broker and return a token. That token can be used to register callbacks, to get notified when either the MQTT-connection gets connected or an error occurs. Running the above example inside an Android `Activity.onCreate` method will print “onSuccess” or “onFailure” to the console.

Publish

The `MqttAndroidClient` allows messages to be published via its `publish(topic, MqttMessage)` method. By design the `MqttAndroidClient` dose not queue any messages if the client is not connected and will throw an error when trying to send messages when the client is offline `client.isConnected() == false`. After the first connect, the `IMqttActionListener.onSuccess` method is called when a MQTT connection is established successfully.

Subscribe

Subscriptions can be created via the ` MqttAndroidClient.subscribe` method, which takes the topic and the QOS as parameters and returns a ` IMqttToken`. The token can be used to keep track if the subscription can successfully established or failed. The example below subscribes to the topic “foo/bar” with QOS 1

Disconnect

To disconnect the client call the `disconnect` method of the ` MqttAndroidClient` object. The example below saves the token returned by the `disconnect` method and adds a` IMqttActionListener` to get notified when the client is successfully disconnected.

Add a comment

Related posts:

Shorthaul Case Study

Construction logistics isn’t something I would consider myself an expert in. By any means. But when I was tasked to come up with high fidelity mock ups for a mobile application called Shorthaul, I…

Can payday loans help rebuild poor credit?

Do payday loan companies like moneymart report to the credit agencies and doe’s that help improve a poor credit rating? ANSWER: For personal finance I might suggest one to try this internet site…

The Audacity to be your true self!

The choice of word might probably sound weird within context, especially considering the fact that it refers to “you” — yourself. The real question is, ‘does one really require “audacity” to be their…