我正在尝试使用带有P2P_CLUSTER
连接策略的Nearby Connections 2.0,而我在设备连接和保持连接方面遇到了问题。根据我的跟踪日志,看起来设备已成功连接,然后立即断开连接。
请注意,它有时也会成功运行相同的代码,然后保持连接状态。我不知道是什么让它有时工作而其他时候失败了,我不知道为什么节点会自动断开连接。但是,我确定我的代码中没有发布“断开连接”。它来自较低级别的东西。
以下是我目前看到的事件序列:
这是一个很大的改进,现在设备几乎总能建立一个成功(和持久)的连接。但是,有时需要2-3次尝试。
例如,这是运行简单测试用例时所发生的情况(使用一个广告设备和一个发现设备,没有复杂的因素)。从Discoverer的角度来看,它看起来像这样:
onEndpointFound
和requestConnection
onConnectionInitiated
了解我们要求的连接,然后接受onConnectionResult
连接成功。onDisconnected
但是第三次运行连接过程,我们获得了良好的持久连接,并能够可靠地发送和接收数据。
我发现了一系列这些自动断开连接。每次广告商在断开连接之前都有此例外。
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)
这有助于您诊断问题吗?
答案 0 :(得分:2)
糟糕!这是附近连接中的竞争条件。它已在下一个即将发布的版本中修复。作为临时修复,延迟接受连接少量(约100毫秒?)。
修改:自Google Play Services 11.6.0起,应修复此问题