当前位置:   article > 正文

Android小Demo——通过MQTT协议连接OneNet平台(旧版)_旧版onenet入口

旧版onenet入口

先看效果图:

OneNet:

第一步:创建产品

1、多协议接入

 2、添加产品

 3、填写产品信息

 4、我已经创建过了,就不重新创了,我的相关填写如下图

5、创建成功界面

第二步:创建设备

1、打开设备列表,点击添加设备

2、添加设备的具体信息(注意:这里的鉴权信息相当于密码,是创建设备时你自己随便设的)

Android:

第一步:配置(这里配置分为两个小点)

1、在build.gradle中的dependencies添加依赖(MQTT的实现一般使用这两个库):

  1. implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
  2. implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'

2、在AndroidManifest.xml中设置权限以及添加服务:

  1. <uses-permission android:name="android.permission.INTERNET"/>
  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"/>
<service android:name="org.eclipse.paho.android.service.MqttService"/>

第二步:页面布局(在activity_main.xml中添加按钮)

  1. <Button
  2. android:id="@+id/conBtn"
  3. android:layout_width="130dp"
  4. android:layout_height="60dp"
  5. android:text="Connect"
  6. android:textSize="18sp"/>
  7. <Button
  8. android:id="@+id/subBtn"
  9. android:layout_width="130dp"
  10. android:layout_height="60dp"
  11. android:text="订阅"
  12. android:textSize="18sp"/>
  13. <Button
  14. android:id="@+id/pubBtn"
  15. android:layout_width="130dp"
  16. android:layout_height="60dp"
  17. android:text="发布"
  18. android:textSize="18sp"/>
  19. <Button
  20. android:id="@+id/disConBtn"
  21. android:layout_width="130dp"
  22. android:layout_height="60dp"
  23. android:text="断开连接"
  24. android:textSize="18sp"/>

第三步:初始化

1、按钮点击监听事件(连接、订阅、发布以及断开连接)

  1. private void initView() {
  2. conBtn = findViewById(R.id.conBtn);
  3. subBtn = findViewById(R.id.subBtn);
  4. pubBtn = findViewById(R.id.pubBtn);
  5. disConBtn = findViewById(R.id.disConBtn);
  6. conBtn.setOnClickListener(new View.OnClickListener() {
  7. @Override
  8. public void onClick(View view) {
  9. if (!client.isConnected()) {
  10. try {
  11. // 连接
  12. client.connect(conOpt, null, mqttActionListener);
  13. } catch (MqttException e) {
  14. Toast.makeText(MainActivity.this, "mqttConnect fail:" + e.getStackTrace(), Toast.LENGTH_SHORT).show();
  15. }
  16. }
  17. }
  18. });
  19. subBtn.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. if (client.isConnected()) {
  23. mqttSubscribe("Topic名称");
  24. }else {
  25. Toast.makeText(MainActivity.this, "请连接服务器!", Toast.LENGTH_SHORT).show();
  26. }
  27. }
  28. });
  29. pubBtn.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View view) {
  32. if (client.isConnected()) {
  33. mqttPublish("Topic名称","数据");
  34. }else {
  35. Toast.makeText(MainActivity.this, "请连接服务器!", Toast.LENGTH_SHORT).show();
  36. }
  37. }
  38. });
  39. disConBtn.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View view) {
  42. try {
  43. if (client.isConnected()) {
  44. // 断开连接
  45. client.disconnect();
  46. Toast.makeText(MainActivity.this, "设备已断开!", Toast.LENGTH_SHORT).show();
  47. }else {
  48. Toast.makeText(MainActivity.this, "设备未连接!", Toast.LENGTH_SHORT).show();
  49. }
  50. } catch (MqttException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. });
  55. }

2、MQTT产品以及设备的配置

  1. private MqttAndroidClient client;
  2. private MqttConnectOptions conOpt;
  3. private MqttCallback mqttCallback;
  4. private IMqttActionListener mqttActionListener;
  5. private static final String SERVER_URL = "tcp://mqtt.heclouds.com:6002";
  6. private static final String CLIENT_ID = "设备ID";
  7. private static final String USER_NAME = "产品ID";
  8. private static final String PASSWORD = "鉴权信息";

3、mqtt客户端初始化

  1. iniData();
  2. initView();
  3. client = new MqttAndroidClient(this, SERVER_URL, CLIENT_ID);
  4. client.setCallback(mqttCallback);
  5. conOpt = new MqttConnectOptions();
  6. conOpt.setCleanSession(true);
  7. conOpt.setConnectionTimeout(10);
  8. conOpt.setKeepAliveInterval(30);
  9. conOpt.setUserName(USER_NAME.trim());
  10. conOpt.setPassword(PASSWORD.trim().toCharArray());

4、mqtt数据回读以及动作监听

  1. private void iniData() {
  2. mqttCallback = new MqttCallback() {
  3. @Override
  4. public void connectionLost(Throwable cause) {
  5. }
  6. @Override
  7. public void messageArrived(String topic, MqttMessage message) throws Exception {
  8. // 收到消息,打印消息
  9. String msg = new String(message.getPayload());
  10. Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
  11. Log.d(TAG, "收到消息:" + msg);
  12. }
  13. @Override
  14. public void deliveryComplete(IMqttDeliveryToken token) {
  15. }
  16. };
  17. mqttActionListener = new IMqttActionListener() {
  18. @Override
  19. public void onSuccess(IMqttToken asyncActionToken) {
  20. Log.d(TAG, "连接成功");
  21. }
  22. @Override
  23. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  24. Log.d(TAG, "连接失败");
  25. }
  26. };
  27. }

第四步:订阅

  1. public void mqttSubscribe(String topic) {
  2. try {
  3. // 订阅myTopic话题
  4. client.subscribe(topic, 1, null, new IMqttActionListener() {
  5. @Override
  6. public void onSuccess(IMqttToken asyncActionToken) {
  7. Toast.makeText(MainActivity.this, "订阅成功", Toast.LENGTH_SHORT).show();
  8. }
  9. @Override
  10. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  11. Toast.makeText(MainActivity.this, "订阅失败", Toast.LENGTH_SHORT).show();
  12. }
  13. });
  14. } catch (MqttException e) {
  15. Toast.makeText(MainActivity.this, "订阅失败:" + e.getStackTrace(), Toast.LENGTH_SHORT).show();
  16. }
  17. }

第五步:发布

  1. public void mqttPublish(String topic,String msg) {
  2. Integer qos = 0;
  3. Boolean retained = false;
  4. try {
  5. if (client != null) {
  6. client.publish(topic, msg.getBytes(), qos.intValue(), retained.booleanValue(), null, new IMqttActionListener() {
  7. @Override
  8. public void onSuccess(IMqttToken asyncActionToken) {
  9. Toast.makeText(MainActivity.this, "发布消息成功", Toast.LENGTH_SHORT).show();
  10. }
  11. @Override
  12. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  13. Toast.makeText(MainActivity.this, "发布消息失败", Toast.LENGTH_SHORT).show();
  14. }
  15. });
  16. }
  17. } catch (MqttException e) {
  18. e.printStackTrace();
  19. }
  20. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/465720
推荐阅读
相关标签
  

闽ICP备14008679号