Android车机系统-蓝牙自动连接,关闭蓝牙配对对话框解决方案

一、需求场景

在特定页面中开启车机蓝牙,手机端会弹出配对对话框,但车机端不弹出对话框,直接执行点击确定后的逻辑。

二、解决方案

需要在 Android 系统应用 Setting 中做相关的修改。修改源码文件位置为:AOSP/packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothPairingRequest.java文件

修改内容如下:

public final class BluetoothPairingRequest extends BroadcastReceiver {
 
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!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);
    //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
    //  context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
    //} else {
      // Put up a notification that leads to the dialog
    //  intent.setClass(context, BluetoothPairingService.class);
    //  context.startServiceAsUser(intent, UserHandle.CURRENT);
    //}
  }
}

之后在项目代码中注册广播 BluetoothDevice.ACTION_PAIRING_REQUEST 设置最大优先级

IntentFilter filter = new IntentFilter();

// 蓝牙自动连接
filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
filter.setPriority(10000);

mContext.registerReceiver(bluetoothReceiver, filter);

在广播接收器的 onReceive 方法中拦截广播

@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null) {
        String action = intent.getAction();
        
        // 蓝牙自动连接
        if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
            abortBroadcast();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            device.setPairingConfirmation(true);
        }
    }
}

其中,abortBroadcast 方法能够拦截广播。 setPairingConfirmation(true) 为点击配对对话框的确定按键后执行的逻辑。在这里直接执行,即表示跳过对话框 直接点击确定按键的逻辑。但如果只是为了消除对话框,但不需要点击确认,该方法可以不用执行。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是 Android 实现手机与蓝牙BLE HID设备的配对连接、解绑、断开连接的代码: 1. 配对连接 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE); // 发现服务 gatt.discoverServices(); // 连接状态回调 private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { // 连接成功,发现服务 gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 连接断开 gatt.close(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { // 发现服务成功 BluetoothGattService service = gatt.getService(SERVICE_UUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID); // 读取特征值 gatt.readCharacteristic(characteristic); } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { // 读取特征值成功 byte[] value = characteristic.getValue(); // 处理特征值 handleCharacteristicValue(value); } } }; ``` 2. 解绑 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE); // 断开连接 gatt.disconnect(); // 关闭连接 gatt.close(); ``` 3. 断开连接 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE); // 断开连接 gatt.disconnect(); ``` 需要注意的是,以上代码中的 `SERVICE_UUID` 和 `CHARACTERISTIC_UUID` 分别为 BLE HID 设备的服务 UUID 和特征值 UUID,具体值需要根据实际设备进行设置。同时,还需要在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A-sL1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值