问题
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.
来源:https://stackoverflow.com/questions/14642014/frequent-random-failures-service-discovery-failed-in-createinsecurerfcommsocke