Android自定义蓝牙配对弹框(去掉配对弹框)

目录

一、概述

二、参考系统设置Settings代码

1、BluetoothPairingRequest广播接收器注册android.bluetooth.device.action.PAIRING_REQUEST。

2、在BluetoothPairingRequest中通过获取getPairingDialogIntent进行弹配对框。

3、getPairingDialogIntent方法的实现是在BluetoothPairingService封装一个Intent,跳转到BluetoothPairingDialog中。

4、BluetoothPairingDialog实际上是通过BluetoothPairingController管理的。

三、实现方法

1、动态注册BluetoothDevice.ACTION_PAIRING_REQUEST,在合适时机进行注册。

2、编写广播接收器,获取关键信息:蓝牙名称和PIN码,abortBroadcast()方法重要,不能去掉。

3、编写自定义Dialog,具体样式等可自己修改,关键是拿到PIN码和蓝牙名称后弹框,如果点击确定按钮,则调用device.setPairingConfirmation(true)。


一、概述

在Android开发中,由于种种定制化需求,导致有些情况是无法接收到蓝牙配对信息的。或者说,自己开发的应用需要拦截蓝牙配对框的。

二、参考系统设置Settings代码

1、BluetoothPairingRequest广播接收器注册android.bluetooth.device.action.PAIRING_REQUEST。

路径:Settings/AndroidManifest.xml

<service android:name=".bluetooth.BluetoothPairingService" />


<receiver android:name=".bluetooth.BluetoothPairingRequest">
    <intent-filter>
        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
    </intent-filter>
</receiver>


<receiver android:name=".bluetooth.BluetoothPermissionRequest"
          android:permission="android.permission.BLUETOOTH_ADMIN">
    <intent-filter>
        <action android:name="android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST" />
        <action android:name="android.bluetooth.device.action.CONNECTION_ACCESS_CANCEL" />
    </intent-filter>
</receiver>

2、在BluetoothPairingRequest中通过获取getPairingDialogIntent进行弹配对框。

路径:Settings/src/com/android/settings/bluetooth/BluetoothPairingRequest.java
/**
 * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It
 * checks if the Bluetooth Settings is currently visible and brings up the PIN, the passkey or a
 * confirmation entry dialog. Otherwise it starts the BluetoothPairingService which
 * starts a notification in the status bar that can be clicked to bring up the same dialog.
 */
public final class BluetoothPairingRequest extends BroadcastReceiver {


  @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);
    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);
    }
  }
}
接下来看getPairingDialogIntent方法,
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值