I am attempting to establish an insecure Bluetooth socket connection between two Android devices. I control the software on both ends. One end uses listenUsingInsecureRfcommWithServiceRecord
to listen for a connection, the other end uses createInsecureRfcommSocketToServiceRecord
to connect. This works. Sometimes.
The "sometimes" is my problem. In about 20% of the attempts, it fails and throws the following exception:
java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)
at com.example.bluetoothpoke.MainActivity$1.onClick(MainActivity.java:34)
Both devices are awake with screen on. The distance between the devices is around 1m (10 cm and 3m don't make a difference). Neither device is performing discovery. Both devices are discoverable (making both non-discoverable doesn't change anything, either).
Do I have to assume it is normal because of the wireless interface and just try again on failure, or is there something I can do? Shouldn't it work more reliably given that I am at a very comfortable distance?
Note: I found lots of similar questions, but the ones I found are either about "doesn't work at all", connecting to a non-Android device, or both.
D/BluetoothService(280): Cleaning up failed UUID channel lookup: xx:xx:xx:xx:xx:xx xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
(the X-ed out values being the target device bluetooth address and the service UUID) is the only thing I see that is different from successful connections. Not very helpful. – Weissberg