问题
Is there any way to forcefully click on "pair button" whenever the Bluetooth pairing dialog appears?
回答1:
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).
来源:https://stackoverflow.com/questions/19510891/how-to-get-access-to-the-bluetoothpairingdialog-java-in-android