MQTT Paho Client不自动重新连接到代理



如果连接丢失,我的paho-mqtt服务无法重新连接到代理。
在连接丢失时,我从Android客户端使用adb shell和服务器(Windows 10)托管蚊子代理的Android设备ping了两个代理。
我通过部署了Android Things(最新版本)的树莓派3B连接到局域网。局域网网络相当稳定。对于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转换。

如果没有错误信息,将很难调试。覆盖"connectionLost(Throwable cause)"&;从MqttCallback接口。这将给出错误原因。

在我的应用程序中,我在一个类中实现了IMqttActionListener和MqttCallback。跟踪应用程序是否调用了disconnect或由于其他原因断开了连接。如果应用程序没有调用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");
}
}

最新更新