I am mystified by two approaches to making BT connections in Android.
This is what I have done for as long as I can remember, and it has worked from 2.3+ devices to early 4.x. This what the Android docs describe as well.
private static final UUID sppUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(sppUUID);
bluetoothSocket.connect();
This has ceased to work on some newer Androids (Nexus 7 running 4.4), Cyanogenmod with this result (or similar):
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
After much thrashing around--much of it in SO!--I find this works
Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
bluetoothSocket = (BluetoothSocket) m.invoke(bluetoothDevice, 1);
bluetoothSocket.connect();
Where does this come from? Why does this work over the other approach?
Thank you
来源:https://stackoverflow.com/questions/21249603/making-successful-bluetooth-connections-in-android