美文网首页
RxAndroidBLE 源码分析:建立蓝牙连接

RxAndroidBLE 源码分析:建立蓝牙连接

作者: linheimx | 来源:发表于2016-08-28 23:09 被阅读1857次

    1. 得到 外围蓝牙设备

    想与 外围的蓝牙设备相连接,首先得要获得这个蓝牙设备。

    官方例子:

    bleDevice = SampleApplication.getRxBleClient(this).getBleDevice(macAddress);
    

    具体的实现:
    由 RxClient 中的 RxBledeviceProvider 提供具体的 获取蓝牙设备的 实现:

    Paste_Image.png

    具体代码:

    Paste_Image.png
    1. 获得 BluetoothDevice
     rxBleAdapterWrapper.getRemoteDevice(macAddress);
    

    rxBleAdapterWrapper是 BluetoothAdapter 的包装,以上代码本质上是:

     public BluetoothDevice getRemoteDevice(String macAddress) {
            return bluetoothAdapter.getRemoteDevice(macAddress);
        }
    
    1. 包装 BluetoothDevice---》RxBleDeviceImpl
      包装了 BluetoothDevice,使其变的更强!
    Paste_Image.png

    如上,可以通过包装后的蓝牙设备做如下事情:

    1. 建立连接
    2. 观察连接状态
    3. 获得当前的连接状态
    4. getName,getMacAddress

    2. 观察 外围蓝牙设备的连接状态

    有了 外围的蓝牙设备,我就可以去观察他了。

    官方例子:

    // How to listen for connection state changes
    bleDevice.observeConnectionStateChanges()
            .compose(bindUntilEvent(DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this::onConnectionStateChange);
    

    深入 RxBleDeviceImpl中的 代码:

    Paste_Image.png

    connectionStateSubject:

    private final BehaviorSubject<RxBleConnection.RxBleConnectionState> connectionStateSubject = BehaviorSubject.create(DISCONNECTED);
    

    它是个平台:

    生产者 生产完东西,消费者过来买东西。他们都是通过 subject 这个平台。

    subject 有如下几种实现:

    Paste_Image.png

    着重看一下:BehaviorSubject

    蓝牙的 连接状态 事件的生产和发布 是通过 connectionStateSubject 这个平台的。
    下面看一下代码实现:

    Paste_Image.png
    1. 生产 连接中(CONNECTING) 这个事件。
    2. 生产 连接上了(CONNECTED) 这个事件。
    3. 生产 断开连接(DISCONNECTED) 这个事件。

    说着说着就到了与外围蓝牙设备的 建立连接~

    连接

    有了蓝牙设备,就可以与这个设备建立连接了。

    RxBleDeviceImpl 中 的connector专门负责连接:

    Paste_Image.png

    核心的连接与断开连接,这些代码被封装到:

    由RxBleConnectionConnectorOperationsProvider来管理。

    其中建立连接:

    Paste_Image.png

    继续:

    1. 初步检查

    若蓝牙未启用,生产一个 异常对象:BleDisconnectedException

    2. 连接过程中,考虑各种情况。

    考虑情况:未连接上的时候
    (在建立蓝牙连接的时候,若蓝牙不可用了)

    Paste_Image.png

    考虑情况:已经连接上的时候
    (这时候,蓝牙断开连接了,也要通知观察者!)

    Paste_Image.png

    考虑情况:用户不想订阅这个连接了。
    (针对这个连接,用户不想玩了:断开连接)

    下面深入:
    将 蓝牙连接这个操作 进行入队:

    Paste_Image.png

    RxBleRadioOperationConnect 的具体执行什么操作?

     @Override
        protected void protectedRun() {
            final Runnable onConnectionEstablishedRunnable = autoConnect ? emptyRunnable : releaseRadioRunnable;
            final Runnable onConnectCalledRunnable = autoConnect ? releaseRadioRunnable : emptyRunnable;
    
            getConnectedBluetoothGatt()
                    .doOnCompleted(onConnectionEstablishedRunnable::run)
                    .subscribe(getSubscriber());
    
            onConnectCalledRunnable.run();
        }
    

    该流程执行完毕后,我们建立了 一个订阅关系:
    生产者:建立蓝牙连接,连接成功后,返回此链接的 gatt。
    消费者:再将该 gatt 发射出去。

    再细看下 getConnectedBluetoothGatt()

    当你连接成功了会得到一个 gatt。

    核心:建立连接

    connectionCompat.connectGatt(bluetoothDevice, autoConnect, rxBleGattCallback.getBluetoothGattCallback())
    

    连接的建立 被封装到 BleConnectionCompat 类中:

    Paste_Image.png

    那我们看一下 他们的 connectGatt 方法:

      public BluetoothGatt connectGatt(BluetoothDevice remoteDevice, boolean autoConnect, BluetoothGattCallback bluetoothGattCallback) {
    
            if (remoteDevice == null) {
                return null;
            }
    
            if (!autoConnect) {
                return connectGattCompat(bluetoothGattCallback, remoteDevice, false);
            }
    
            /**
             * Some implementations of Bluetooth Stack have a race condition where autoConnect flag
             * is not properly set before calling connectGatt. That's the reason for using reflection
             * to set the flag manually.
             */
    
            try {
                RxBleLog.v("Trying to connectGatt using reflection.");
                Object iBluetoothGatt = getIBluetoothGatt(getIBluetoothManager());
    
                if (iBluetoothGatt == null) {
                    RxBleLog.w("Couldn't get iBluetoothGatt object");
                    return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
                }
    
                BluetoothGatt bluetoothGatt = createBluetoothGatt(iBluetoothGatt, remoteDevice);
    
                if (bluetoothGatt == null) {
                    RxBleLog.w("Couldn't create BluetoothGatt object");
                    return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
                }
    
                boolean connectedSuccessfully = connectUsingReflection(bluetoothGatt, bluetoothGattCallback, true);
    
                if (!connectedSuccessfully) {
                    RxBleLog.w("Connection using reflection failed, closing gatt");
                    bluetoothGatt.close();
                }
    
                return bluetoothGatt;
            } catch (NoSuchMethodException
                    | IllegalAccessException
                    | IllegalArgumentException
                    | InvocationTargetException
                    | InstantiationException
                    | NoSuchFieldException exception) {
                RxBleLog.w(exception, "Error during reflection");
                return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
            }
        }
    
    Paste_Image.png

    调用系统中的 connect 方法进行连接。(这个有空再详谈吧)


    Paste_Image.png

    相关文章

      网友评论

          本文标题:RxAndroidBLE 源码分析:建立蓝牙连接

          本文链接:https://www.haomeiwen.com/subject/lrkxettx.html