我正在尝试使用带有 P2P_CLUSTER
连接策略的Nearby Connections 2.0,而我在设备连接和保持连接方面遇到了问题 . 根据我的跟踪日志,看起来设备已成功连接,然后立即断开连接 .
请注意,它有时也会成功运行相同的代码,然后保持连接状态 . 我不知道是什么让它有时工作而其他时候失败了,我不知道为什么节点会自动断开连接 . 但是,我确定我的代码中没有发布“断开连接” . 它来自较低级别的东西 .
这是我目前看到的事件序列:
-
我们发现另一个节点(即,点击onEndpointFound回调),然后发出requestConnection
-
我们点击该 endpoints 的onConnectionInitiated . (在这里检查info.isIncomingConnection(),这对我们自己的请求或在其他地方发起的请求的响应似乎并不重要 . )
-
我们发出acceptConnection(并将我们的状态移至PENDING)
-
我们用"success"状态命中了onConnectionResult . 一切's good (and both sides briefly think they'重新连接) .
紧接着 -
,我们点击onDisconnected(并且双方都处于未知状态)
更新:在acceptConnection之前添加了200ms延迟
这是一个很大的改进,现在设备几乎总能 Build 一个成功(和持久)的连接 . 但是,有时需要2-3次尝试 .
例如,这是运行简单测试用例时所发生的情况(使用一个广告设备和一个发现设备,没有复杂的因素) . 从Discoverer的角度来看,它看起来像这样:
-
我们点击
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)
这有助于您诊断问题吗?
1 回答
哎呀!这是附近连接中的竞争条件 . 它已在下一个即将发布的版本中修复 . 作为临时修复,延迟接受连接少量(约100毫秒?) .
编辑:自Google Play Services 11.6.0起,应修复此问题