Android MQTT连接,重新编译Service-1.1.1兼容Android高版本服务
Paho Android Service-1.1.1
Paho Client Mqtt3-1.1.0
1.build.grade
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2./app/build.grade
dependencies {
implementation 'com.github.RelinRan:MQTT:2023.9.23.1'
}
配置权限
Service配置
初始化服务端
MqttOptions options = new MqttOptions();
options.setHost("tcp://xxx.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883");
options.setClientId("xxxx.301F9A89A749|securemode=2,signmethod=hmacsha256,timestamp=1189770888615|");
options.setUserName("321F9A89A749&xxxx");
options.setPassword("64ac769d2f523d48730294bffe6323a566005543e3e372c802903f748f118a");
Imqtt mqtt = Mqtt.initialize(this,options);
long cid = mqtt.addConnectListener(new OnConnectListener() {
@Override
public void onConnectionLost(Throwable cause) {
}
@Override
public void onConnectionSuccessful(IMqttToken token) {
}
@Override
public void onConnectionFailed(IMqttToken token, Throwable exception) {
}
});
long mid = mqtt.addMessageListener(new OnMessageListener() {
@Override
public void onMessageReceived(String topic, MqttMessage message) {
}
@Override
public void onMessageDelivered(IMqttDeliveryToken token) {
}
});
mqtt.connect();
mqtt.remove(mid,cid);
mqtt.clear();
mqtt.publish(String topic, String payload);
mqtt.subscribe(String topic);