1

Is there any way to forcefully click on "pair button" whenever the Bluetooth pairing dialog appears?

enter image description here

Muhammad Zeeshan
  • 2,441
  • 22
  • 33
  • http://stackoverflow.com/questions/17168263/how-to-pair-bluetooth-device-programmatically-android – Tom Oct 22 '13 at 21:07

1 Answers1

3

I don't know how to get access to the pairing dialog, but I was able to "force" pairing in the following way:

1) register a BroadcastReceiver for the action:

android.bluetooth.device.action.PAIRING_REQUEST

2) once the action is received, "force" the PIN using reflection:

String DEVICE_PIN = "12345";

final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
    byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, ARDUINO_PIN);
    BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin); 
}

It worked for me on GB and ICS (don't know if it works on newer releases).

Stefano S.
  • 242
  • 2
  • 3