mqtt android demo
时间: 2023-08-13 09:07:17 浏览: 157
以下是一个基本的MQTT Android应用程序的示例代码,包括连接到MQTT服务器、发布消息和订阅主题:
```java
public class MainActivity extends AppCompatActivity {
private MqttAndroidClient mqttAndroidClient;
private static final String TAG = "MQTT Example";
private static final String MQTT_BROKER_URL = "tcp://mqtt.example.com:1883";
private static final String MQTT_USERNAME = "username";
private static final String MQTT_PASSWORD = "password";
private static final String PUBLISH_TOPIC = "mytopic";
private static final String SUBSCRIBE_TOPIC = "mytopic";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String clientId = MqttClient.generateClientId();
mqttAndroidClient = new MqttAndroidClient(this, MQTT_BROKER_URL, clientId);
mqttAndroidClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
Log.d(TAG, "Connection lost");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.d(TAG, "Message received: " + new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG, "Delivery complete");
}
});
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setUserName(MQTT_USERNAME);
mqttConnectOptions.setPassword(MQTT_PASSWORD.toCharArray());
try {
IMqttToken token = mqttAndroidClient.connect(mqttConnectOptions);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "Connected to broker");
subscribeToTopic();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG, "Connection failed: " + exception.getMessage());
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
private void subscribeToTopic() {
try {
mqttAndroidClient.subscribe(SUBSCRIBE_TOPIC, 0, null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "Subscribed to topic: " + SUBSCRIBE_TOPIC);
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG, "Failed to subscribe to topic: " + SUBSCRIBE_TOPIC);
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
public void publishMessage(View view) {
String message = "Hello, MQTT!";
try {
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setPayload(message.getBytes());
mqttAndroidClient.publish(PUBLISH_TOPIC, mqttMessage);
Log.d(TAG, "Message published: " + message);
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
mqttAndroidClient.disconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
}
```
此示例中,`MqttAndroidClient`是用于连接到MQTT服务器的客户端。在`onCreate`方法中,我们首先创建一个`MqttAndroidClient`实例并设置它的回调方法。然后,我们使用`MqttConnectOptions`来设置MQTT连接的用户名和密码,并使用`connect`方法连接到MQTT服务器。如果连接成功,我们将调用`subscribeToTopic`方法来订阅一个主题。
在`subscribeToTopic`方法中,我们使用`mqttAndroidClient`实例来订阅一个主题。如果订阅成功,我们将在日志中打印一条消息。
在`publishMessage`方法中,我们使用`mqttAndroidClient`实例来发布一条消息到指定的主题。这条消息将会在MQTT服务器上发送给所有订阅该主题的设备。
最后,在`onDestroy`方法中,我们使用`mqttAndroidClient`实例来断开MQTT连接。
请注意,这只是一个基本示例,并且可能需要根据您的应用程序的需求进行修改。
阅读全文