Problem
How can I differentiate between being unable to establish a Bluetooth connection with a remote Android device because:
- scenario 1: the remote device is out of range, or its Bluetooth is disabled.
- scenario 2: the remote device is in range, but there is no server socket on the remote device to accept my connection.
What I've tried
I could not differentiate between exceptions thrown when connecting because it threw the identical exceptions in both cases:
java.io.IOException: read failed, socket might closed or timeout, read ret -1
I could not use
fetchUuidsWithSdp()
to check if my UUID supported by the remote device because it behaves the same way in either scenario...according to the documentation:This API is asynchronous and {@link #ACTION_UUID} intent is sent, with the UUIDs supported by the remote end. If there is an error in getting the SDP records or if the process takes a long time, {@link #ACTION_UUID} intent is sent with the UUIDs that is currently present in the cache...
it's behaviour also seems sort of unpredictable according to this SO thread.
lastly, I didn't want to use
sdpSearch
to differentiate between the two, because that was added in API 23, and I want to be able to support down to API 19.