蓝牙搜索
我们从settings中来分析蓝牙搜索操作。
//android8.0\packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothSettings.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ID_SCAN:
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
mMetricsFeatureProvider.action(getActivity(),
MetricsEvent.ACTION_BLUETOOTH_SCAN);
//开始扫描
startScanning();
}
return true;
...
}
当点击搜索的时候,会进行扫描操作。
//先检查是否正在扫描。
private void startScanning() {
if (isUiRestricted()) {
return;
}
//附近列表
if (!mAvailableDevicesCategoryIsPresent) {
getPreferenceScreen().addPreference(mAvailableDevicesCategory);
mAvailableDevicesCategoryIsPresent = true;
}
if (mAvailableDevicesCategory != null) {
setDeviceListGroup(mAvailableDevicesCategory);
removeAllDevices();
}
//清除绑定的设备
mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
mAvailableDevicesCategory.removeAll();
mInitialScanStarted = true;
//1 调用了LocalBluetoothAdapter
mLocalAdapter.startScanning(true);
}
1. mLocalAdapter是什么?
我们看一下bluetoothsettings的父类DeviceListPreferenceFragment
packages\apps\Settings\src\com\android\settings\bluetooth\DeviceListPreferenceFragment.java
LocalBluetoothAdapter mLocalAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocalManager = Utils.getLocalBtManager(getActivity());
if (mLocalManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
return;
}
//定义了mLocalAdapter是LocalBluetoothAdapter
mLocalAdapter = mLocalManager.getBluetoothAdapter();
addPreferencesForActivity();
mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
}
看一下LocalBluetoothAdapter
这个类是在settingslib中
// frameworks/base/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothAdapter.java
private final BluetoothAdapter mAdapter;
...
public void startScanning(boolean force) {
// Only start if we're not already scanning
if (!mAdapter.isDiscovering()) {
if (!force) {
//不要扫描太频繁了
// Don't scan more than frequently than SCAN_EXPIRATION_MS,
// unless forced
if (mLastScan + SCAN_EXPIRATION_MS > System.currentTimeMillis()) {
return;
}
//当播放音乐的时不要扫描。
// If we are playing music, don't scan unless forced.
A2dpProfile a2dp = mProfileManager.getA2dpProfile();
if (a2dp != null && a2dp.isA2dpPlaying()) {
return;
}
A2dpSinkProfile a2dpSink = mProfileManager.getA2dpSinkProfile();
if ((a2dpSink != null) && (a2dpSink.isA2dpPlaying())){
return;
}
}
//开始扫描,并得到扫描的时间点
if (mAdapter.startDiscovery()) {
mLastScan = System.currentTimeMillis();
}
}
}
从settingslib中的LocalBluetoothAdapter我们调用到了接口类。从这我们可以分析出,LocalBluetoothAdapter就是对bluetoothAdapter的一个封装。
从而进行扫描的函数。BluetoothAdapter。我们通常调用api是也是调用这个类。
// frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java
public boolean startDiscovery() {
android.util.SeempLog.record(58);
if (getState() != STATE_ON) return false;
try {
mServiceLock.readLock().lock();
if (mService != null) return mService.startDiscovery();
} catch (RemoteException e) {
Log.e(TAG, "", e);
} finally {
mServiceLock.readLock().unlock();
}
return false;
}
看看mService是什么
frameworks/base/core/java/android/bluetooth/IBluetooth.aidl
boolean startDiscovery();
继承stub端在AdapterService类中
AdapterServiceBinder是AdapterService的内部类
这个是aidl调用。
/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java
private static class AdapterServiceBinder extends IBluetooth.Stub {
private AdapterService mService;
public AdapterServiceBinder(AdapterService svc) {