6

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.

Rapunzel Van Winkle
  • 5,380
  • 6
  • 31
  • 48
  • I started working Nearby Connections Api today. I'm just trying the Walkie Talkie Automatic. There are some issues i experienced so far. Wifi is sometimes disconnected. It takes too long to connect(I'm using 2 devices to test) and if you disconnect by pausing the app and open it again both devices stays on advertasing endpoint. Do you experience this issues on your own code? If this Api is buggy i don't want to use it. I'm trying to build a walkie talkie app like Zello. I'm searching SIP and creating server but connectivity is not my field. Some tips would be very helpful. Regards. – Thracian Oct 11 '17 at 13:56
  • I also get lots of **requestConnection failed. [8007]STATUS_BLUETOOTH_ERROR** and **couldn't pair with..** errors too on sample app. – Thracian Oct 11 '17 at 13:58
  • 1
    Faith, do you mind letting me know which devices you're testing on? I can look into it. Take a look at https://stackoverflow.com/questions/46036191/error-codes-in-nearby-connections-2-0/46328410 too, to understand what the different error codes mean and how to best resolve them. – Xlythe Oct 12 '17 at 19:20
  • Fatih, I've also encountered problems with this API (as is clear from the questions I've asked), but I'm hoping that the answers to my questions can either identify bugs (so that they can be fixed) or provide documentation and workarounds (to make the API easier for developers to use). The developer is very responsive, as you can see. I've also noticed that connection is not fast, but I'm not sure if that can be fixed. You can try to keep the connections while your app is paused by putting the connection-related code into a service or a retained headless fragment, that will persist. – Rapunzel Van Winkle Oct 13 '17 at 04:50

1 Answers1

4

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.

Xlythe
  • 1,958
  • 1
  • 8
  • 12