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
  • 19,827
  • 73
  • 224
  • 393
Sam
  • 4,241
  • 13
  • 42
  • 76

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,503
  • 1
  • 27
  • 39