Android系统在收到蓝牙配对请求时,会触发BluetoothDevice.ACTION_PAIRING_REQUEST广播,并通过BluetoothPairingRequest.java类处理弹窗逻辑。弹窗显示的逻辑位于BluetoothPairingDialog.java,需通过修改相关代码跳过弹窗,直接完成配对。
关键修改点
-
拦截配对请求:在BluetoothPairingRequest的onReceive方法中,直接调用设备确认配对的逻辑,而非启动弹窗Activity。
-
处理不同配对类型:根据配对请求的类型(如PIN码输入、确认配对等),自动填充或确认配对参数。
具体修改
- /packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothPairingRequest.java
public final class BluetoothPairingRequest extends BroadcastReceiver {
// add start
private static int mType = 0;
private BluetoothDevice mDevice;
// 直接执行配对确认逻辑
private void autoPair(int value) {
switch (mType) {
case BluetoothDevice.PAIRING_VARIANT_PIN: // 需要输入PIN码的情况
byte[] pin= {0,1,2,3};
mDevice.setPin(pin);
break;
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION: // 需要用户确认的情况
case BluetoothDevice.PAIRING_VARIANT_CONSENT:
mDevice.setPairingConfirmation(true);
break;
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
// Do nothing.
break;
default:
Log.e("autoPair", "Incorrect pairing type received");
}
}
// add end
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null || !action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
return;
}
// convert broadcast intent into activity intent (same action string)
Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent);
PowerManager powerManager =
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// add start
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
BluetoothDevice.ERROR);
int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,
BluetoothDevice.ERROR);
mType = type;
mDevice = device;
autoPair(pairingKey);
// add end
String deviceAddress = device != null ? device.getAddress() : null;
String deviceName = device != null ? device.getName() : null;
boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground(
context, deviceAddress, deviceName);
if (powerManager.isInteractive() && shouldShowDialog) {
// Since the screen is on and the BT-related activity is in the foreground,
// just open the dialog
// add start
//context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
// add end
} else {
// Put up a notification that leads to the dialog
intent.setClass(context, BluetoothPairingService.class);
context.startServiceAsUser(intent, UserHandle.CURRENT);
}
}
}
1万+

被折叠的 条评论
为什么被折叠?



