附近的连接2.0:成功连接,然后立即断开连接 [英] Nearby Connections 2.0: Successful connection, immediately followed by disconnection

查看:138
本文介绍了附近的连接2.0:成功连接,然后立即断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将附近连接2.0与P2P_CLUSTER连接策略配合使用,但是在使我的设备保持连接状态时遇到了问题.根据我的跟踪日志,设备似乎已成功连接,然后立即断开连接.

I'm trying to use Nearby Connections 2.0 with the P2P_CLUSTER connection strategy, and I'm having problems getting my devices to connect and stay connected. According to my trace logs, it looks like the devices are successfully connecting and then getting instantly disconnected.

请注意,有时它也可以成功地运行相同的代码,然后保持连接状态.我不知道是什么使它有时工作,而另一些时间又失败了,我也不知道为什么节点会自动断开连接.但是,我确定我的代码中没有发出断开连接"信息.它来自较低级别的东西.

Note that it also sometimes runs through the same code successfully, and then stays connected. I don't know what makes it work sometimes and fail other times, and I don't know why the nodes are getting automatically disconnected. However, I'm certain that the "disconnect" isn't being issued in my code. It's coming from something at a lower level.

这是我当前看到的事件序列:

Here's the sequence of events that I'm currently seeing:

  • 我们发现另一个节点(即,打了onEndpointFound回调),然后发出requestConnection
  • 我们点击了该端点的onConnectionInitiated. (在这里检查info.isIncomingConnection(),这是对我们自己的请求的响应,还是在其他地方发起的请求,似乎都没有关系.)
  • 我们发出acceptConnection(并将我们的状态更改为PENDING)
  • 我们以成功"状态命中onConnectionResult.一切都很好(双方都短暂地认为他们已经建立联系).
  • 此后,我们立即打上Disconnected(双方都进入未知状态)

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

This was a big improvement, and the devices almost always establish a successful (and lasting) connection now. However, sometimes it takes 2-3 tries.

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

For example, here's what happened when running a simple test case (with one advertising device and one discovering device, and no complicating factors). From the Discoverer's point of view, it looked like this:

  • 我们点击onEndpointFoundrequestConnection
  • 我们点击onConnectionInitiated进行我们请求的连接,并接受它
  • 我们成功连接后击中了onConnectionResult.
  • 此后不久,我们点击了onDisconnected
  • 我们停止发现,等待3秒钟,然后重新开始发现
  • 我们第二次完成以上所有操作,再次断开连接
  • we hit onEndpointFound and requestConnection
  • we hit onConnectionInitiated for the connection we requested, and accept it
  • we hit onConnectionResult with successful connection.
  • very soon afterwards, we hit onDisconnected
  • we stop discovery, wait 3 seconds, restart discovery
  • we run through all of the above a second time, getting disconnected again

但是第三次​​运行连接过程后,我们获得了良好的持久连接,并且能够可靠地发送和接收数据.

But running through the connection process a third time, we got a good persistent connection, and were able to send and receive data reliably.

我遇到了很多这样的自动断开连接.每次,广告客户在断开连接之前都会有此例外.

I caught a long series of these auto-disconnects. Each time, the Advertiser had this exception just before they got disconnected.

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)

这是否可以帮助您诊断问题?

Does this help you diagnose the problem?

推荐答案

糟糕!这是附近的连接"中的竞赛条件.在下一个即将发布的版本中已修复.作为临时解决方法,将您的接受连接延迟一小段时间(约100毫秒?).

Oops! This is a race condition in Nearby Connections. It's fixed in the next upcoming release. As a temporary fix, delay your accept connection by a small amount (~100ms?).

自Google Play服务11.6.0起该问题已修复

This should be fixed as of Google Play Services 11.6.0

这篇关于附近的连接2.0:成功连接,然后立即断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆