低功耗蓝牙--BluetoothGatt资源释放

一直比较懒,没有仔细整理过开发记录,今天有点小收获,分享一下

1. BluetoothGatt怎么生成

1*

BluetoothGatt = mDevice .connectGatt(mContext, false, mGattCallback)

源码

   /**
     * Connect to GATT Server hosted by this device. Caller acts as GATT client.
     * The callback is used to deliver results to Caller, such as connection status as well
     * as any further GATT client operations.
     * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
     * GATT client operations.
     * @param callback GATT callback handler that will receive asynchronous callbacks.
     * @param autoConnect Whether to directly connect to the remote device (false)
     *                    or to automatically connect as soon as the remote
     *                    device becomes available (true).
     * @throws IllegalArgumentException if callback is null
     */
    public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                     BluetoothGattCallback callback) {}

调用BluetoothDevice-》connectGatt()获取BluetoothGatt

connectGatt()参数

参数一:上下文context

参数二:是否自动连接,通常是false,(true没试过)

参数三:回调函数

2.BluetoothGatt释放

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        //连接状态的改变监听
        public void onConnectionStateChange(BluetoothGatt gatt, int status,
                int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {

            //连接
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

            //断开连接

              //2*
                BuleCommonConstants.mBluetoothGatt  = gatt;

                BuleCommonConstants.mBluetoothGatt. close();
            }
        }

        // 发现连接服务监听
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        }

        // 获取读的特征值
        public void onCharacteristicRead(BluetoothGatt gatt,
                BluetoothGattCharacteristic characteristic, int status) {
        }

        // 获取从机回复特征值
        public void onCharacteristicChanged(BluetoothGatt gatt,
                BluetoothGattCharacteristic characteristic) {
        }

        // 监听到蓝牙底层发送成功事件
        public void onCharacteristicWrite(BluetoothGatt gatt,
                BluetoothGattCharacteristic characteristic, int status) {
        };
    };

3.BluetoothGatt释放解析

在(1*)出我们已经保存了bluetoothgatt,当我们调用断开连接接口,直接通过(1*)出的bluetoothgatt,调用close()

场景一

通过apk连接上一个设备记录全局blueDevice和bluetoothgatt

然后在一设备没断开情况下,连接设备二将blueDevice和bluetoothgatt覆盖,然后调用断开接口BluetoothGatt.disconnect()

此时BluetoothGatt已经被释放掉。

就不能通过BluetoothGatt.disconnect()进行断开设备一,只能从机断开连接。但此时BluetoothGatt全局已经被释放到,但是android层还持有这个BluetoothGatt,不能及时释放就会导致异常,如主机端收到回调特征值重复等

所以正确释放流程应该(2*)出重新获取当前断开设备的BluetoothGatt,并将其释放。

4.Android官方低功耗蓝牙资源

https://www.android-doc.com/guide/topics/connectivity/bluetooth.html

 

后续会陆续分享这几年的低功耗开发,文档如有不当出,望各位路过大神,留笔指点。

                                                 未完待续。。。

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值