附近的连接2.0:连接成功,紧接着断开连接

时间:2017-10-02 21:07:39

标签: android bluetooth google-nearby

我正在尝试使用带有P2P_CLUSTER连接策略的Nearby Connections 2.0,而我在设备连接和保持连接方面遇到了问题。根据我的跟踪日志,看起来设备已成功连接,然后立即断开连接。

请注意,它有时也会成功运行相同的代码,然后保持连接状态。我不知道是什么让它有时工作而其他时候失败了,我不知道为什么节点会自动断开连接。但是,我确定我的代码中没有发布“断开连接”。它来自较低级别的东西。

以下是我目前看到的事件序列:

  • 我们发现另一个节点(即点击onEndpointFound回调),然后发出requestConnection
  • 我们点击该端点的onConnectionInitiated。 (在这里检查info.isIncomingConnection(),这是否是对我们自己的请求的响应或在其他地方发起的请求似乎并不重要。)
  • 我们发出acceptConnection(并将我们的状态移至PENDING)
  • 我们以“成功”状态点击onConnectionResult。一切都很好(双方都认为他们已经联系在一起)。
  • 之后,我们点击onDisconnected(并且双方都处于未知状态)

更新:在acceptConnection

之前添加了200ms延迟

这是一个很大的改进,现在设备几乎总能建立一个成功(和持久)的连接。但是,有时需要2-3次尝试。

例如,这是运行简单测试用例时所发生的情况(使用一个广告设备和一个发现设备,没有复杂的因素)。从Discoverer的角度来看,它看起来像这样:

  • 我们点击onEndpointFoundrequestConnection
  • 我们点击onConnectionInitiated了解我们要求的连接,然后接受
  • 我们点击onConnectionResult连接成功。
  • 之后很快,我们点击onDisconnected
  • 我们停止发现,等待3秒,重新启动发现
  • 我们第二次完成上述所有操作,再次断开连接

但是第三次​​运行连接过程,我们获得了良好的持久连接,并能够可靠地发送和接收数据。

更新:广告商方面的异常导致断开连接?

我发现了一系列这些自动断开连接。每次广告商在断开连接之前都有此例外。

10-09 19:17:26.365 29232-29232/? E/SearchServiceStarter: Task 174 failed or timed out. Client 128565203126930423 disconnecting from SearchService!
                                                     java.util.concurrent.CancellationException: Task was cancelled.
                                                         at com.google.common.util.concurrent.d.cp(SourceFile:75)
                                                         at com.google.common.util.concurrent.d.get(SourceFile:57)
                                                         at com.google.common.util.concurrent.cj.n(SourceFile:2)
                                                         at com.google.common.util.concurrent.ay.l(SourceFile:50)
                                                         at com.google.common.util.concurrent.ba.run(SourceFile:5)
                                                         at com.google.android.apps.gsa.shared.util.concurrent.a.bc.run(SourceFile:2)
                                                         at android.os.Handler.handleCallback(Handler.java:808)
                                                         at android.os.Handler.dispatchMessage(Handler.java:103)
                                                         at android.os.Looper.loop(Looper.java:193)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
                                                         at dalvik.system.NativeStart.main(Native Method)

这有助于您诊断问题吗?

1 个答案:

答案 0 :(得分:2)

糟糕!这是附近连接中的竞争条件。它已在下一个即将发布的版本中修复。作为临时修复,延迟接受连接少量(约100毫秒?)。

修改:自Google Play Services 11.6.0起,应修复此问题