目录
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方法,

2116

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



