当前位置:   article > 正文

Android 发送MQTT消息

Android 发送MQTT消息

现在物联网很多都是使用的MQTT消息,在手机和设备之间通过MQTT协议通信,Android发送MQTT消息就会用得比较多。

步骤1:添加依赖库

你需要在你的build.gradle文件中添加MQTT客户端库。一个常用的库是Eclipse Paho,你可以这样添加依赖:

  1. dependencies {
  2. implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
  3. }

步骤2:设置网络权限

在AndroidManifest.xml中增加:

  1. <!--网络权限-->
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

步骤3:前台代码(layout.xml)

在视图文件中增加按钮。

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:gravity="center_horizontal"
  5. android:orientation="horizontal">
  6. <Button
  7. android:id="@+id/btnTest"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="10dp"
  11. android:text="测试"
  12. android:textAlignment="center" />
  13. </LinearLayout>

步骤4:后台代码

  1. import org.eclipse.paho.client.mqttv3.MqttClient;
  2. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  3. import org.eclipse.paho.client.mqttv3.MqttException;
  4. import org.eclipse.paho.client.mqttv3.MqttMessage;
  5. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  6. ... ...
  7. btnTest.setOnClickListener(view -> {
  8. new Thread(() -> {
  9. String context = "tcp://broker.emqx.io:1883";
  10. String clientId = "mqttx_20240804-android";
  11. String username = "username";
  12. String password = "password";
  13. String topic = "mqttx_20240804";
  14. try {
  15. MqttClient client = new MqttClient(context, clientId, new MemoryPersistence());
  16. MqttConnectOptions options = new MqttConnectOptions();
  17. options.setCleanSession(true);
  18. options.setUserName(username);
  19. options.setPassword(password.toCharArray());
  20. client.connect(options);
  21. String action = "hello";
  22. MqttMessage message = new MqttMessage(action.getBytes());
  23. message.setQos(0);
  24. client.publish(topic, message);
  25. Message handlerMsg = new Message();
  26. handlerMsg.what = 1;
  27. handlerMsg.obj = action;
  28. handler.sendMessage(handlerMsg);
  29. } catch (MqttException e) {
  30. Message message = new Message();
  31. message.what = 2;
  32. message.obj = e.getMessage();
  33. handler.sendMessage(message);
  34. }
  35. }).start();
  36. });
  1. public Handler handler = new Handler() {
  2. @SuppressLint("HandlerLeak")
  3. @Override
  4. public void handleMessage(Message msg) {
  5. String text = msg.obj.toString();
  6. switch (msg.what) {
  7. case 1:
  8. Toast.makeText(ConfigActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
  9. break;
  10. case 2:
  11. Toast.makeText(ConfigActivity.this, "抱歉,发送异常:" + text, Toast.LENGTH_SHORT).show();
  12. break;
  13. }
  14. }
  15. };

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

闽ICP备14008679号