4

My Android device is trying to connect to a sensor via Bluetooth.

As a normal Bluetooth device, I will need to pragmatically set up the pin code (usually 0000 or 1234)

for the sensor side since it is silent and would not pop up the request dialogue.

I did not find any related clue on the Android dev site.

Does anyone can tell me if there is any approach available to achieve this?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Sam
  • 4,521
  • 13
  • 46
  • 81

1 Answers1

5

To set the PIN, you can call by reflection the hidden method setPin(byte[]) from the BluetoothDevice class.

Example:

try {
  Log.d("setPin()", "Try to set the PIN");
  Method m = device.getClass().getMethod("setPin", byte[].class);
  m.invoke(device, pin);
  Log.d("setPin()", "Success to add the PIN");
} catch (Exception e) {
  Log.e("setPin()", e.getMessage());
}

Where device is your BluetoothDevice and pin a byte[] array which contains the bluetooth device pin.

But I think, you'll prefer to use the method setPasskey(int). It would be easier for you because you want to set a passkey like "0000" or "1234".

[UPDATE]

Previous source links are dead and the class has been updated. Apparently setPasskey does not exist anymore. Follow the documentation link below to find the information you need.

Sources: BluetoothDevice Android documention

David Guyon
  • 2,759
  • 1
  • 28
  • 40