设备锁定后,Paho的MQTT客户端断开连接 [英] Paho's MQTT client disconnects after the device locks

查看:1423
本文介绍了设备锁定后,Paho的MQTT客户端断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个小型的应急按钮"应用程序,该应用程序应在医疗机构中运行.由于该项目的一个假设是独立于Internet,所以我决定使用在本地WLAN中设置服务器的MQTT.

I'm currently working on a small "Panic Button" app supposed to operate in a medical facility. As one of the project's assumptions is to be Internet-independent, I've decided to go for MQTT with a server set up in the local WLAN.

我已经实现了Paho Android服务,并且效果很好.到一定程度.一旦我锁定设备并关闭屏幕,一分钟后客户端就会断开连接.由于我已将MQTT选项设置为30s的KeepAlive间隔,这一定是由Android本身引起的,很可能进入了其锁睡眠状态.我在几个不同的智能手机上都获得了相同的结果,因此可能也与用户设置无关.

I've implemented the Paho Android Service and it works pretty good. Up to a certain point. Once I lock the device and turn off the sreen, exactly after one minute the client gets disconnected. As I've set MQTT options to KeepAlive interval of 30s, this must be caused by Android itself, probably going into its lock-sleep. I'm obtaining the same results on couple of different smartphones, so it is probably also not user settings - related.

我宁愿避免在

 public class ServerCallback implements MqttCallback
{
    public void connectionLost(Throwable cause) { 
    ...
    }
}

因为一旦由于不可预测的原因而导致连接断开,我想使用此方法来提示错误对话框.

Because I want to use this method to prompt an error dialog once connection is lost due to less predictable reasons.

如果是这样,我必须采取什么措施来防止这种断开连接?

If so, what options do I have to prevent this disconnection?

我的另一个观察结果是,只要将设备插入电源并充电,就不会断开连接.

Additional observation of mine is that as long as the device is plugged in and charging, disconnection does not occur.

推荐答案

在谷歌搜索后,我认为我找到了原因:

After googling around I think I found the reason:

Paho MQTT客户端使用 TimerTask 安排Keepalive ping. TimerTask 将在手机进入睡眠状态时停止,因此在这里是一个糟糕的选择... keepalive ping的实现可以在 TimerPingSender 类中找到.源自 MqttPingSender 类.

The Paho MQTT client uses a TimerTask to schedule the keepalive ping. A TimerTask will stop when the phone goes to sleep, and is therefore a poor choice here... The implementation for the keepalive ping can be found in the class TimerPingSender which is derived from the MqttPingSender class.

为了在手机处于休眠状态时获取定时事件,必须由 AlarmManager 触发它.我发现的问题的最佳解决方案是制作一个从 MqttPingSender 类派生的替代类.在我自己开始编写此类课程之前,我在google上搜索并找到了已经在GitHub上完成过课程的人.

In order to get timed events when the phone is sleeping, it must be triggered by the AlarmManager. The best solution to the problem I found was to make an alternative class derived from the MqttPingSender class. Before I started writing such a class myself, I googled and found someone who had already done it on GitHub.

可以在这里找到: https: //github.com/Suxsem/Domo-Android/blob/master/app/src/main/java/com/suxsem/domo/MqttPingSender.java

It can be found here: https://github.com/Suxsem/Domo-Android/blob/master/app/src/main/java/com/suxsem/domo/MqttPingSender.java

我还必须向MqttClient添加替代构造函数:

I also had to add an alternative constructor to MqttClient:

public MqttClient(String serverURI, String clientId, MqttClientPersistence persistence, MqttPingSender pingSender) throws MqttException {
    aClient = new MqttAsyncClient(serverURI, clientId, persistence, pingSender);
}

在我实例化MqttClient的位置(在我的服务中),我这样做:

and where I instantiate the MqttClient (in my Service) I do this:

MqttPingSender pingSender = new MqttPingSenderAlarm(this);
mqClient = new MqttClient("tcp://<IP>:<PORT>", "MyTestServiceID", new MemoryPersistence(), pingSender);

直到现在,这似乎都可以正常工作,但是我只测试了20-30分钟.

Until now this seems to work flawlessly, but I've only tested it 20-30 minutes.

这篇关于设备锁定后,Paho的MQTT客户端断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆