五蓝牙搜索

蓝牙搜索

我们从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) {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值