Created
July 5, 2018 07:36
-
-
Save gkesharwani/f0e903f3565759c53ca6e551fceefa53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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