public class MqttAndroidClient
extends BroadcastReceiver
implements org.eclipse.paho.client.mqttv3.IMqttAsyncClient
Implementation of the MQTT asynchronous client interface IMqttAsyncClient
, using the MQTT
android service to actually interface with MQTT server. It provides android applications a simple programming interface to all features of the MQTT version 3.1
specification including:
Modifier and Type | Class and Description |
---|---|
static class |
MqttAndroidClient.Ack
The Acknowledgment mode for messages received from
MqttCallback.messageArrived(String, MqttMessage) |
Modifier and Type | Field and Description |
---|---|
(package private) Context |
myContext |
Constructor and Description |
---|
MqttAndroidClient(Context context,
java.lang.String serverURI,
java.lang.String clientId)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
|
MqttAndroidClient(Context ctx,
java.lang.String serverURI,
java.lang.String clientId,
MqttAndroidClient.Ack ackType)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
|
MqttAndroidClient(Context ctx,
java.lang.String serverURI,
java.lang.String clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence)
Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
|
MqttAndroidClient(Context context,
java.lang.String serverURI,
java.lang.String clientId,
org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence,
MqttAndroidClient.Ack ackType)
constructor
|
Modifier and Type | Method and Description |
---|---|
boolean |
acknowledgeMessage(java.lang.String messageId)
Acknowledges a message received on the
MqttCallback.messageArrived(String, MqttMessage) |
void |
close()
Close the client
Releases all resource associated with the client.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
connect()
Connects to an MQTT server using the default options.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions options)
Connects to an MQTT server using the provided connect options.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions options,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Connects to an MQTT server using the specified options.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
connect(java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Connects to an MQTT server using the default options.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
disconnect()
Disconnects from the server.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
disconnect(long quiesceTimeout)
Disconnects from the server.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
disconnect(long quiesceTimeout,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Disconnects from the server.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
disconnect(java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Disconnects from the server.
|
void |
disconnectForcibly() |
void |
disconnectForcibly(long disconnectTimeout) |
void |
disconnectForcibly(long quiesceTimeout,
long disconnectTimeout) |
java.lang.String |
getClientId()
Returns the client ID used by this client.
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[] |
getPendingDeliveryTokens()
Returns the delivery tokens for any outstanding publish operations.
|
org.eclipse.paho.client.mqttv3.MqttProtocolVersion |
getProtocolVersion() |
java.lang.String |
getServerURI()
Returns the URI address of the server used by this client.
|
javax.net.ssl.SSLSocketFactory |
getSSLSocketFactory(java.io.InputStream keyStore,
java.lang.String password)
Get the SSLSocketFactory using SSL key store and password
|
boolean |
isConnected()
Determines if this client is currently connected to the server.
|
void |
onReceive(Context context,
Intent intent)
Process incoming Intent objects representing the results of operations
and asynchronous activities such as message received
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String topic,
byte[] payload,
int qos,
boolean retained)
Publishes a message to a topic on the server.
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String topic,
byte[] payload,
int qos,
boolean retained,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Publishes a message to a topic on the server.
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message)
Publishes a message to a topic on the server.
|
org.eclipse.paho.client.mqttv3.IMqttDeliveryToken |
publish(java.lang.String topic,
org.eclipse.paho.client.mqttv3.MqttMessage message,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Publishes a message to a topic on the server.
|
void |
registerResources(Context context)
Register receiver to receiver intent from MqttService.
|
void |
setCallback(org.eclipse.paho.client.mqttv3.MqttCallback callback)
Sets a callback listener to use for events that happen asynchronously.
|
void |
setProtocolVersion(org.eclipse.paho.client.mqttv3.MqttProtocolVersion version) |
void |
setTraceCallback(MqttTraceHandler traceCallback)
identify the callback to be invoked when making tracing calls back into
the Activity
|
void |
setTraceEnabled(boolean traceEnabled)
turn tracing on and off
|
org.eclipse.paho.client.mqttv3.IMqttToken |
subscribe(java.lang.String[] topic,
int[] qos)
Subscribe to multiple topics, each of which may include wildcards.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
subscribe(java.lang.String[] topic,
int[] qos,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Subscribes to multiple topics, each of which may include wildcards.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
subscribe(java.lang.String topic,
int qos)
Subscribe to a topic, which may include wildcards.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
subscribe(java.lang.String topic,
int qos,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Subscribe to a topic, which may include wildcards.
|
void |
unregisterResources()
Unregister receiver which receives intent from MqttService avoids IntentReceiver
leaks.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
unsubscribe(java.lang.String topic)
Requests the server unsubscribe the client from a topic.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
unsubscribe(java.lang.String[] topic)
Requests the server unsubscribe the client from one or more topics.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
unsubscribe(java.lang.String[] topic,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Requests the server unsubscribe the client from one or more topics.
|
org.eclipse.paho.client.mqttv3.IMqttToken |
unsubscribe(java.lang.String topic,
java.lang.Object userContext,
org.eclipse.paho.client.mqttv3.IMqttActionListener callback)
Requests the server unsubscribe the client from a topics.
|
public MqttAndroidClient(Context context, java.lang.String serverURI, java.lang.String clientId)
context
- object used to pass context to the callback.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 serverpublic MqttAndroidClient(Context ctx, java.lang.String serverURI, java.lang.String clientId, MqttAndroidClient.Ack ackType)
ctx
- Application's contextserverURI
- 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 serverackType
- how the application wishes to acknowledge a message has been processedpublic MqttAndroidClient(Context ctx, java.lang.String serverURI, java.lang.String clientId, org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence)
ctx
- Application's contextserverURI
- 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 serverpersistence
- The object to use to store persisted datapublic MqttAndroidClient(Context context, java.lang.String serverURI, java.lang.String clientId, org.eclipse.paho.client.mqttv3.MqttClientPersistence persistence, MqttAndroidClient.Ack ackType)
context
- used to pass context to the callback.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 serverpersistence
- the persistence class to use to store in-flight message. If null then the
default persistence mechanism is usedackType
- how the application wishes to acknowledge a message has been processed.public boolean isConnected()
isConnected
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
true
if connected, false
otherwise.public java.lang.String getClientId()
All clients connected to the same server or server farm must have a unique ID.
getClientId
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
public java.lang.String getServerURI()
The format of the returned String is the same as that used on the constructor.
getServerURI
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
public void close()
close
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
- if the client is not disconnected.public org.eclipse.paho.client.mqttv3.IMqttToken connect() throws org.eclipse.paho.client.mqttv3.MqttException
The default options are specified in MqttConnectOptions
class.
connect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
- for any connected problemsconnect(MqttConnectOptions, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions options) throws org.eclipse.paho.client.mqttv3.MqttException
The connection will be established using the options specified in the
MqttConnectOptions
parameter.
connect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
options
- a set of connection parameters that override the defaults.org.eclipse.paho.client.mqttv3.MqttException
- for any connected problemsconnect(MqttConnectOptions, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken connect(java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
The default options are specified in MqttConnectOptions
class.
connect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when the connect completes. Use
null if not required.org.eclipse.paho.client.mqttv3.MqttException
- for any connected problemsconnect(MqttConnectOptions, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions options, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
The server to connect to is specified on the constructor.
It is recommended to call setCallback(MqttCallback)
prior to
connecting in order that messages destined for the client can be accepted
as soon as the client is connected.
The method returns control before the connect completes. Completion can be tracked by:
IMqttToken.waitForCompletion()
orIMqttActionListener
connect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
options
- a set of connection parameters that override the defaults.userContext
- optional object for used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when the connect completes. Use
null if not required.org.eclipse.paho.client.mqttv3.MqttException
- for any connected problems, including communication errorspublic org.eclipse.paho.client.mqttv3.IMqttToken disconnect() throws org.eclipse.paho.client.mqttv3.MqttException
An attempt is made to quiesce the client allowing outstanding
work to complete before disconnecting. It will wait
for a maximum of 30 seconds for work to quiesce before disconnecting.
This method must not be called from inside MqttCallback
methods.
disconnect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
- for problems encountered while disconnectingdisconnect(long, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken disconnect(long quiesceTimeout) throws org.eclipse.paho.client.mqttv3.MqttException
An attempt is made to quiesce the client allowing outstanding
work to complete before disconnecting. It will wait
for a maximum of the specified quiesce time for work to complete before disconnecting.
This method must not be called from inside MqttCallback
methods.
disconnect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
quiesceTimeout
- the amount of time in milliseconds to allow for
existing work to finish before disconnecting. A value of zero or less
means the client will not quiesce.org.eclipse.paho.client.mqttv3.MqttException
- for problems encountered while disconnectingdisconnect(long, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken disconnect(java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
An attempt is made to quiesce the client allowing outstanding
work to complete before disconnecting. It will wait
for a maximum of 30 seconds for work to quiesce before disconnecting.
This method must not be called from inside MqttCallback
methods.
disconnect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when the disconnect completes. Use
null if not required.org.eclipse.paho.client.mqttv3.MqttException
- for problems encountered while disconnectingdisconnect(long, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken disconnect(long quiesceTimeout, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
The client will wait for MqttCallback
methods to
complete. It will then wait for up to the quiesce timeout to allow for
work which has already been initiated to complete. For instance when a QoS 2
message has started flowing to the server but the QoS 2 flow has not completed.It
prevents new messages being accepted and does not send any messages that have
been accepted but not yet started delivery across the network to the server. When
work has completed or after the quiesce timeout, the client will disconnect from
the server. If the cleanSession flag was set to false and is set to false the
next time a connection is made QoS 1 and 2 messages that
were not previously delivered will be delivered.
This method must not be called from inside MqttCallback
methods.
The method returns control before the disconnect completes. Completion can be tracked by:
IMqttToken.waitForCompletion()
orIMqttActionListener
disconnect
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
quiesceTimeout
- the amount of time in milliseconds to allow for
existing work to finish before disconnecting. A value of zero or less
means the client will not quiesce.userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when the disconnect completes. Use
null if not required.org.eclipse.paho.client.mqttv3.MqttException
- for problems encountered while disconnectingpublic org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String topic, byte[] payload, int qos, boolean retained) throws org.eclipse.paho.client.mqttv3.MqttException, org.eclipse.paho.client.mqttv3.MqttPersistenceException
A convenience method, which will
create a new MqttMessage
object with a byte array payload and the
specified QoS, and then publish it.
publish
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- to deliver the message to, for example "finance/stock/ibm".payload
- the byte array to use as the payloadqos
- the Quality of Service to deliver the message at. Valid values are 0, 1 or 2.retained
- whether or not this message should be retained by the server.org.eclipse.paho.client.mqttv3.MqttPersistenceException
- when a problem occurs storing the messagejava.lang.IllegalArgumentException
- if value of QoS is not 0, 1 or 2.org.eclipse.paho.client.mqttv3.MqttException
- for other errors encountered while publishing the message.
For instance if too many messages are being processed.publish(String, MqttMessage, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String topic, org.eclipse.paho.client.mqttv3.MqttMessage message) throws org.eclipse.paho.client.mqttv3.MqttException, org.eclipse.paho.client.mqttv3.MqttPersistenceException
MqttMessage
message and delivers it to the server at the
requested quality of service.publish
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- to deliver the message to, for example "finance/stock/ibm".message
- to deliver to the serverorg.eclipse.paho.client.mqttv3.MqttPersistenceException
- when a problem occurs storing the messagejava.lang.IllegalArgumentException
- if value of QoS is not 0, 1 or 2.org.eclipse.paho.client.mqttv3.MqttException
- for other errors encountered while publishing the message.
For instance client not connected.publish(String, MqttMessage, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String topic, byte[] payload, int qos, boolean retained, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException, org.eclipse.paho.client.mqttv3.MqttPersistenceException
A convenience method, which will
create a new MqttMessage
object with a byte array payload and the
specified QoS, and then publish it.
publish
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- to deliver the message to, for example "finance/stock/ibm".payload
- the byte array to use as the payloadqos
- the Quality of Service to deliver the message at. Valid values are 0, 1 or 2.retained
- whether or not this message should be retained by the server.userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when message delivery
hsa completed to the requested quality of serviceorg.eclipse.paho.client.mqttv3.MqttPersistenceException
- when a problem occurs storing the messagejava.lang.IllegalArgumentException
- if value of QoS is not 0, 1 or 2.org.eclipse.paho.client.mqttv3.MqttException
- for other errors encountered while publishing the message.
For instance client not connected.publish(String, MqttMessage, Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttDeliveryToken publish(java.lang.String topic, org.eclipse.paho.client.mqttv3.MqttMessage message, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException, org.eclipse.paho.client.mqttv3.MqttPersistenceException
Once this method has returned cleanly, the message has been accepted for publication by the client and will be delivered on a background thread. In the event the connection fails or the client stops. Messages will be delivered to the requested quality of service once the connection is re-established to the server on condition that:
When building an application, the design of the topic tree should take into account the following principles of topic name syntax and semantics:
The following principles apply to the construction and content of a topic tree:
The method returns control before the publish completes. Completion can be tracked by:
IMqttAsyncClient.setCallback(MqttCallback)
where the
MqttCallback.deliveryComplete(IMqttDeliveryToken)
method will be called.MqttToken.waitForCompletion()
orIMqttActionListener
to this methodpublish
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- to deliver the message to, for example "finance/stock/ibm".message
- to deliver to the serveruserContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when message delivery
has completed to the requested quality of serviceorg.eclipse.paho.client.mqttv3.MqttPersistenceException
- when a problem occurs storing the messagejava.lang.IllegalArgumentException
- if value of QoS is not 0, 1 or 2.org.eclipse.paho.client.mqttv3.MqttException
- for other errors encountered while publishing the message.
For instance client not connected.MqttMessage
public org.eclipse.paho.client.mqttv3.IMqttToken subscribe(java.lang.String topic, int qos) throws org.eclipse.paho.client.mqttv3.MqttException, org.eclipse.paho.client.mqttv3.MqttSecurityException
subscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- the topic to subscribe to, which can include wildcards.qos
- the maximum quality of service at which to subscribe. Messages
published at a lower quality of service will be received at the published
QoS. Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.org.eclipse.paho.client.mqttv3.MqttSecurityException
- for security related problemsorg.eclipse.paho.client.mqttv3.MqttException
- for non security related problemssubscribe(String[], int[], Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken subscribe(java.lang.String[] topic, int[] qos) throws org.eclipse.paho.client.mqttv3.MqttException, org.eclipse.paho.client.mqttv3.MqttSecurityException
Provides an optimized way to subscribe to multiple topics compared to subscribing to each one individually.
subscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- one or more topics to subscribe to, which can include wildcardsqos
- the maximum quality of service at which to subscribe. Messages
published at a lower quality of service will be received at the published
QoS. Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.org.eclipse.paho.client.mqttv3.MqttSecurityException
- for security related problemsorg.eclipse.paho.client.mqttv3.MqttException
- for non security related problemssubscribe(String[], int[], Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken subscribe(java.lang.String topic, int qos, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
subscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- the topic to subscribe to, which can include wildcards.qos
- the maximum quality of service at which to subscribe. Messages
published at a lower quality of service will be received at the published
QoS. Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when subscribe
has completedorg.eclipse.paho.client.mqttv3.MqttException
- if there was an error registering the subscription.subscribe(String[], int[], Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken subscribe(java.lang.String[] topic, int[] qos, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
Provides an optimized way to subscribe to multiple topics compared to subscribing to each one individually.
The setCallback(MqttCallback)
method
should be called before this method, otherwise any received messages
will be discarded.
If (@link MqttConnectOptions#setCleanSession(boolean)} was set to true when when connecting to the server then the subscription remains in place until either:
If (@link MqttConnectOptions#setCleanSession(boolean)} was set to false when connecting to the server then the subscription remains in place until either:
The "topic filter" string used when subscribing may contain special characters, which allow you to subscribe to multiple topics at once.
The topic level separator is used to introduce structure into the topic, and can therefore be specified within the topic for that purpose. The multi-level wildcard and single-level wildcard can be used for subscriptions, but they cannot be used within a topic by the publisher of a message.
The number sign (#) is a wildcard character that matches any number of levels within a topic. For example, if you subscribe to finance/stock/ibm/#, you receive messages on these topics:
finance/stock/ibm
finance/stock/ibm/closingprice
finance/stock/ibm/currentprice
The multi-level wildcard can represent zero or more levels. Therefore, finance/# can also match the singular finance, where # represents zero levels. The topic level separator is meaningless in this context, because there are no levels to separate.
The multi-level wildcard can be specified only on its own or next to the topic level separator character. Therefore, # and finance/# are both valid, but finance# is not valid. The multi-level wildcard must be the last character used within the topic tree. For example, finance/# is valid but finance/#/closingprice is not valid.
The plus sign (+) is a wildcard character that matches only one topic level. For example, finance/stock/+ matches finance/stock/ibm and finance/stock/xyz, but not finance/stock/ibm/closingprice. Also, because the single-level wildcard matches only a single level, finance/+ does not match finance.
Use the single-level wildcard at any level in the topic tree, and in conjunction with the multilevel wildcard. Specify the single-level wildcard next to the topic level separator, except when it is specified on its own. Therefore, + and finance/+ are both valid, but finance+ is not valid. The single-level wildcard can be used at the end of the topic tree or within the topic tree. For example, finance/+ and finance/+/ibm are both valid.
The method returns control before the subscribe completes. Completion can be tracked by:
MqttToken.waitForCompletion()
orIMqttActionListener
to this methodsubscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- one or more topics to subscribe to, which can include wildcardsqos
- the maximum quality of service to subscribe each topic at.Messages
published at a lower quality of service will be received at the published
QoS. Messages published at a higher quality of service will be received using
the QoS specified on the subscribe.userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when subscribe
has completedorg.eclipse.paho.client.mqttv3.MqttException
- if there was an error registering the subscription.java.lang.IllegalArgumentException
- if the two supplied arrays are not the same size.public org.eclipse.paho.client.mqttv3.IMqttToken unsubscribe(java.lang.String topic) throws org.eclipse.paho.client.mqttv3.MqttException
unsubscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- the topic to unsubscribe from. It must match a topic
specified on an earlier subscribe.org.eclipse.paho.client.mqttv3.MqttException
- if there was an error unregistering the subscription.unsubscribe(String[], Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken unsubscribe(java.lang.String[] topic) throws org.eclipse.paho.client.mqttv3.MqttException
unsubscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- one or more topics to unsubscribe from. Each topic
must match one specified on an earlier subscribe.org.eclipse.paho.client.mqttv3.MqttException
- if there was an error unregistering the subscription.unsubscribe(String[], Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken unsubscribe(java.lang.String topic, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
unsubscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- the topic to unsubscribe from. It must match a topic
specified on an earlier subscribe.userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when unsubscribe
has completedorg.eclipse.paho.client.mqttv3.MqttException
- if there was an error unregistering the subscription.unsubscribe(String[], Object, IMqttActionListener)
public org.eclipse.paho.client.mqttv3.IMqttToken unsubscribe(java.lang.String[] topic, java.lang.Object userContext, org.eclipse.paho.client.mqttv3.IMqttActionListener callback) throws org.eclipse.paho.client.mqttv3.MqttException
Unsubcribing is the opposite of subscribing. When the server receives the unsubscribe request it looks to see if it can find a matching subscription for the client and then removes it. After this point the server will send no more messages to the client for this subscription.
The topic(s) specified on the unsubscribe must match the topic(s) specified in the original subscribe request for the unsubscribe to succeed
The method returns control before the unsubscribe completes. Completion can be tracked by:
MqttToken.waitForCompletion()
orIMqttActionListener
to this methodunsubscribe
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
topic
- one or more topics to unsubscribe from. Each topic
must match one specified on an earlier subscribe.userContext
- optional object used to pass context to the callback. Use
null if not required.callback
- optional listener that will be notified when unsubscribe
has completedorg.eclipse.paho.client.mqttv3.MqttException
- if there was an error unregistering the subscription.public org.eclipse.paho.client.mqttv3.IMqttDeliveryToken[] getPendingDeliveryTokens()
If a client has been restarted and there are messages that were in the
process of being delivered when the client stopped this method
returns a token for each in-flight message enabling the delivery to be tracked
Alternately the MqttCallback.deliveryComplete(IMqttDeliveryToken)
callback can be used to track the delivery of outstanding messages.
If a client connects with cleanSession true then there will be no delivery tokens as the cleanSession option deletes all earlier state. For state to be remembered the client must connect with cleanSession set to false
getPendingDeliveryTokens
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
public void setCallback(org.eclipse.paho.client.mqttv3.MqttCallback callback)
There are a number of events that the listener will be notified about. These include:
Other events that track the progress of an individual operation such
as connect and subscribe can be tracked using the MqttToken
returned from
each non-blocking method or using setting a IMqttActionListener
on the
non-blocking method.
setCallback
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
callback
- which will be invoked for certain asynchronous eventsMqttCallback
public void setTraceCallback(MqttTraceHandler traceCallback)
traceCallback
- handlerpublic void setTraceEnabled(boolean traceEnabled)
traceEnabled
- set true
to enable trace, otherwise, set false
to disable tracepublic void onReceive(Context context, Intent intent)
Process incoming Intent objects representing the results of operations and asynchronous activities such as message received
Note: This is only a public method because the Android
APIs require such.
This method should not be explicitly invoked.
public boolean acknowledgeMessage(java.lang.String messageId)
MqttCallback.messageArrived(String, MqttMessage)
messageId
- the messageId received from the MqttMessage (To access this field you need to cast MqttMessage
to ParcelableMqttMessage
)public javax.net.ssl.SSLSocketFactory getSSLSocketFactory(java.io.InputStream keyStore, java.lang.String password) throws org.eclipse.paho.client.mqttv3.MqttSecurityException
A convenience method, which will help user to create a SSLSocketFactory object
keyStore
- the SSL key store which is generated by some SSL key tool, such as keytool in Java JDKpassword
- the password of the key store which is set when the key store is generatedorg.eclipse.paho.client.mqttv3.MqttSecurityException
- if there was any error when getting the SSLSocketFactorypublic void disconnectForcibly() throws org.eclipse.paho.client.mqttv3.MqttException
disconnectForcibly
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
public void disconnectForcibly(long disconnectTimeout) throws org.eclipse.paho.client.mqttv3.MqttException
disconnectForcibly
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
public void disconnectForcibly(long quiesceTimeout, long disconnectTimeout) throws org.eclipse.paho.client.mqttv3.MqttException
disconnectForcibly
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
org.eclipse.paho.client.mqttv3.MqttException
public org.eclipse.paho.client.mqttv3.MqttProtocolVersion getProtocolVersion()
getProtocolVersion
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
public void setProtocolVersion(org.eclipse.paho.client.mqttv3.MqttProtocolVersion version)
setProtocolVersion
in interface org.eclipse.paho.client.mqttv3.IMqttAsyncClient
public void unregisterResources()
public void registerResources(Context context)
context
- - Current activity context.