一、界面触发扫描
在Android原生的系统设置里面,点击添加新设备的“加号”按钮后,就会触发BluetoothScanningDevicesGroupPreferenceController的状态改变,然后去开启扫描:
protected void updateState(PreferenceGroup preferenceGroup) {
super.updateState(preferenceGroup);
if (shouldEnableScanning()) {
enableScanning(); //打开扫描
} else {
disableScanning();
}
}
private void enableScanning() {
mIsScanningEnabled = true;
if (!mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.startDiscovery();
}
........
}
意料中地会去调用BluetoothAdapter的开启扫描的方法。而BluetoothAdapter会通过Binder去远程调用Bluetooth协议栈里面的方法,发起扫描:
public boolean startDiscovery() {
......
if (mService != null) {
return mService.startDiscovery(getOpPackageName(), getAttributionTag());
}