Nearby Connections 2.0: Successful connection, immediately followed by disconnection

…衆ロ難τιáo~ 提交于 2019-12-02 05:48:21

问题


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:

  • we discover another node (ie, hit the onEndpointFound callback) and then issue requestConnection
  • we hit onConnectionInitiated for that endpoint. (Checking info.isIncomingConnection() here, it doesn't seem to matter whether this was a response to our own request, or a request initiated elsewhere.)
  • we issue acceptConnection (and move our status to PENDING)
  • we hit onConnectionResult with "success" status. Everything's good (and both sides briefly think they're connected).
  • immediately afterward, we hit onDisconnected (and both sides go to unknown status)

Update: Added 200ms delay before acceptConnection

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:

  • 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.

Update: Exception on Advertiser side causing disconnect?

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?


回答1:


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?).

Edit: This should be fixed as of Google Play Services 11.6.0



来源:https://stackoverflow.com/questions/46533735/nearby-connections-2-0-successful-connection-immediately-followed-by-disconnec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!