I've got to write an app which has the prerequisite to allow in-app bluetooth pairing.
Googling around, i found out there's a way of programmatically pair a BluetoothDevice with API level < 19:
Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
Boolean paired = (Boolean) createBondMethod.invoke(device);
if (paired.booleanValue()) {
Log.i(LOGTAG, "Pairing bluetooth device succeded.");
} else {
Log.i(LOGTAG, "Pairing bluetooth device failed.");
}
It's a very hacky solution, reflection on private methods is quite never a good solution.. but it seems to be the only one available.
I tried it.. and I manage to pair my tablet with another Bluetooth device (it's neither a smartphone nor a tablet..).
After pairing the two devices, I need to create a BluetoothSocket and connect it.
Here the problem comes:
BluetoothSocket socket = null;
try {
final UUID uuid = this.mDevice.getUuids()[0].getUuid();
socket = this.mDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
Log.e(LOGTAG, "Connected to the socket");
this.onBluetoothDeviceConnected(socket, this.mDevice);
return;
} catch (Exception e) {
// handling exception
}
My this.mDevice is the paired device and it's not null.. But it's got no UUID, in fact the method BluetoothDevice.getUuids() returns a null value.
Have you ever been facing the same problem? Have you got any working solution to this annoing problem?
Thanks everyone!