Rapunzel Van Winkle
Rapunzel Van Winkle

Reputation: 5500

Nearby Connections 2.0: Both sides request connections, but don't successfully connect

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

Upvotes: 6

Views: 1418

Answers (1)

Xlythe
Xlythe

Reputation: 2033

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.

Upvotes: 4

Related Questions