广播的发出地
/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/BondStateMachine.java
private void sendDisplayPinIntent(byte[] address, int pin, int variant) {
Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mRemoteDevices.getDevice(address));
if (pin != 0) {
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pin);
}
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, variant);
intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
mAdapterService.sendOrderedBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
}
因为是有序广播,而且原生里的监听并没有设置优先级。
<receiver android:name=".bluetooth.BluetoothPairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
</intent-filter>
</receiver>
所以我们就可以注册监听设置优先级来自己处理了
参照原生的写法 /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;
}
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
int pairingKey = 0;
if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION ||
type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY ||
type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,
BluetoothDevice.ERROR);
}
abortBroadcast();
}
}
配对的处理
/packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothPairingDialog.java
1. 取消配对
device.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
device.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED);
try {
device.setPairingConfirmation(false);
}catch (Exception e){
e.printStackTrace();
}
device.cancelPairingUserInput();
device.cancelBondProcess();
2. 配对
device.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
device.setMessageAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
switch (type) {
case BluetoothDevice.PAIRING_VARIANT_PIN:
case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
byte[] pinBytes = BluetoothDevice.convertPinToBytes(passkey);
if (pinBytes == null) {
return;
}
device.setPin(pinBytes);
break;
case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
int pass = Integer.parseInt(passkey);
device.setPasskey(pass);
break;
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
case BluetoothDevice.PAIRING_VARIANT_CONSENT:
device.setPairingConfirmation(true);
break;
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
break;
case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
device.setRemoteOutOfBandData();
break;
default:
}
3. 其他情况监听
class MyReceiver extends BroadcastReceiver{
public IntentFilter filter(){
IntentFilter intentFilter = new IntentFilter("android.bluetooth.device.action.PAIRING_CANCEL");
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
return intentFilter;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
if (bondState == BluetoothDevice.BOND_BONDED) {
dismiss();
}else if (bondState == BluetoothDevice.BOND_NONE){
dismiss();
}
} else if ("android.bluetooth.device.action.PAIRING_CANCEL".equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
}
}
}
关于Dialog弹窗
Window window = getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
window.clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp = window.getAttributes();
lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
设置主题供参考
<style name="Theme.Dialog" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
|