How to get access to the BluetoothPairingDialog.java in android?

北城余情 提交于 2019-12-24 12:51:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!