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

Android 附近连接2.0:成功连接,紧接着断开连接,android,bluetooth,google-nearby,Android,Bluetooth,Google Nearby,我正在尝试使用附近的Connections 2.0和P2P_集群连接策略,但我的设备在连接和保持连接时遇到问题。根据我的跟踪日志,看起来设备正在成功连接,然后立即断开连接 10-09 19:17:26.365 29232-29232/? E/SearchServiceStarter: Task 174 failed or timed out. Client 128565203126930423 disconnecting from SearchService!

我正在尝试使用附近的Connections 2.0和
P2P_集群
连接策略,但我的设备在连接和保持连接时遇到问题。根据我的跟踪日志,看起来设备正在成功连接,然后立即断开连接

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)
请注意,它有时也会成功地运行相同的代码,然后保持连接。我不知道是什么让它有时工作,有时失败,我也不知道为什么节点会自动断开连接。但是,我确信代码中没有发出“断开连接”。它来自较低层次的东西

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

  • 我们发现另一个节点(即,点击onEndpointFound回调),然后发出requestConnection
  • 我们点击了该端点的onConnectionInitiated。(在这里检查info.isIncomingConnection(),这似乎与这是对我们自己的请求的响应还是在别处发起的请求无关。)
  • 我们发出acceptConnection(并将状态移动到PENDING)
  • 我们以“成功”状态点击onConnectionResult。一切都很好(双方都认为他们之间有联系)
  • 紧接着,我们击中了onDisconnected(双方都处于未知状态)
更新:在接受连接之前增加了200ms延迟 这是一个很大的改进,现在这些设备几乎总是能够建立成功(持久)的连接。然而,有时需要2-3次尝试

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

  • 我们点击了
    onEndpointFound
    requestConnection
  • 对于我们请求的连接,我们点击
    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)

这有助于您诊断问题吗?

哎呀!这是附近连接中的竞争条件。它将在下一个即将发布的版本中修复。作为临时解决方案,请将accept连接延迟一小段时间(~100ms?)


编辑:这应该从Google Play Services 11.6.0开始修复,自动断开的频率似乎要低得多(重试时恢复得更好),延迟100毫秒,但有时仍然会发生。我应该试着再推迟一段时间吗?或者可能还有其他问题?如果您仍然看到它,是的,更长的延迟应该会有所帮助。将延迟增加到200ms。查看更新后的帖子了解更多详细信息。我很高兴延迟有帮助!至于剩下的几个断开连接,请检查logcat是否存在错误。应该存在一个一致的“未能解析”异常,该异常将竞争条件暴露为断开连接的原因。如果你看到了其他的例外情况,请告诉我:)顺便说一句,我们有一个测试版程序,在它公开发布之前,你可以获得最新的Play Services更新(包括所有修复)。如果你感兴趣的话,我只需要一个挂在电话上的Gmail地址。(StackOverflow是否有PM系统?)?我可以显示自己的消息日志,但只能在我使用Android Monitor监控的一侧看到较低级别的NearbyConnections消息(我只有一份Android Studio的副本)