android - 附近连接 2.0 : Successful connection, 立即断开连接

标签 android bluetooth google-nearby

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

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

以下是我当前看到的事件顺序:

  • 我们发现另一个节点(即,点击 onEndpointFound 回调),然后发出 requestConnection
  • 我们为该端点点击了 onConnectionInitiated。 (在这里检查 info.isIncomingConnection() ,这似乎是对我们自己的请求的响应,还是其他地方发起的请求并不重要。)
  • 我们发出 AcceptConnection(并将我们的状态更改为 PENDING)
  • 我们点击 onConnectionResult 并获得“成功”状态。一切都很好(双方都短暂地认为他们是有联系的)。
  • 紧接着,我们点击了“Disconnected”(双方都进入未知状态)

更新:在acceptConnection之前添加了200ms延迟

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

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

  • 我们点击了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)

这可以帮助您诊断问题吗?

最佳答案

哎呀!这是“附近连接”中的竞争条件。它已在下一个即将发布的版本中修复。作为临时解决方案,请稍微延迟您的接受连接(~100 毫秒?)。

编辑:此问题应从 Google Play Services 11.6.0 开始修复

关于android - 附近连接 2.0 : Successful connection, 立即断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46533735/

相关文章:

android - 接近信标 API : Android and Nearby

android - Nearby Messages API 在发布时返回状态代码 2806

android - 位图太大

android - 简化 Android 游戏中的 Facebook 和 Google Play 游戏服务集成

Android辅助功能更改焦点监听器

android - 使用 Google Nearby Connections 2.0 时是否可以监控发现过程?

android - 无法在 Android 5 和 BLE 设备之间建立 BLE 连接

Android - VideoView 需要按 BACK 两次才能退出

android - 低功耗蓝牙安全性如何在 Android 应用程序和 BLE 设备之间发挥作用?

android - google附近连接访问api