The normal way to connect to a bluetooth device is by pairing.
We need to connect to a device in an abnormal way: By using the Bluetooth MAC address only. We do not want to be prompted for a PIN.
We know the device supports this technique, but we cannot find a way to do it on Android.
The abbreviated code looks like this:
String mac_address = “00:11:22:33:44:55”
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(mac_address);
Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket",
new Class[] {int.class});
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
The problem is that upon socket.connect() we are prompted for a PIN. The PIN is not necessary for this device, so we do not want to be prompted for a PIN.
The reason we know the PIN is not necessary:
- We manufacture this device and write the firmware.
- We can connect to it using a program on windows written in C#.
How can we modify the code such that it will not prompt for a PIN?
[Edit to test suggestion in comment]
We tested method createInsecureRfcommSocketToServiceRecord like this:
ParcelUuid list[] = device.getUuids();
BluetoothSocket socket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(list[0].getUuid());
Then we get error like this:
I/InputDispatcher( 382): Delivering touch to current input target: action: 0x1
I/InputDispatcher( 382): Delivering touch to current input target: action: 0x1
V/BluetoothSocket.cpp(16956): initSocketNative
V/BluetoothSocket.cpp(16956): ...fd 66 created (RFCOMM, lm = 0)
V/BluetoothSocket.cpp(16956): initSocketFromFdNative
I/BluetoothPolicyService( 382): getBluetoothDataTransferAllowed
D/BluetoothUtils(16956): isSocketAllowedBySecurityPolicy start : device null
V/BluetoothService.cpp( 382): createDeviceNative
V/BluetoothService.cpp( 382): ... address =
V/BluetoothEventLoop.cpp( 382): onCreateDeviceResult
V/BluetoothEventLoop.cpp( 382): onCreateDeviceResult
E/BluetoothEventLoop.cpp( 382): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
D/BluetoothEventLoop( 382): Result of onCreateDeviceResult:1
V/BluetoothService.cpp( 382): discoverServicesNative
V/BluetoothService.cpp( 382): ... Object Path = /org/bluez/1100/hci0/dev_00_11_22_33_44_55
V/BluetoothService.cpp( 382): ... Pattern = , strlen = 0
D/FStest ( 633): check default
D/FStest ( 633): check default