使用MQTT Android服务从飞行模式重新连接

3
我正在按照这里http://dalelane.co.uk/blog/?p=1599的Dale Lane MQTT Android样例实现项目,这个样例对于我的特定目的工作得很好,但我面临一个无法解决的问题。
经过广泛测试,我发现当手机完全失去连接后,该服务不会重新连接。例如,如果将手机放入飞行模式,然后退出飞行模式,该服务将继续尝试connectToBroker(),但永远不会连接。
如果我在主屏幕上按下“connect”按钮并使用给定的主机/主题参数启动服务,则应用程序可以正确连接。
我该如何解决这个问题?应用程序在WIFI网络与手机数据之间切换时没有问题。
02-16 12:19:39.348: E/mqtt(23628): ping failed - MQTT exception
02-16 12:19:39.348: E/mqtt(23628): com.ibm.mqtt.MqttNotConnectedException
02-16 12:19:39.348: E/mqtt(23628):  at com.ibm.mqtt.Mqtt.writePacket(Unknown Source)
02-16 12:19:39.348: E/mqtt(23628):  at com.ibm.mqtt.Mqtt.pingOut(Unknown Source)
02-16 12:19:39.348: E/mqtt(23628):  at com.ibm.mqtt.MqttClient.ping(Unknown Source)
02-16 12:19:39.348: E/mqtt(23628):  at com.limosys.limosystestmqtt.MQTTService$PingSender.onReceive(MQTTService.java:919)
02-16 12:19:39.348: E/mqtt(23628):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
02-16 12:19:39.348: E/mqtt(23628):  at android.os.Handler.handleCallback(Handler.java:733)
02-16 12:19:39.348: E/mqtt(23628):  at android.os.Handler.dispatchMessage(Handler.java:95)
02-16 12:19:39.348: E/mqtt(23628):  at android.os.Looper.loop(Looper.java:136)
02-16 12:19:39.348: E/mqtt(23628):  at android.app.ActivityThread.main(ActivityThread.java:5105)
02-16 12:19:39.348: E/mqtt(23628):  at java.lang.reflect.Method.invokeNative(Native Method)
02-16 12:19:39.348: E/mqtt(23628):  at java.lang.reflect.Method.invoke(Method.java:515)
02-16 12:19:39.348: E/mqtt(23628):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
02-16 12:19:39.348: E/mqtt(23628):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
02-16 12:19:39.348: E/mqtt(23628):  at dalvik.system.NativeStart.main(Native Method)

以下是ping失败时抛出的异常的堆栈跟踪,应用程序将根据给定的keepAliveSecond值每隔10秒尝试重新连接。

连接丢失(CONNECTIONLOST)

/*
   * callback - method called when we no longer have a connection to the message broker server
   */
  public void connectionLost() throws Exception {
    // we protect against the phone switching off while we're doing this
    // by requesting a wake lock - we request the minimum possible wake
    // lock - just enough to keep the CPU running until we've finished
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MQTT");
    wl.acquire();


    //
    // have we lost our data connection?
    //

    if (isOnline() == false) {
      Log.e("CONNECTION LOST", "LOST CONNECTION");
      connectionStatus = MQTTConnectionStatus.NOTCONNECTED_WAITINGFORINTERNET;

      // inform the app that we are not connected any more
      broadcastServiceStatus("Connection lost - no network connection");

      //
      // inform the user (for times when the Activity UI isn't running)
      // that we are no longer able to receive messages
      notifyUser("Connection lost - no network connection", "MQTT",
          "Connection lost - no network connection");

      //
      // wait until the phone has a network connection again, when we
      // the network connection receiver will fire, and attempt another
      // connection to the broker
    } else {
      //
      // we are still online
      // the most likely reason for this connectionLost is that we've
      // switched from wifi to cell, or vice versa
      // so we try to reconnect immediately
      //

      connectionStatus = MQTTConnectionStatus.NOTCONNECTED_UNKNOWNREASON;

      // inform the app that we are not connected any more, and are
      // attempting to reconnect
      broadcastServiceStatus("Connection lost - reconnecting...");

      // try to reconnect
      if (connectToBroker()) {
        subscribeToTopic(topicName);
      }
    }

    // we're finished - if the phone is switched off, it's okay for the CPU
    // to sleep now
    wl.release();
  }

连接到代理 此代码在服务可用后(即退出飞行模式并连接到4G / Wifi)多次调用。

 /*
   * (Re-)connect to the message broker
   */
  private boolean connectToBroker() {
    try {
      // try to connect
      Log.e("CONNECTTOBROKER", "TRYING TO CONNECT");
      Log.e("SERVICE HOST - CONNECT TO BROKER", "" + brokerHostName);
      Log.e("SERVICE TOPIC - CONNECT TO BROKER", "" + topicName);

      mqttClient.connect(generateClientId(), cleanStart, keepAliveSeconds);

      //
      // inform the app that the app has successfully connected
      broadcastServiceStatus("Connected");

      // we are connected
      connectionStatus = MQTTConnectionStatus.CONNECTED;

      // we need to wake up the phone's CPU frequently enough so that the
      // keep alive messages can be sent
      // we schedule the first one of these now
      scheduleNextPing();


      return true;
    } catch (MqttException e) {
      // something went wrong!

      connectionStatus = MQTTConnectionStatus.NOTCONNECTED_UNKNOWNREASON;

      //
      // inform the app that we failed to connect so that it can update
      // the UI accordingly
      broadcastServiceStatus("Unable to connect");

      //
      // inform the user (for times when the Activity UI isn't running)
      // that we failed to connect
      notifyUser("Unable to connect", "MQTT", "Unable to connect - will retry later");

      // if something has failed, we wait for one keep-alive period before
      // trying again
      // in a real implementation, you would probably want to keep count
      // of how many times you attempt this, and stop trying after a
      // certain number, or length of time - rather than keep trying
      // forever.
      // a failure is often an intermittent network issue, however, so
      // some limited retry is a good idea
      scheduleNextPing();

      return false;
    }
  }
1个回答

3

从paho下载最新的Android服务jar包(1.02)和Java客户端(1.02)。最新的Android服务jar包已经修复了此问题。它会在网络连接恢复后自动重新连接。

将此代码添加到Activity的onResume()方法中:

mqttclient.registerResources(this);     

在同一个Activity的onDestroy()方法中加入以下代码:

mqttclient.unregisterResources();

这对我很有帮助。

非常感谢...帮我解决了一个长期存在的问题:D - kdoteu
2
@kdoteu 我对Paho很感兴趣,我正在使用它。但是为什么在你杀掉应用程序时它会停止服务呢?这个问题在某些版本中得到了解决还是仍然存在?Ravi,你也可以回答一下。 :D - Muhamed Huseinbašić
@kdoteu,当您从最近的应用程序中滑动该应用程序时,您的服务是否停止?这两个问题描述了此问题:https://dev59.com/CYfca4cB1Zd3GeqPpOl9 和 https://dev59.com/0Ifca4cB1Zd3GeqPgDxs - Muhamed Huseinbašić
@MuhamedHuseinbašić 不,服务不会停止。我认为这是因为我在清单文件中添加了 ‹service android:name="com.ibm.android.service.MqttService" /›。 - kdoteu
@MuhamedHuseinbašić 你使用.unregisterResource()还是其他方法?我只在失去连接并想要重新连接时使用此方法。如果你在onDestroy()中使用此方法,那么你就无法从服务中得到通知。 - kdoteu
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接