Paho MQTT AlarmPingSender 唤醒锁卡住

Paho MQTT AlarmPingSender wakelock stucked

我正在为我的项目使用 Paho Android 服务(应用名称是 Sealer)。 (link)

我测试了大约22小时,结果给我带来了一个奇怪的结果。

我的应用似乎在 CPU 很长时间(~10.5 小时)保持唤醒状态。

我通过wakelock标签在源代码中搜索,发现wakelock标签属于AlarmPingSender class。有人遇到过这个问题吗?

没有修改Android服务源码,是原装的

我附上了一些屏幕截图(环聊和 Viber 只是为了比较)。

Screenshots

编辑 1.

我的源代码中有一个代码片段:

    mqttOptions = new MqttConnectOptions();
    mqttOptions.setCleanSession(false);
    // defaultKeepAlive is 240
    mqttOptions.setKeepAliveInterval(Constants.defaultKeepAlive);

编辑 2

我认为这是来自Android服务源代码的相关代码:

/*
 * This class sends PingReq packet to MQTT broker
 */
class AlarmReceiver extends BroadcastReceiver {
    private WakeLock wakelock;
    private String wakeLockTag = MqttServiceConstants.PING_WAKELOCK
            + that.comms.getClient().getClientId();

    @Override
    public void onReceive(Context context, Intent intent) {
        // According to the docs, "Alarm Manager holds a CPU wake lock as
        // long as the alarm receiver's onReceive() method is executing.
        // This guarantees that the phone will not sleep until you have
        // finished handling the broadcast.", but this class still get
        // a wake lock to wait for ping finished.
        int count = intent.getIntExtra(Intent.EXTRA_ALARM_COUNT, -1);
        Log.d(TAG, "Ping " + count + " times.");

        Log.d(TAG, "Check time :" + System.currentTimeMillis());
        IMqttToken token = comms.checkForActivity();

        // No ping has been sent.
        if (token == null) {
            return;
        }

        // Assign new callback to token to execute code after PingResq
        // arrives. Get another wakelock even receiver already has one,
        // release it until ping response returns.
        if (wakelock == null) {
            PowerManager pm = (PowerManager) service
                    .getSystemService(Service.POWER_SERVICE);
            wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    wakeLockTag);
        }
        wakelock.acquire();
        token.setActionCallback(new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                Log.d(TAG, "Success. Release lock(" + wakeLockTag + "):"
                        + System.currentTimeMillis());
                //Release wakelock when it is done.
                if(wakelock != null && wakelock.isHeld()){
                    wakelock.release();
                }
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken,
                    Throwable exception) {
                Log.d(TAG, "Failure. Release lock(" + wakeLockTag + "):"
                        + System.currentTimeMillis());
                //Release wakelock when it is done.
                if(wakelock != null && wakelock.isHeld()){
                    wakelock.release();
                }
            }
        });
    }
}

似乎(至少根据屏幕截图)唤醒锁不知何故 'stucked',没有释放。

ping 发送器将需要在配置的任何保持活动期间唤醒以发送 ping。该应用程序需要唤醒以发送保持连接活动的数据包。我没有玩过 Paho Android 服务,但您应该能够通过将相关值添加到传递给 MQTTAndoridClient.connect() 方法的 MQTTConnectOptions 对象来更改它。

编辑:

例如

MQTTConnectOptions opts = new MQTTConnectOptions();
opts.setConnectionTimeout(240000);
client.connect(opts);

我遇到了同样的问题并创建了一个错误报告。请查看更多帮助:https://bugs.eclipse.org/bugs/show_bug.cgi?id=480134