Gradle
implementation 'andy.github.mqtt:MqttManager:1.0.0'
Maven
<dependency>
<groupId>andy.github.mqtt</groupId>
<artifactId>MqttManager</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
MQTT的连接/订阅/发布,支持以下功能
- 建立连接
- 断开连接
- 订阅 (单个&多个)
- 取消订阅 (单个&多个)
- 发布 (单个&多个)
- 创建主题
mConnection = MqttConnection
.createConnection(applicationContext, IP, CLIENT_ID)
.addConnectionOptions(ConnectOptionsBuilder()
.setUserCredentials(false)
.setConnectionTimeout(30)
.setKeppAliveInterval(10))
mConnection!!.connect(object : OnActionListener<MqttTopic> {
override fun onSuccess(topic: MqttTopic?, message: String?) {
runOnUiThread { mBinding!!.stateTv.text = "已连接,已订阅${mDeviceTopicList.size}种主题" }
}
override fun onFailure(topic: MqttTopic?, throwable: Throwable?) {
runOnUiThread { mBinding!!.stateTv.text = "断开连接" }
}
})
mConnection!!.disconnect()
- 单个
mConnection!!.subscribeTopic(LightTopic(), object : OnActionListener<LightTopic> {
override fun onSuccess(topic: LightTopic?, message: String?) {
TODO("Not yet implemented")
}
override fun onFailure(topic: LightTopic?, throwable: Throwable?) {
TODO("Not yet implemented")
}
})
- 多个
mConnection!!.subscribeTopics(mDeviceTopicList, object : OnActionListener<DeviceTopic> {
override fun onSuccess(topic: DeviceTopic?, message: String?) {
TODO("Not yet implemented")
}
override fun onFailure(topic: DeviceTopic?, throwable: Throwable?) {
TODO("Not yet implemented")
}
})
- 单个
mConnection!!.unSubscribeTopic(LightTopic(), object : OnActionListener<LightTopic> {
override fun onSuccess(topic: LightTopic?, message: String?) {
TODO("Not yet implemented")
}
override fun onFailure(topic: LightTopic?, throwable: Throwable?) {
TODO("Not yet implemented")
}
})
- 多个
mConnection!!.unSubscribeTopics(mDeviceTopicList,object : OnActionListener<DeviceTopic>{
override fun onSuccess(topic: DeviceTopic?, message: String?) {
TODO("Not yet implemented")
}
override fun onFailure(topic: DeviceTopic?, throwable: Throwable?) {
TODO("Not yet implemented")
}
})
- 单个
mConnection!!.publishTopic(LightTopic(),object : OnActionListener<LightTopic>{
override fun onSuccess(topic: LightTopic?, message: String?) {
TODO("Not yet implemented")
}
override fun onFailure(topic: LightTopic?, throwable: Throwable?) {
TODO("Not yet implemented")
}
})
- 多个
mConnection!!.publishTopics(mDeviceTopicList,object : OnActionListener<DeviceTopic>{
override fun onSuccess(topic: DeviceTopic?, message: String?) {
TODO("Not yet implemented")
}
override fun onFailure(topic: DeviceTopic?, throwable: Throwable?) {
TODO("Not yet implemented")
}
})
//DeviceTopic
open class DeviceTopic(val topic: String, var message: String) : MqttTopic("/DEVICE/$topic") {
constructor(mqttTopic: String) : this(mqttTopic, "") {}
}
//LightTopic
class LightTopic : DeviceTopic, Subscription, Publishing {
constructor() : super("Light") {}
constructor(topic: String?, message: String?) : super(topic!!, message!!) {}
override val publishingQoS: Int
get() = MqttConstants.EXACTLY_ONCE
override val isRetained: Int
get() = MqttConstants.RETAINED
override val publishingMessage: String
get() = message
override val subscriptionQoS: Int
get() = MqttConstants.AT_LEAST_ONCE
}
外部调用 |
---|
fun connect(onActionListener: OnActionListener, retryTime: Int) |
fun connect(onActionListener: OnActionListener) |
外部调用 |
---|
fun subscribeTopics(topics: MutableList, onActionListener: OnActionListener) |
fun subscribeTopics(retryTime: Int, topics: MutableList, onActionListener: OnActionListener) |
fun subscribeTopic(mqttTopic: T, onActionListener: OnActionListener) |
fun subscribeTopic(retryTime: Int, mqttTopic: T, onActionListener: OnActionListener) |
外部调用 |
---|
fun publishTopic(topic: T?, onActionListener: OnActionListener?) |
fun publishTopic(retryTime: Int, topic: T?, onActionListener: OnActionListener?) |
fun publishTopics(mqttTopics:MutableList?, onActionListener: OnActionListener?) |
fun publishTopics(retryTime: Int, mqttTopics: MutableList?, onActionListener: OnActionListener?) |
外部调用 |
---|
fun publishTopic(topic: T?, onActionListener: OnActionListener?) |
fun publishTopic(retryTime: Int, topic: T?, onActionListener: OnActionListener?) |
fun publishTopics(mqttTopics:MutableList?, onActionListener: OnActionListener?) |
fun publishTopics(retryTime: Int, mqttTopics: MutableList?, onActionListener: OnActionListener?) |
![]() |
![]() |
This plugin is available under the Apache License, Version 2.0.
(c) All rights reserved JFrog