Your code assumes a secure BT connection and attempts in two different ways (which is good). The first test you need to run is an unsafe BT connection using the following methods:
BluetoothSocket sock; // open API sock = device.createInsecureRfcommSocketToServiceRecord(serviceUuid); // if failed: hidden API createMethod = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class }); sock = (BluetoothSocket)createMethod.invoke(device, 1);
If this does not fix the problem, you must solve the problem. go to validation from uuid you go to the creation method. You probably (?) Use default SPP uuid:
UUID DEFAULT_SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
This works on many devices, but not all.
Try to ask your peer for the list of uuids that he supports :
// for apiVer >= 15 supportedUuids = device.getUuids(); // for apiVer < 15 try { Class cl = Class.forName("android.bluetooth.BluetoothDevice"); Class[] params = {}; Method method = cl.getMethod("getUuids", params); Object[] args = {}; supportedUuids = (ParcelUuid[])method.invoke(device, args); } catch (Exception e) { // no op Log.e("uuids", "Activation of getUuids() via reflection failed: " + e); }
And, if not empty, use the first in the array as a parameter to the create method.
You can also use BTWiz , written by your truly, which does everything for you in a transparent failed model and also supports asynchronous Bluetooth IO.
source share