小工具知识网

当前位置: 首页 > 行业知识 > android studio 使用MQTT协议连接onenet 注意事项和相关函数

行业知识

android studio 使用MQTT协议连接onenet 注意事项和相关函数

2026-11-07 20:38:43 来源:互联网转载
一、注意事项:
1.添加依赖:
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
2.在注册表允许打开网络
                            
3.在之间添加用于支持 MQTT(Message Queuing Telemetry Transport)通信协议的 Eclipse Paho Android 服务。
 
4.打开项目中的 gradle.properties 文件,并确保以下属性设置为true------更新所有的支持库到AndroidX中。
android.useAndroidX=trueandroid.enableJetifier=true
二、程序实现:
1.建立onenet连接
private void connectToMQTT() {        String serverUri = "tcp://183.230.40.39:6002";        String clientId = "设备ID";        String username = "产品ID";        String password = "鉴权信息";        mqttClient = new MqttAndroidClient(this, serverUri, clientId);        MqttConnectOptions options = new MqttConnectOptions();        options.setUserName(username);        options.setPassword(password.toCharArray());        try {            mqttClient.connect(options, null, new IMqttActionListener() {                @Override                public void onSuccess(IMqttToken asyncActionToken) {                    // 连接成功(订阅主题)                    subscribeToTopic();                }                @Override                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {                    // 连接失败                    exception.printStackTrace();                }            });        } catch (MqttException e) {            e.printStackTrace();        }    }
2.订阅主题
private void subscribeToTopic() {        String topic = "data/#";        int qos = 1;        try {            mqttClient.subscribe(topic, qos, null, new IMqttActionListener() {                @Override                public void onSuccess(IMqttToken asyncActionToken) {                    // 订阅成功                }                @Override                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {                    // 订阅失败                    exception.printStackTrace();                }            });            mqttClient.setCallback(new MqttCallback() {                @Override                public void connectionLost(Throwable cause) {                    // 处理连接丢失的情况                }                @Override                public void messageArrived(String topic, MqttMessage message) throws Exception {                    // 处理收到的消息                    String data = new String(message.getPayload());                    if(data.indexOf("temp")!=-1)                    {                    }                }                @Override                public void deliveryComplete(IMqttDeliveryToken token) {                    // 处理消息发送完成的情况                }            });        } catch (MqttException e) {            e.printStackTrace();        }    }
3.发布消息
private void publishDataToMQTT() {        String topic = "cmd/water";        String payload = "flag:"+count;        int qos = 1;        try {            MqttMessage message = new MqttMessage(payload.getBytes());            message.setQos(qos);            mqttClient.publish(topic, message);        } catch (MqttException e) {            e.printStackTrace();        }    }
4.断开onenet连接
private void disconnectFromMQTT() {        try {            mqttClient.disconnect();        } catch (MqttException e) {            e.printStackTrace();        }    }

下面就是连接成功后的效果图 

上一篇:揭秘B2B外贸源码,高效拓展国际市场的解决方案重磅来袭!

下一篇:怎么总英文?