附近的连接2.0:双方都请求连接,但未成功连接 [英] Nearby Connections 2.0: Both sides request connections, but don't successfully connect

查看:78
本文介绍了附近的连接2.0:双方都请求连接,但未成功连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的具有P2P_CLUSTER连接策略的Android附近连接v2.0 API.在运行Android K-N的各种设备上似乎会出现以下问题(两个设备都请求连接,但未成功连接).有时代码获得成功的持久连接...有时没有.

在这种情况下,双方都在做广告和发现,双方互相发现,并且双方都发送连接请求(但未启动连接).很难通过特定的测试用例来可靠地实现这种情况,但是确实会发生这种情况.

使用Android Monitor,我从一侧捕获了有关此场景的详细信息,并在下面列出了事件的顺序.在此特定会话中,我正在监视的设备是非常便宜的KitKat手机,而远程端点是Samsung Lollipop平板电脑(如果相关).

  • 发现了远程节点,我们发出了requestConnection
  • 此后,Android Monitor立即显示此错误:

  • 此后,它立即以STATUS_ENDPOINT_IO_ERROR(8012)命中requestConnection的ResultCallback.

  • 接下来,由于requestConnection返回错误,我们停止发现,等待3秒钟,然后再次开始发现.
  • 重新启动发现后的下一个跟踪消息是来自NearestConnections的以下消息:

  • 我猜上面的错误是从另一端的requestConnection触发的吗?如果我们没有该错误,它将进入我的onConnectionInitiated回调中? (只是要澄清:它没有达到我的onConnectionInitiated回调.)

  • 接下来,我再次发现另一个节点,并再次发现requestConnection.

  • 在此之后,我立即从NearestConnections中看到以下(不同的)错误消息:

  • 接下来,我们用STATUS_BLUETOOTH_ERROR(8007)

  • 命中requestConnection的ResultCallback
  • 此后,我看到了设备 connect然后自动断开连接,该过程重复了几次.他们从来没有获得成功的持久连接,尽管我已经看到在一系列错误之后会发生这种情况.

解决方案

这是同时发生的连接冲突;如果您有2个设备同时相互连接,则在第二个设备在两个设备上触发onConnectionInititated()之前,附近连接将(随机)使该设备的第一个requestConnection()失败.这就是为什么在第一个日志中看到"java.io.IOException:bt套接字关闭,读取返回:-1"的原因.

如果不是因为STATUS_BLUETOOTH_ERROR故障,您应该会看到两个设备成功连接.

I'm trying to use the new Android Nearby Connections v2.0 API with the P2P_CLUSTER connection strategy. The following problem (in which both devices request connections, but don't successfully connect) seems to occur on a variety of devices running Android K-N. Sometimes the code gets a successful persistent connection... Sometimes it doesn't.

In this scenario, both sides are advertising and discovering, both sides discover each other, and both sides send connection requests (but connection is not initiated). It's difficult to make this situation happen reliably with a specific test case, but it does happen.

Using Android Monitor, I've captured detailed information about what this scenario looks like from one side, and listed the sequence of events below. In this particular session, the device that I was monitoring was a very cheap KitKat phone, and the remote endpoint was a Samsung Lollipop tablet (in case that's relevant).

  • The remote node is discovered, and we issue requestConnection
  • Immediately afterward, Android Monitor shows this error:

    09-28 11:49:38.706 17321-17823/? E/NearbyConnections: In startClient(), UKEY2 failed with endpoint AqXW
                                                      java.io.IOException: bt socket closed, read return: -1
                                                          at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:647)
                                                          at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
                                                          at java.io.InputStream.read(InputStream.java:162)
                                                          at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)
                                                          at java.io.BufferedInputStream.read(BufferedInputStream.java:295)
                                                          at libcore.io.Streams.readFully(Streams.java:81)
                                                          at java.io.DataInputStream.readInt(DataInputStream.java:103)
                                                          at xkj.c(:com.google.android.gms@11509230:1)
                                                          at xkg.run(:com.google.android.gms@11509230:8)
                                                          at mng.run(:com.google.android.gms@11509230:25)
                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                          at mso.run(:com.google.android.gms@11509230)
                                                          at java.lang.Thread.run(Thread.java:818)
    

  • Immediately after that, it hits the requestConnection's ResultCallback, with STATUS_ENDPOINT_IO_ERROR (8012).

  • Next, because the requestConnection returned an error, we stop discovery, wait 3 seconds, and then start discovery again.
  • The next trace message after we restart discovery is this one from NearbyConnections:

    09-28 11:49:46.333 17321-17412/? E/NearbyConnections: onIncomingConnection() for client 308946494 failed to initialize the connection with IkFxWFf_F-sAAAAAAAAAHHA_I2pIK25WaixTQEZSNHtVbmtub3duIFVzZXI
                                                      java.io.IOException: Failed to read ConnectionRequestFrame
                                                          at xjj.a(:com.google.android.gms@11509230:61)
                                                          at xlr.run(:com.google.android.gms@11509230:12)
                                                          at mng.run(:com.google.android.gms@11509230:25)
                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                          at mso.run(:com.google.android.gms@11509230)
                                                          at java.lang.Thread.run(Thread.java:818)
                                                       Caused by: java.io.IOException: bt socket closed, read return: -1
                                                          at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:647)
                                                          at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
                                                          at java.io.InputStream.read(InputStream.java:162)
                                                          at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)
                                                          at java.io.BufferedInputStream.read(BufferedInputStream.java:295)
                                                          at libcore.io.Streams.readFully(Streams.java:81)
                                                          at java.io.DataInputStream.readInt(DataInputStream.java:103)
                                                          at xkj.c(:com.google.android.gms@11509230:1)
                                                          at xjj.a(:com.google.android.gms@11509230:56)
                                                          at xlr.run(:com.google.android.gms@11509230:12) 
                                                          at mng.run(:com.google.android.gms@11509230:25) 
                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                          at mso.run(:com.google.android.gms@11509230) 
                                                          at java.lang.Thread.run(Thread.java:818)
    

  • I'm guessing that the above error was triggered from a requestConnection from the other side? And if we hadn't had that error, it would have gone to my onConnectionInitiated callback? (Just to clarify: It did not hit my onConnectionInitiated callback.)

  • Next, I once again discover the other node, and requestConnection once more.

  • Immediately after this, I see the following (different) error message from NearbyConnections:

    09-28 11:51:14.639 17321-17412/? E/NearbyConnections: sendConnectionRequest() for client 308946494 failed to initialize the connection with endpoint AqXW
                                                      xjx: In connectImpl(), failed to connect to Bluetooth device 28:BE:03:0C:F1:5B for endpoint AqXW
                                                          at xlo.a(:com.google.android.gms@11509230:126)
                                                          at xjs.run(:com.google.android.gms@11509230:12)
                                                          at mng.run(:com.google.android.gms@11509230:25)
                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                          at mso.run(:com.google.android.gms@11509230)
                                                          at java.lang.Thread.run(Thread.java:818)
    

  • Next, we hit the requestConnection's ResultCallback with STATUS_BLUETOOTH_ERROR (8007)

  • After this, I saw the devices connect then auto-disconnect, which repeated several times. They never did get a successful persistent connection, although I've seen this happen after a bunch of errors.

解决方案

That's a simultaneous connection clash; if you have 2 devices connecting to each other at the same time, Nearby Connections will (randomly) fail one of the device's requestConnection()'s first before the second device triggers onConnectionInititated() on both devices. That's why you're seeing "java.io.IOException: bt socket closed, read return: -1" in the first log.

If it weren't for the STATUS_BLUETOOTH_ERROR failures, you should be seeing the two devices successfully connect.

这篇关于附近的连接2.0:双方都请求连接,但未成功连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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