Skip to content

Instantly share code, notes, and snippets.

@gkesharwani
Created July 5, 2018 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkesharwani/f0e903f3565759c53ca6e551fceefa53 to your computer and use it in GitHub Desktop.
Save gkesharwani/f0e903f3565759c53ca6e551fceefa53 to your computer and use it in GitHub Desktop.
/**
* We can subscribe topic from here
*/
public void subscribeToTopic() {
try {
/**
* @param topic_name
* @param QOS_level (0,1,2)
* @param userContext
* @param Listener
*/
mqttAndroidClient.subscribe(topic, QOS, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
addToHistory("Subscribed!");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
addToHistory("Failed to subscribe");
}
});
/**
* @param topic_name
* @param QOS_level (0,1,2)
* @param Listener
*/
mqttAndroidClient.subscribe(topic, QOS, new IMqttMessageListener() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
/*
message Arrived!
* */
System.out.println("Message: " + topic + " : " + new String(message.getPayload()));
}
});
} catch (MqttException ex) {
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment