gpt4 book ai didi

android - MQTT Paho 客户端不会自动重新连接到代理

转载 作者:行者123 更新时间:2023-12-05 08:04:12 34 4
gpt4 key购买 nike

如果连接丢失,我的 paho-mqtt 服务无法重新连接到代理。
在连接丢失时,我使用 adb shell 从 Android 客户端 ping 两个代理,并从托管 mosquito 代理的服务器 (Windows 10) 对 Android 设备执行 ping 操作。
我通过部署了 Android Things(最新版本)的 Raspberry Pi 3B 使用 LAN 连接到网络。 LAN网络相当稳定。对于 Paho MQTT,我使用的是最新版本。
我注意到我的 MQTT 连接随机丢失。

fun connect(context: Context) {
connectOptions.keepAliveInterval=30//seconds
connectOptions.mqttVersion = MqttConnectOptions.MQTT_VERSION_3_1_1
connectOptions.isAutomaticReconnect = true
connectOptions.isCleanSession = false

connectOptions.setWill(Global.HmiSrNo + "_out", "Disconnected".toByteArray(), 2, false)
mqttAndroidClient = MqttAndroidClient(context, serverUri, clientId)
try {
val token = mqttAndroidClient.connect(connectOptions)
token.actionCallback = object : IMqttActionListener {
override fun onSuccess(asyncActionToken: IMqttToken)
{
subscribe(context)
automicLight.set(true)
issnackbarshown = true
if(Global.connectivitylost)
wantToCloseDialog = true

}
override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) {
//connectionStatus = false
Log.i("Connection", "failure")
// Give your callback on connection failure here
exception.printStackTrace()
}
}
} catch (e: MqttException) {
// Give your callback on connection failure here
e.printStackTrace()
}
}

建筑等级

<service android:name="org.eclipse.paho.android.service.MqttService" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}

最佳答案

以下是 JAVA,因此您需要进行 kotlin 转换。

如果没有错误消息,将很难调试。从 MqttCallback 接口(interface)覆盖“connectionLost(Throwable cause)”。这将为您提供错误原因。

在我的应用程序中,我在一个类中实现了 IMqttActionListener 和 MqttCallback。跟踪应用程序是否调用断开连接或因其他原因断开连接。如果应用程序没有调用 disconnect 重新启动连接过程。我过去看到的最常见原因之一是 Context 为 null,它会关闭连接并且不允许重新启动连接。如果使用 AndroidViewModel,您可以使用应比 Activity Context 生命周期更长的应用程序上下文。

public class Pg3MqttDelegate implements IMqttActionListener, MqttCallback {

....



/////////////////////// IMqttActionListener /////////////////////////////
////////////////////////////////////////////////////////////////////////

@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.v(LOG_TAG, "onSuccess: " + asyncActionToken);
viewModel.setMqttConnected(true);
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
viewModel.setError(ErrorObject.getInstance("" + exception));
Log.v(LOG_TAG, "onFailure: " + exception + ", " + asyncActionToken.getException());
}



/////////////////////////// MqttCallback ///////////////////////////////
////////////////////////////////////////////////////////////////////////


@Override
public void connectionLost(Throwable cause) {
Log.v(LOG_TAG, "connection lost: " + cause);
viewModel.setMqttConnected(false);
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.v(LOG_TAG, "Id:" + message.getId() + ", message topic:" + topic + ", message:" + message.toString());

....

}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
//triggered when publish is completed
try {
Log.v(LOG_TAG, "deliveryComplete token:" + token.getMessage());
} catch (MqttException e) {
Log.v(LOG_TAG, "could not log deliveryComplete message");
}
}

关于android - MQTT Paho 客户端不会自动重新连接到代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69615728/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com