我正在尝试将新的Android Nearby Connections v2.0 API与P2P_CLUSTER连接策略一起使用.以下问题(两个设备都请求连接但未成功连接)似乎发生在运行Android K-N的各种设备上.有时代码会获得成功的持久连接…有时它不会.
在这种情况下,双方都是广告和发现,双方互相发现,双方都发送连接请求(但未启动连接).使用特定的测试用例很难使这种情况可靠地发生,但它确实发生了.
使用Android Monitor,我从一侧捕获了有关此场景的详细信息,并列出了下面的事件顺序.在这个特定的会话中,我正在监控的设备是一个非常便宜的KitKat手机,远程端点是三星Lollipop平板电脑(如果相关).
>发现远程节点,我们发出requestConnection
>之后,Android Monitor立即显示以下错误:
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)
>在此之后,它立即使用STATUS_ENDPOINT_IO_ERROR(8012)命中requestConnection的ResultCallback.
>接下来,因为requestConnection返回错误,我们停止发现,等待3秒,然后再次开始发现.
>我们重新启动发现后的下一条跟踪消息是来自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)
>我猜测上面的错误是从另一方的requestConnection触发的?如果我们没有那个错误,它会转到我的onConnectionInitiated回调? (只是为了澄清:它没有点击我的onConnectionInitiated回调.)
>接下来,我再次发现另一个节点和requestConnection.
>在此之后,我立即看到来自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)
>接下来,我们使用STATUS_BLUETOOTH_ERROR(8007)命中了requestConnection的ResultCallback
>在此之后,我看到了设备connect then auto-disconnect,它重复了几次.他们从来没有得到过成功的持久连接,尽管我已经看到这种情况发生在一堆错误之后.
解决方法:
这是同时发生的连接冲突;如果您有2个设备同时相互连接,则在第二个设备在两个设备上触发onConnectionInititated()之前,Nearby Connections将(随机)首先使设备的requestConnection()之一失败.这就是你在第一个日志中看到“java.io.IOException:bt socket closed,read return:-1”的原因.
如果不是STATUS_BLUETOOTH_ERROR失败,您应该看到两个设备成功连接.