今天要繼續繼續介紹MQTT,特別是如何使用MQTTX來連接到MQTT Broker。MQTTX是一個功能豐富的MQTT客戶端應用程序,它可以幫助您輕鬆地測試、監視和交互MQTT Broker。
demo/1
就可以接收和發送了。 //訂閱 Topic
private void subscribeToTopic(View view) {
String topic;
if(publish_editText.getText() != null){
topic = subscribe_editText.getText().toString();
mqttHandler.subscribe(topic);
}
}
//取消訂閱 Topic
private void unSubscribeToTopic(View view) {
String topic;
if(publish_editText.getText() != null){
topic = subscribe_editText.getText().toString();
mqttHandler.unSubscribe(topic);
}
}
//傳送 message
private void publishMessagePost(View view) {
String topic;
if(publish_editText.getText() != null){
topic = publish_editText.getText().toString();
String message = "Android Post";
mqttHandler.publish(topic, message);
}
}
今天就分享到這,我們明天會介紹如何用 Docker 架設 broker 那明天見摟~~~!!!