赞
踩
目录
这是记录能用代码的,想知道原理的话,可以自行搜索
二话不说,上代码,拷了就能用
- package com.doshare.boardroom.control.comm.mqtt
-
- import android.content.Context
- import android.util.Log
- import androidx.lifecycle.ViewModelProvider
- import com.doshare.boardroom.control.comm.retrofit.BoardRoomControlServiceImpl
- import com.doshare.boardroom.control.common.device.AppHelper
- import com.doshare.boardroom.control.common.device.DeviceIdUtils
- import com.doshare.boardroom.control.view.viewmodel.HomeViewModel
- import com.google.gson.Gson
- import com.google.gson.internal.LinkedTreeMap
- import org.eclipse.paho.android.service.MqttAndroidClient
- import org.eclipse.paho.client.mqttv3.*
-
-
- object MqttClient {
- private lateinit var mqttClient: MqttAndroidClient
- // TAG
- private val TAG = "AndroidMqttClient"
-
- /**
- * 创建连接
- */
- fun connect(context: Context) {
- val serverURI = "tcp://172.17.**.202:1883" //mqtt的服务器地址
- val username = "ds-mqtt" //用户名
- val password = "do***321" //密码
-
- mqttClient = MqttAndroidClient(context, serverURI, "IScreen-"+ DeviceIdUtils.getDeviceId(context))
- mqttClient.setCallback(object : MqttCallback {
- override fun messageArrived(topic: String?, message: MqttMessage?) {
- //这里是对收到mqtt消息进行处理的地方,
-
- Log.d(TAG, "Receive message: ${message.toString()} from topic: $topic")
- val messageMap = Gson().fromJson(message.toString(),Map::class.java)as LinkedTreeMap<String, Any>
-
- }
-
- override fun connectionLost(cause: Throwable?) {
- Log.d(TAG, "Connection lost ${cause.toString()}")
- }
-
- override fun deliveryComplete(token: IMqttDeliveryToken?) {
-
- }
- })
-
- // MQTT的连接设置
- val options = MqttConnectOptions()
- options.userName = username
- options.password = password.toCharArray()
-
- try {
- mqttClient.connect(options, null, object : IMqttActionListener {
- override fun onSuccess(asyncActionToken: IMqttToken?) {
- Log.d(TAG, "Connection success")
- subscribe("sunblind/user/+/notify") //订阅主题
-
- }
- override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
- Log.d(TAG, "Connection failure")
- }
- })
- } catch (e: MqttException) {
- e.printStackTrace()
- }
-
- }
-
- /**
- * 订阅主题
- */
- fun subscribe(topic: String, qos: Int = 1) {
- try {
- mqttClient.subscribe(topic, qos, null, object : IMqttActionListener {
- override fun onSuccess(asyncActionToken: IMqttToken?) {
- Log.d(TAG, "Subscribed to $topic")
- }
-
- override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
- Log.d(TAG, "Failed to subscribe $topic")
- }
- })
- } catch (e: MqttException) {
- e.printStackTrace()
- }
- }
-
- /**
- * 取消订阅
- */
- fun unsubscribe(topic: String) {
- try {
- mqttClient.unsubscribe(topic, null, object : IMqttActionListener {
- override fun onSuccess(asyncActionToken: IMqttToken?) {
- Log.d(TAG, "Unsubscribed to $topic")
- }
-
- override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
- Log.d(TAG, "Failed to unsubscribe $topic")
- }
- })
- } catch (e: MqttException) {
- e.printStackTrace()
- }
- }
-
- /**
- * 发布消息
- */
- fun publish(topic: String, msg: String, qos: Int = 1, retained: Boolean = false) {
- try {
- val message = MqttMessage()
- message.payload = msg.toByteArray()
- message.qos = qos
- message.isRetained = retained
- mqttClient.publish(topic, message, null, object : IMqttActionListener {
- override fun onSuccess(asyncActionToken: IMqttToken?) {
- Log.d(TAG, "$msg published to $topic")
- }
-
- override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
- Log.d(TAG, "Failed to publish $msg to $topic")
- }
- })
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }
-
- /**
- * 取消连接
- */
- fun disconnect() {
- try {
- mqttClient.disconnect(null, object : IMqttActionListener {
- override fun onSuccess(asyncActionToken: IMqttToken?) {
- Log.d(TAG, "Disconnected")
- }
-
- override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
- Log.d(TAG, "Failed to disconnect")
- }
- })
- } catch (e: MqttException) {
- e.printStackTrace()
- }
- }
- }
1.connect方法中serverURI //mqtt的服务器地址,username //用户名,password //密码这三个数据换成自己的
2. mqttClient.connect中订阅的主题换成自己的
3.上面import了好多的无用包,直接删掉
4.改包名
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。