当前位置:   article > 正文

android studio 使用MQTT协议连接onenet 注意事项和相关函数_安卓端使用mqtt连接到onenet

安卓端使用mqtt连接到onenet
一、注意事项:
1.添加依赖:
  1. implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
  2. implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
2.在注册表允许打开网络
  1. <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" tools:ignore="ProtectedPermissions" />
  2. <uses-permission android:name="android.permission.WAKE_LOCK" />
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  5. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  6. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  7. <uses-permission android:name="android.permission.INTERNET" />
3.在<application     </application>之间添加用于支持 MQTT(Message Queuing Telemetry Transport)通信协议的 Eclipse Paho Android 服务。
<service android:name="org.eclipse.paho.android.service.MqttService"/>
4.打开项目中的 gradle.properties 文件,并确保以下属性设置为true------更新所有的支持库到AndroidX中。
  1. android.useAndroidX=true
  2. android.enableJetifier=true
二、程序实现:
1.建立onenet连接
  1. private void connectToMQTT() {
  2. String serverUri = "tcp://183.230.40.39:6002";
  3. String clientId = "设备ID";
  4. String username = "产品ID";
  5. String password = "鉴权信息";
  6. mqttClient = new MqttAndroidClient(this, serverUri, clientId);
  7. MqttConnectOptions options = new MqttConnectOptions();
  8. options.setUserName(username);
  9. options.setPassword(password.toCharArray());
  10. try {
  11. mqttClient.connect(options, null, new IMqttActionListener() {
  12. @Override
  13. public void onSuccess(IMqttToken asyncActionToken) {
  14. // 连接成功(订阅主题)
  15. subscribeToTopic();
  16. }
  17. @Override
  18. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  19. // 连接失败
  20. exception.printStackTrace();
  21. }
  22. });
  23. } catch (MqttException e) {
  24. e.printStackTrace();
  25. }
  26. }
2.订阅主题
  1. private void subscribeToTopic() {
  2. String topic = "data/#";
  3. int qos = 1;
  4. try {
  5. mqttClient.subscribe(topic, qos, null, new IMqttActionListener() {
  6. @Override
  7. public void onSuccess(IMqttToken asyncActionToken) {
  8. // 订阅成功
  9. }
  10. @Override
  11. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  12. // 订阅失败
  13. exception.printStackTrace();
  14. }
  15. });
  16. mqttClient.setCallback(new MqttCallback() {
  17. @Override
  18. public void connectionLost(Throwable cause) {
  19. // 处理连接丢失的情况
  20. }
  21. @Override
  22. public void messageArrived(String topic, MqttMessage message) throws Exception {
  23. // 处理收到的消息
  24. String data = new String(message.getPayload());
  25. if(data.indexOf("temp")!=-1)
  26. {
  27. }
  28. }
  29. @Override
  30. public void deliveryComplete(IMqttDeliveryToken token) {
  31. // 处理消息发送完成的情况
  32. }
  33. });
  34. } catch (MqttException e) {
  35. e.printStackTrace();
  36. }
  37. }
3.发布消息
  1. private void publishDataToMQTT() {
  2. String topic = "cmd/water";
  3. String payload = "flag:"+count;
  4. int qos = 1;
  5. try {
  6. MqttMessage message = new MqttMessage(payload.getBytes());
  7. message.setQos(qos);
  8. mqttClient.publish(topic, message);
  9. } catch (MqttException e) {
  10. e.printStackTrace();
  11. }
  12. }
4.断开onenet连接
  1. private void disconnectFromMQTT() {
  2. try {
  3. mqttClient.disconnect();
  4. } catch (MqttException e) {
  5. e.printStackTrace();
  6. }
  7. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/465716
推荐阅读
相关标签
  

闽ICP备14008679号