Setting之蓝牙(1)

安卓设置的蓝牙模块的BluetoothEventManager类

从命名就知道是处理蓝牙事件的管理类

主要成员变量:
private final LocalBluetoothAdapter mLocalAdapter;
蓝牙适配器本地管理类 内部持有BluetoothAdapter类,其实就是封装的一个单例模式。方法有:打开关闭蓝牙(enable(),disable()),开始扫描蓝牙设备(startScanning(boolean force)),停止扫描(stopScanning()),获取蓝牙信息等(更多看源码)

private final CachedBluetoothDeviceManager mDeviceManager;
缓存蓝牙设备管理类 具体讲这个时再说
private LocalBluetoothProfileManager mProfileManager;
本地蓝牙简介管理类,具体不知道还没研究
private final IntentFilter mAdapterIntentFilter, mProfileIntentFilter;
mAdapterIntentFilter是蓝牙状态变化广播过滤类

private final Map<String, Handler> mHandlerMap;
   蓝牙状态变化广播Action对应处理Handler,这个Handler是自定义的
 interface Handler {
  void onReceive(Context context, Intent intent, BluetoothDevice device);
    }

private Context mContext;

private final Collection mCallbacks =
new ArrayList();
所有注册的蓝牙状态变化事件的回调
回调接口

public interface BluetoothCallback {
    void onBluetoothStateChanged(int bluetoothState);
    void onScanningStateChanged(boolean started);
    void onDeviceAdded(CachedBluetoothDevice cachedDevice);
    void onDeviceDeleted(CachedBluetoothDevice cachedDevice);
    void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState);
    void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state);
}

private android.os.Handler mReceiverHandler;
处理蓝牙状态变化广播的线程

看源码可以知道构造方法中注册蓝牙变化广播和添加处理方法:

BluetoothEventManager(LocalBluetoothAdapter adapter,
            CachedBluetoothDeviceManager deviceManager, Context context) {
        mLocalAdapter = adapter;
        mDeviceManager = deviceManager;
        mAdapterIntentFilter = new IntentFilter();
        mProfileIntentFilter = new IntentFilter();
        mHandlerMap = new HashMap<String, Handler>();
        mContext = context;

        // Bluetooth on/off broadcasts
        addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler());

        ......一堆类似的方法
        // Dock event broadcasts
        addHandler(Intent.ACTION_DOCK_EVENT, new DockEventHandler());

        mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter, null, mReceiverHandler);
    }

addHandler(String action, Handler handler)方法用于添加action,和对应的处理类:

 private void addHandler(String action, Handler handler) {
        mHandlerMap.put(action, handler);
        mAdapterIntentFilter.addAction(action);
    }

总的来说就是 一个蓝牙变化action对应一个handle处理类,在广播接收器中分发action等信息,再在handle中调用每一个回调

所以BluetoothEventManager类的作用就是将蓝牙状态变化广播转化成一个个蓝牙事件,便于统一管理蓝牙状态变化

延伸:这个类中使用到的广播
BluetoothAdapter的广播
1:BluetoothAdapter.ACTION_STATE_CHANGED
本地蓝牙状态变化广播 如STATE_OFF,STATE_TURNING_ON,STATE_ON,STATE_TURNING_OFF
intent携带的数据
获取状态:

//当前状态
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                    BluetoothAdapter.ERROR);
 //之前状态                                   
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE,
                                    BluetoothAdapter.ERROR); 

2:BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED
本地蓝牙连接状态变化广播 如:STATE_DISCONNECTED,STATE_CONNECTING,STATE_CONNECTED,STATE_DISCONNECTING
intent携带的数据
获取状态和远程设备:

//远程设备
BluetoothDevice device = intent                 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//当前状态
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                                    BluetoothAdapter.ERROR);
    //前一个状态                                //当前状态
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,BluetoothAdapter.ERROR);

3:BluetoothAdapter.ACTION_DISCOVERY_STARTED
本地蓝牙开始扫面蓝牙设备广播
4:BluetoothAdapter.ACTION_DISCOVERY_FINISHED
本地蓝牙结束扫面蓝牙设备广播

BluetoothDevice.的广播
1:BluetoothDevice.ACTION_FOUND
调用startDiscovery()后,发现蓝牙设备的广播
intent携带的数据:

//远程设备
BluetoothDevice device = intent                 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 //获取BluetoothClass 
            BluetoothClass btClass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
            //可能有,当远程设备可用
            short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
            //可能有,当远程设备可用
            String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);

2:BluetoothDevice.ACTION_DISAPPEARED
当时远程设备上次可见,现在不可见是的广播
ntent携带的数据:

//远程设备
BluetoothDevice device = intent                 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

其他相关广播还有:
BluetoothDevice.ACTION_NAME_CHANGED
BluetoothDevice.ACTION_ALIAS_CHANGED

BluetoothDevice.ACTION_BOND_STATE_CHANGED
BluetoothDevice.ACTION_PAIRING_CANCEL
BluetoothDevice.ACTION_CLASS_CHANGED
BluetoothDevice.ACTION_UUID
等等具体看源码

猜你喜欢

转载自blog.csdn.net/kingyc123456789/article/details/53087376