public class MqttClient extends Object
Constructor and Description |
---|
MqttClient(String serverURI,
String clientId)
Initialize the MQTT server URI, client id
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close and release the resources
|
void |
connect(MqttConnectOptions options)
Connect to the server
|
void |
connect(MqttConnectOptions options,
Object userContext)
Connect to the server with a context
|
void |
connectAcked(int connResult) |
void |
disconnect()
Disconnect the server
|
static String |
generateClientId()
Returns a randomly generated client identifier based on the the fixed prefix
(tijos) and the system time.
|
String |
getClientId() |
String |
getServerURI() |
boolean |
isConnected() |
void |
messageArrived(int msgNum)
the event to indicate that there are messages arrived from server
|
void |
messageCompleted(int completedNum,
int waitNum)
the event to indicate that there are messages to server are completed
|
void |
networkStateChange(int oldState,
int newState)
network state is changed
|
int |
publish(String topic,
byte[] payload,
int qos,
boolean retained)
publish topic to the server
|
void |
reconnect()
User triggered attempt to reconnect
|
void |
SetMqttClientListener(MqttClientListener listener) |
int |
subscribe(String topic,
int qos)
subscribe a topic from the server
|
int |
unsubscribe(String topic)
unsubscribe a topic from the server
|
public void close() throws MqttException
MqttException
- If a MQTT error occurspublic void connect(MqttConnectOptions options) throws MqttException
options
- MQTT optionsMqttException
- If a MQTT error occurspublic void connect(MqttConnectOptions options, Object userContext) throws MqttException
options
- MQTT optionsuserContext
- user context objectMqttException
- If a MQTT error occurspublic void connectAcked(int connResult) throws IOException
connResult
- the MQTT CONNACT result valueIOException
- If an I/O error occurspublic void disconnect() throws MqttException
MqttException
- If a MQTT error occurspublic static String generateClientId()
When cleanSession is set to false, an application must ensure it uses the same client identifier when it reconnects to the server to resume state and maintain assured message delivery.
MqttConnectOptions.setCleanSession(boolean)
public String getClientId()
public String getServerURI()
public boolean isConnected()
public void messageArrived(int msgNum) throws IOException
IOException
- If an I/O error occurspublic void messageCompleted(int completedNum, int waitNum) throws IOException
completedNum
- the message number completedwaitNum
- the message number waitingIOException
- If an I/O error occurspublic void networkStateChange(int oldState, int newState) throws IOException
oldState
- the last statenewState
- the current stateIOException
- If an I/O error occurspublic int publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException
topic
- the topic namepayload
- the payload dataqos
- the qosretained
- if retained in serverMqttException
- If a MQTT error occurspublic void reconnect() throws MqttException
MqttException
- If a MQTT error occurspublic void SetMqttClientListener(MqttClientListener listener)
public int subscribe(String topic, int qos) throws MqttException
topic
- the topic name to subscribeqos
- the qos to subscribeMqttException
- If a MQTT error occurspublic int unsubscribe(String topic) throws MqttException
topic
- the topic name to subscribeMqttException
- If a MQTT error occurs