Android MQTT Client

What is MQTT?

Message Queue Telemetry Transport(MQTT) is a machine-to-machine (M2M)/Internet of Things connectivity protocol. It was designed as an extremely lightweight publish-subscribe based messaging protocol for use on top of the TCP/IP protocol. It is designed for connections to remote locations where a small code footprint is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message.

Real world applications

MQTT has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios. It is also ideal for mobile applications because of its small size, low power usage, minimized data packets, and efficient distribution of information to one or many receivers.

MQTT Broker

The broker is primarily responsible for receiving all messages, filtering them, decide who is interested in it and then sending the message to all subscribed clients.It also holds the session of all persisted clients including subscriptions and missed messages.MQTT Mosquitto is a message broker that implements the MQTT 3.1/3.1.1.There is a publically accessible sandbox server for the Eclipse IoT projects available at


Multiple clients connect to a broker and subscribe to topics that they are interested in. Clients also connect to the broker and publish messages to topics.

Android MQTT client

The Eclipse Paho project provides open-source client implementations of MQTT.Paho Android Service is an MQTT client library written in Java for developing applications on Android.The MQTT connection is encapsulated within an Android Service that runs in the background of the Android application, keeping it alive when the Android application is switching between different Activities.The Paho Android Service is an interface to the Paho Java MQTT Client library for the Android Platform.


The most convenient way to start a new Android Application is to use Android Studio. Download  Paho Android Service and Android MQTT Client library.Go to your libs folder inside app folder and paste all your .jar

To add the Paho Android Service as a dependency to you app add the following parts to your gradle file.


The Paho Android Service needs the following permissions to work

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

MQTT Connection Option

MqttAndroidClient will connect with MQTT 3.1.1 by default. To intentionally connect with MQTT 3.1  MqttConnectOptions object can be supplied to the connect method

Clean session

On connection, a client sets the “clean session” flag.If the clean session is set to false, then the connection is treated as durable. This means that when the client disconnects, any subscriptions it has will remain and any subsequent QoS 1 or 2 messages will be stored until it connects again in the future. If the clean session is true, then all subscriptions will be removed from the client when it disconnects.

Automatic Reconnect

Sets whether the client will automatically attempt to reconnect to the server if the connection is lost.If set to true, in the event that the connection is lost, the client will attempt to reconnect to the server. It will initially wait 1 second before it attempts to reconnect, for every failed to reconnect attempt, the delay will double until it is at 2 minutes at which point the delay will stay at 2 minutes.


When a client connects to a broker, it may inform the broker that it has a will. This is a message that it wishes the broker to send when the client disconnects unexpectedly. The will message has a topic, QoS and retains status just the same as any other message.

Receive MQTT Message

Disconnect Options

Messages published to the topic if the connection is available, but if it disconnects and connects again offline messages send.Holds the set of options that govern the behavior of Offline (or Disconnected) buffering of messages.

Create MQTT Client

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


Messages in MQTT are published on topics. There is no need to configure a topic, publishing on it is enough. Topics are treated as a hierarchy, using a slash (/) as a separator much in the same way as a filesystem.

Clients can receive messages by creating subscriptions. A subscription may be to an explicit topic, in which case only messages to that topic will be received, or it may include wildcards. Two wildcards are available, +or.#

Quality of Service

MQTT defines 3 levels of Quality of Service (QoS). The QoS defines how hard the broker/client will try to ensure that a message is received. Messages may be sent at any QoS level, and clients may attempt to subscribe to topics at any QoS level.

  • 0: The broker/client will deliver the message once, with no confirmation.
  • 1: The broker/client will deliver the message at least once, with confirmation required.
  • 2: The broker/client will deliver the message exactly once by using a 4 step handshake.

Retained Messages

The broker will keep the message even after sending it to all current subscribers. If a new subscription is made that matches the topic of the retained message will be sent to the client. This is useful as a “last known good” mechanism. With a retained message, the client will receive an instant update.

Publish Message

The MqttAndroidClient allows messages to be published via its publish method.


Subscriptions can be created via the MqttAndroidClient.subscribe method, which takes the topic and the QOS as parameters and returns a IMqttToken.


Disconnect MQTT Client

Download this project from GitHub.


MQTTBox enables you to create MQTT clients to publish or subscript topics, create MQTT virtual device networks, load test MQTT devices or brokers.

Config MQTT Client

1.Create MQTT client

2.Config MQTT client

Host :

Protocol : mqtt/tcp


Related Post

What’s new in MQTT 5


13 Replies to “Android MQTT Client”

  1. Hi there, i tried the code, but im not sure why im getting this error

    java.lang.NullPointerException: Attempt to invoke virtual method ‘org.eclipse.paho.client.mqttv3.IMqttDeliveryToken, java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage, java.lang.String, java.lang.String)’ on a null object reference

  2. Hi There,

    We are working in a project that needs a MQTT broker running on Android Platform. Do you have any idea why we cant find a MQTT broker for Android?

  3. Hi, how do you deal with doze? Whenever my phone goes into doze, the mqtt client can’t reconnect when it exists doze again, it keeps logging /AlarmPingSender: Failure. Release lock(MqttService.client.xiaomi_Redmi Note 3_);

      1. I think that mAlarmManager causes the problem on Android Oreo! I’m searching another version of Mqtt Service.

Leave a Reply

Your email address will not be published. Required fields are marked *