public class MqttService extends Service implements MqttTraceHandler
The android service which interfaces with an MQTT client implementation
The main API of MqttService is intended to pretty much mirror the
IMqttAsyncClient with appropriate adjustments for the Android environment.
These adjustments usually consist of adding two parameters to each method :-
To support multiple client connections, the bulk of the MQTT work is delegated to MqttConnection objects. These are identified by "client handle" strings, which is how the Activity, and the higher-level APIs refer to them.
Activities using this service are expected to start it and bind to it using the BIND_AUTO_CREATE flag. The life cycle of this service is based on this approach.
Operations are highly asynchronous - in most cases results are returned to
the Activity by broadcasting one (or occasionally more) appropriate Intents,
which the Activity is expected to register a listener for.
The Intents have an Action of
MqttServiceConstants.CALLBACK_TO_ACTIVITY
which allows the Activity to
register a listener with an appropriate IntentFilter.
Further data is provided by "Extra Data" in the Intent, as follows :-
Name | Data Type | Value | Operations used for |
---|---|---|---|
MqttServiceConstants.CALLBACK_CLIENT_HANDLE |
String | The clientHandle identifying the client which initiated this operation | All operations |
MqttServiceConstants.CALLBACK_STATUS |
Serializable | An Status value indicating success or
otherwise of the operation | ;
All operations |
MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN |
String | the activityToken passed into the operation | All operations |
MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT |
String | the invocationContext passed into the operation | All operations |
MqttServiceConstants.CALLBACK_ACTION |
String | one of | All operations |
MqttServiceConstants.CALLBACK_ERROR_MESSAGE
| String | A suitable error message (taken from the relevant exception where possible) | All failing operations |
MqttServiceConstants.CALLBACK_ERROR_NUMBER
| int | A suitable error code (taken from the relevant exception where possible) | All failing operations |
MqttServiceConstants.CALLBACK_EXCEPTION_STACK |
String | The stacktrace of the failing call | The Connection Lost event |
MqttServiceConstants.CALLBACK_MESSAGE_ID |
String | The identifier for the message in the message store, used by the Activity to acknowledge the arrival of the message, so that the service may remove it from the store | The Message Arrived event |
MqttServiceConstants.CALLBACK_DESTINATION_NAME
| String | The topic on which the message was received | The Message Arrived event |
MqttServiceConstants.CALLBACK_MESSAGE_PARCEL |
Parcelable | The new message encapsulated in Android
Parcelable format as a ParcelableMqttMessage |
The Message Arrived event |
Modifier and Type | Field and Description |
---|---|
(package private) MessageStore |
messageStore |
(package private) static java.lang.String |
TAG |
Constructor and Description |
---|
MqttService() |
Modifier and Type | Method and Description |
---|---|
Status |
acknowledgeMessageArrival(java.lang.String clientHandle,
java.lang.String id)
Called by the Activity when a message has been passed back to the
application
|
(package private) void |
callbackToActivity(java.lang.String clientHandle,
Status status,
Bundle dataBundle)
pass data back to the Activity, by building a suitable Intent object and
broadcasting it
|
void |
close(java.lang.String clientHandle)
Close connection from a particular client
|
void |
connect(java.lang.String clientHandle,
org.eclipse.paho.client.mqttv3.MqttConnectOptions connectOptions,
java.lang.String invocationContext,
java.lang.String activityToken)
Connect to the MQTT server specified by a particular client
|
void |
disconnect(java.lang.String clientHandle,
long quiesceTimeout,
java.lang.String invocationContext,
java.lang.String activityToken)
Disconnect from the server
|
void |
disconnect(java.lang.String clientHandle,
java.lang.String invocationContext,
java.lang.String activityToken)
Disconnect from the server
|
java.lang.String |
getClient(java.lang.String serverURI,
java.lang.String clientId,
java.lang.String contextId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence)
Get an MqttConnection object to represent a connection to a server
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[] |
getPendingDeliveryTokens(java.lang.String clientHandle)
Get tokens for all outstanding deliveries for a client
|
boolean |
isConnected(java.lang.String clientHandle)
Get the status of a specific client
|
boolean |
isOnline() |
boolean |
isTraceEnabled()
Check whether trace is on or off.
|
void |
notifyClientsOffline()
Notify clients we're offline
|
IBinder |
onBind(Intent intent) |
void |
onCreate() |
void |
onDestroy() |
int |
onStartCommand(Intent intent,
int flags,
int startId) |
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String clientHandle,
java.lang.String topic,
byte[] payload,
int qos,
boolean retained,
java.lang.String invocationContext,
java.lang.String activityToken)
Publish a message to a topic
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String clientHandle,
java.lang.String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message,
java.lang.String invocationContext,
java.lang.String activityToken)
Publish a message to a topic
|
(package private) void |
reconnect()
Request all clients to reconnect if appropriate
|
void |
setTraceCallbackId(java.lang.String traceCallbackId)
Identify the callbackId to be passed when making tracing calls back into
the Activity
|
void |
setTraceEnabled(boolean traceEnabled)
Turn tracing on and off
|
void |
subscribe(java.lang.String clientHandle,
java.lang.String[] topic,
int[] qos,
java.lang.String invocationContext,
java.lang.String activityToken)
Subscribe to one or more topics
|
void |
subscribe(java.lang.String clientHandle,
java.lang.String topic,
int qos,
java.lang.String invocationContext,
java.lang.String activityToken)
Subscribe to a topic
|
void |
traceDebug(java.lang.String tag,
java.lang.String message)
Trace debugging information
|
void |
traceError(java.lang.String tag,
java.lang.String message)
Trace error information
|
void |
traceException(java.lang.String tag,
java.lang.String message,
java.lang.Exception e)
trace exceptions
|
void |
unsubscribe(java.lang.String clientHandle,
java.lang.String[] topic,
java.lang.String invocationContext,
java.lang.String activityToken)
Unsubscribe from one or more topics
|
void |
unsubscribe(java.lang.String clientHandle,
java.lang.String topic,
java.lang.String invocationContext,
java.lang.String activityToken)
Unsubscribe from a topic
|
static final java.lang.String TAG
MessageStore messageStore
void callbackToActivity(java.lang.String clientHandle, Status status, Bundle dataBundle)
clientHandle
- source of the datastatus
- OK or ErrordataBundle
- the data to be passedpublic java.lang.String getClient(java.lang.String serverURI, java.lang.String clientId, java.lang.String contextId, org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence)
serverURI
- specifies the protocol, host name and port to be used to connect to an MQTT serverclientId
- specifies the name by which this connection should be identified to the servercontextId
- specifies the app conext info to make a difference between appspublic void connect(java.lang.String clientHandle, org.eclipse.paho.client.mqttv3.MqttConnectOptions connectOptions, java.lang.String invocationContext, java.lang.String activityToken) throws org.eclipse.paho.client.mqttv3.MqttSecurityException, org.eclipse.paho.client.mqttv3.MqttException
clientHandle
- identifies the MqttConnection to useconnectOptions
- the MQTT connection options to be usedinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activityorg.eclipse.paho.client.mqttv3.MqttSecurityException
org.eclipse.paho.client.mqttv3.MqttException
void reconnect()
public void close(java.lang.String clientHandle)
clientHandle
- identifies the MqttConnection to usepublic void disconnect(java.lang.String clientHandle, java.lang.String invocationContext, java.lang.String activityToken)
clientHandle
- identifies the MqttConnection to useinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activitypublic void disconnect(java.lang.String clientHandle, long quiesceTimeout, java.lang.String invocationContext, java.lang.String activityToken)
clientHandle
- identifies the MqttConnection to usequiesceTimeout
- in millisecondsinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activitypublic boolean isConnected(java.lang.String clientHandle)
clientHandle
- identifies the MqttConnection to usepublic org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String clientHandle, java.lang.String topic, byte[] payload, int qos, boolean retained, java.lang.String invocationContext, java.lang.String activityToken) throws org.eclipse.paho.client.mqttv3.MqttPersistenceException, org.eclipse.paho.client.mqttv3.MqttException
clientHandle
- identifies the MqttConnection to usetopic
- the topic to which to publishpayload
- the content of the message to publishqos
- the quality of service requestedretained
- whether the MQTT server should retain this messageinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activityorg.eclipse.paho.client.mqttv3.MqttPersistenceException
org.eclipse.paho.client.mqttv3.MqttException
public org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String clientHandle, java.lang.String topic, org.eclipse.paho.client.mqttv3.MqttMessage message, java.lang.String invocationContext, java.lang.String activityToken) throws org.eclipse.paho.client.mqttv3.MqttPersistenceException, org.eclipse.paho.client.mqttv3.MqttException
clientHandle
- identifies the MqttConnection to usetopic
- the topic to which to publishmessage
- the message to publishinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activityorg.eclipse.paho.client.mqttv3.MqttPersistenceException
org.eclipse.paho.client.mqttv3.MqttException
public void subscribe(java.lang.String clientHandle, java.lang.String topic, int qos, java.lang.String invocationContext, java.lang.String activityToken)
clientHandle
- identifies the MqttConnection to usetopic
- a possibly wildcarded topic nameqos
- requested quality of service for the topicinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activitypublic void subscribe(java.lang.String clientHandle, java.lang.String[] topic, int[] qos, java.lang.String invocationContext, java.lang.String activityToken)
clientHandle
- identifies the MqttConnection to usetopic
- a list of possibly wildcarded topic namesqos
- requested quality of service for each topicinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activitypublic void unsubscribe(java.lang.String clientHandle, java.lang.String topic, java.lang.String invocationContext, java.lang.String activityToken)
clientHandle
- identifies the MqttConnectiontopic
- a possibly wildcarded topic nameinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activitypublic void unsubscribe(java.lang.String clientHandle, java.lang.String[] topic, java.lang.String invocationContext, java.lang.String activityToken)
clientHandle
- identifies the MqttConnectiontopic
- a list of possibly wildcarded topic namesinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activitypublic org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[] getPendingDeliveryTokens(java.lang.String clientHandle)
clientHandle
- identifies the MqttConnectionpublic Status acknowledgeMessageArrival(java.lang.String clientHandle, java.lang.String id)
clientHandle
- identifier for the client which received the messageid
- identifier for the MQTT messagepublic void onCreate()
android.app.Service#onCreate()
public void onDestroy()
android.app.Service#onDestroy()
public IBinder onBind(Intent intent)
android.app.Service#onBind(Intent)
public int onStartCommand(Intent intent, int flags, int startId)
android.app.Service#onStartCommand(Intent,int,int)
public void setTraceCallbackId(java.lang.String traceCallbackId)
traceCallbackId
- identifier to the callback into the Activitypublic void setTraceEnabled(boolean traceEnabled)
traceEnabled
- set true
to turn on tracing, false
to turn off tracingpublic boolean isTraceEnabled()
public void traceDebug(java.lang.String tag, java.lang.String message)
traceDebug
in interface MqttTraceHandler
tag
- identifier for the source of the tracemessage
- the text to be tracedpublic void traceError(java.lang.String tag, java.lang.String message)
traceError
in interface MqttTraceHandler
tag
- identifier for the source of the tracemessage
- the text to be tracedpublic void traceException(java.lang.String tag, java.lang.String message, java.lang.Exception e)
traceException
in interface MqttTraceHandler
tag
- identifier for the source of the tracemessage
- the text to be tracede
- the exceptionpublic boolean isOnline()
public void notifyClientsOffline()