class MqttConnection
extends java.lang.Object
implements org.eclipse.paho.client.mqttv3.MqttCallback
MqttConnection holds a MqttAsyncClient {host,port,clientId} instance to perform MQTT operations to MQTT broker.
Most of the major API here is intended to implement the most general forms of
the methods in IMqttAsyncClient, with slight adjustments for the Android
environment
These adjustments usually consist of adding two parameters to each method :-
Operations are very much asynchronous, so success and failure are notified by packing the relevant data into Intent objects which are broadcast back to the Activity via the MqttService.callbackToActivity() method.
Constructor and Description |
---|
MqttConnection(MqttService service,
java.lang.String serverURI,
java.lang.String clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence,
java.lang.String clientHandle)
Constructor - create an MqttConnection to communicate with MQTT server
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
close()
Close connection from the server
|
void |
connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions options,
java.lang.String invocationContext,
java.lang.String activityToken)
Connect to the server specified when we were instantiated
|
void |
connectionLost(java.lang.Throwable why)
Callback for connectionLost
|
void |
deliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken messageToken)
Callback to indicate a message has been delivered (the exact meaning of
"has been delivered" is dependent on the QOS value)
|
(package private) void |
disconnect(long quiesceTimeout,
java.lang.String invocationContext,
java.lang.String activityToken)
Disconnect from the server
|
(package private) void |
disconnect(java.lang.String invocationContext,
java.lang.String activityToken)
Disconnect from the server
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[] |
getPendingDeliveryTokens()
Get tokens for all outstanding deliveries for a client
|
boolean |
isConnected() |
void |
messageArrived(java.lang.String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message)
Callback when a message is received
|
(package private) void |
offline()
Receive notification that we are offline
if cleanSession is true, we need to regard this as a disconnection |
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String topic,
byte[] payload,
int qos,
boolean retained,
java.lang.String invocationContext,
java.lang.String activityToken)
Publish a message on a topic
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message,
java.lang.String invocationContext,
java.lang.String activityToken)
Publish a message on a topic
|
(package private) void |
reconnect()
Reconnect
Only appropriate if cleanSession is false and we were connected Declare as synchronized to avoid multiple calls to this method to send connect multiple times |
(package private) void |
setConnectingState(boolean isConnecting) |
void |
subscribe(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 topic,
int qos,
java.lang.String invocationContext,
java.lang.String activityToken)
Subscribe to a topic
|
(package private) void |
unsubscribe(java.lang.String[] topic,
java.lang.String invocationContext,
java.lang.String activityToken)
Unsubscribe from one or more topics
|
(package private) void |
unsubscribe(java.lang.String topic,
java.lang.String invocationContext,
java.lang.String activityToken)
Unsubscribe from a topic
|
MqttConnection(MqttService service, java.lang.String serverURI, java.lang.String clientId, org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence, java.lang.String clientHandle)
service
- our "parent" service - we make callbacks to itserverURI
- the URI of the MQTT server to which we will connectclientId
- the name by which we will identify ourselves to the MQTT
serverpersistence
- the persistence class to use to store in-flight message. If null then the
default persistence mechanism is usedclientHandle
- the "handle" by which the activity will identify uspublic void connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions options, java.lang.String invocationContext, java.lang.String activityToken)
options
- timeout, etcinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activityvoid close()
void disconnect(long quiesceTimeout, java.lang.String invocationContext, java.lang.String activityToken)
quiesceTimeout
- in millisecondsinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary string to be passed back to the activityvoid disconnect(java.lang.String invocationContext, java.lang.String activityToken)
invocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary string to be passed back to the activitypublic boolean isConnected()
public org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String topic, byte[] payload, int qos, boolean retained, java.lang.String invocationContext, java.lang.String activityToken)
topic
- the topic on which to publish - represented as a string, not
an MqttTopic objectpayload
- 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 string to be passed back to the activitypublic org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String topic, org.eclipse.paho.client.mqttv3.MqttMessage message, java.lang.String invocationContext, java.lang.String activityToken)
topic
- the topic on which to publish - represented as a string, not
an MqttTopic objectmessage
- the message to publishinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary string to be passed back to the activitypublic void subscribe(java.lang.String topic, int qos, java.lang.String invocationContext, java.lang.String activityToken)
topic
- 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[] topic, int[] qos, java.lang.String invocationContext, java.lang.String activityToken)
topic
- 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 Activityvoid unsubscribe(java.lang.String topic, java.lang.String invocationContext, java.lang.String activityToken)
topic
- a possibly wildcarded topic nameinvocationContext
- arbitrary data to be passed back to the applicationactivityToken
- arbitrary identifier to be passed back to the Activityvoid unsubscribe(java.lang.String[] topic, java.lang.String invocationContext, java.lang.String activityToken)
topic
- 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()
public void connectionLost(java.lang.Throwable why)
connectionLost
in interface org.eclipse.paho.client.mqttv3.MqttCallback
why
- the exeception causing the break in communicationspublic void deliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken messageToken)
deliveryComplete
in interface org.eclipse.paho.client.mqttv3.MqttCallback
messageToken
- the messge token provided when the message was originally sentpublic void messageArrived(java.lang.String topic, org.eclipse.paho.client.mqttv3.MqttMessage message) throws java.lang.Exception
messageArrived
in interface org.eclipse.paho.client.mqttv3.MqttCallback
topic
- the topic on which the message was receivedmessage
- the message itselfjava.lang.Exception
void offline()
void reconnect()
void setConnectingState(boolean isConnecting)
isConnecting
-