According to Android Bluetooth documentation (Here), when the client calls BluetoothAdapter#startDiscovery and subsequently get a BluetoothDevice, it should first call BluetoothAdapter#cancelDiscovery before it attempts to call createRfcommSocketToServiceRecord because startDiscovery uses too much bandwidth that might cause the connection to fail if it's running concurrently. However, how do you know which BluetoothDevice is the server you are trying to connect to?

I don't understand how this can avoid the loop where you start discovery, finds a device (not your server), cancels discovery and tries to connect to the device, fails, re-start discovery, and repeat.

Thanks!

有帮助吗?

解决方案

One possible solution I came up with is to keep track of the addresses of all the BluetoothDevice that you already tried to connect with and failed, and on the next attempt of startDiscovery simply bypass those. However, I am still calling startDiscovery and cancelDiscovery multiple times and discovering the same wrong devices multiple times, so I would love to hear a better solution if anyone has one!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top