Nearby Connections 2.0:连接成功,立即断开连接

Nearby Connections 2.0: Successful connection, immediately followed by disconnection

我正在尝试将 Nearby Connections 2.0 与 P2P_CLUSTER 连接策略结合使用,但我在让我的设备连接和保持连接时遇到问题。根据我的跟踪日志,看起来设备已成功连接然后立即断开连接。

请注意,它有时也会 运行 成功通过相同的代码,然后保持连接。我不知道是什么让它有时工作而有时失败,而且我不知道为什么节点会自动断开连接。但是,我确定 "disconnect" 没有在我的代码中发布。它来自较低层次的东西。

这是我目前看到的事件顺序:

更新:在接受连接之前添加了 200 毫秒的延迟

这是一个很大的改进,现在设备几乎总是能建立成功(且持久)的连接。但是,有时需要尝试 2-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)

这是否有助于您诊断问题?

糟糕!这是 Nearby Connections 中的竞争条件。它已在下一个即将发布的版本中修复。作为临时修复,请稍微延迟您的接受连接(~100 毫秒?)。

编辑:自 Google Play Services 11.6.0

起应该修复此问题