Mqtt Android SDK v1.1.1封装
- 使用 jitpack 仓库
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
- 引入 Library
dependencies {
implementation 'com.github.niulinguo:MqttModule:v1.2'
}
- 初始化
mMqttClientManager = new MqttClientManager(this, MqttConfig
.newBuilder()
.setAutomaticReconnect(true)
.setBufferEnabled(true)
.setBufferSize(10000)
.setMaxInflight(10000)
.setCleanSession(false)
.setClientId("demoClient")
.setDeleteOldestMessages(false)
.setPersistBuffer(true)
.setTraceEnable(true)
.setServerUri(BuildConfig.SERVER_URI)
.setUsername(BuildConfig.USERNAME)
.setPassword(BuildConfig.PASSWORD)
.build());
- mqtt 连接
mMqttClientManager.connect();
- 发布消息
mMqttClientManager.publish(MqttMessage.newBuilder()
.setQos(2)
.setRetained(true)
.setTopic("useiov/event")
.setPayload(payload)
.build());