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

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

问题描述

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

本场景中,双方都是advertising和discovery,双方互相发现,双方发送连接请求(但没有发起连接).使用特定的测试用例很难使这种情况可靠地发生,但它确实发生了.

使用 Android Monitor,我从一侧捕获了有关此场景的详细信息,并在下面列出了事件序列.在这个特定的会话中,我监控的设备是非常便宜的 KitKat 手机,远程端点是三星棒棒糖平板电脑(以防万一).

  • 发现远程节点,我们发出requestConnection
  • 紧接着,Android Monitor 显示此错误:<代码><预>09-28 11:49:38.706 17321-17823/?E/NearbyConnections:在 startClient() 中,UKEY2 与端点 AqXW 失败java.io.IOException:bt 套接字已关闭,读取返回:-1在 android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:647)在 android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)在 java.io.InputStream.read(InputStream.java:162)在 java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)在 java.io.BufferedInputStream.read(BufferedInputStream.java:295)在 libcore.io.Streams.readFully(Streams.java:81)在 java.io.DataInputStream.readInt(DataInputStream.java:103)在 xkj.c(:com.google.android.gms@11509230:1)在 xkg.run(:com.google.android.gms@11509230:8)在 mng.run(:com.google.android.gms@11509230:25)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)在 mso.run(:com.google.android.gms@11509230)在 java.lang.Thread.run(Thread.java:818)

  • 在那之后,它立即命中 requestConnection 的 ResultCallback,带有 STATUS_ENDPOINT_IO_ERROR (8012).

  • 接下来,由于requestConnection返回错误,我们停止发现,等待3秒,然后再次开始发现.
  • 我们重新启动发现后的下一条跟踪消息是来自 NearbyConnections 的这条消息:<代码><预>09-28 11:49:46.333 17321-17412/?E/NearbyConnections:客户端 308946494 的 onIncomingConnection() 未能初始化与 IkFxWFf_F-sAAAAAAAAAHHA_I2pIK25WaixTQEZSNHtVbmtub3duIFVzZXI 的连接java.io.IOException: 无法读取 ConnectionRequestFrame在 xjj.a(:com.google.android.gms@11509230:61)在 xlr.run(:com.google.android.gms@11509230:12)在 mng.run(:com.google.android.gms@11509230:25)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)在 mso.run(:com.google.android.gms@11509230)在 java.lang.Thread.run(Thread.java:818)引起:java.io.IOException: bt socket closed, read return: -1在 android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:647)在 android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)在 java.io.InputStream.read(InputStream.java:162)在 java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:149)在 java.io.BufferedInputStream.read(BufferedInputStream.java:295)在 libcore.io.Streams.readFully(Streams.java:81)在 java.io.DataInputStream.readInt(DataInputStream.java:103)在 xkj.c(:com.google.android.gms@11509230:1)在 xjj.a(:com.google.android.gms@11509230:56)在 xlr.run(:com.google.android.gms@11509230:12)在 mng.run(:com.google.android.gms@11509230:25)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)在 mso.run(:com.google.android.gms@11509230)在 java.lang.Thread.run(Thread.java:818)

  • 我猜上面的错误是由另一端的 requestConnection 触发的?如果我们没有出现那个错误,它会进入我的 onConnectionInitiated 回调吗?(只是为了澄清:它没有命中我的 onConnectionInitiated 回调.)

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

  • 紧接着,我看到来自 NearbyConnections 的以下(不同的)错误消息:<代码><预>09-28 11:51:14.639 17321-17412/?E/NearbyConnections:客户端 308946494 的 sendConnectionRequest() 未能初始化与端点 AqXW 的连接xjx:在 connectImpl() 中,无法连接到端点 AqXW 的蓝牙设备 28:BE:03:0C:F1:5B在 xlo.a(:com.google.android.gms@11509230:126)在 xjs.run(:com.google.android.gms@11509230:12)在 mng.run(:com.google.android.gms@11509230:25)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)在 mso.run(:com.google.android.gms@11509230)在 java.lang.Thread.run(Thread.java:818)

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

  • 点击 requestConnection 的 ResultCallback
  • 在此之后,我看到了设备 连接然后自动断开,重复多次.他们从未获得成功的持久连接,尽管我在一系列错误之后看到了这种情况.

解决方案

那是并发连接冲突;如果您有 2 个设备同时相互连接,则在第二个设备在两个设备上触发 onConnectionInititated() 之前,Nearby Connections 将(随机)使设备的第一个 requestConnection() 失败.这就是您在第一个日志中看到java.io.IOException: bt socket closed, read return: -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.

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

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