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

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

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

未完待续。。。

低功耗蓝牙--BluetoothGatt资源释放相关推荐

  1. Android 低功耗蓝牙开发

    初识低功耗蓝牙 Android 4.3(API Level 18)开始引入Bluetooth Low Energy(BLE,低功耗蓝牙)的核心功能并提供了相应的 API, 应用程序通过这些 API 扫 ...

  2. Android 上的低功耗蓝牙实践

    转载自:https://www.race604.com/android-ble-in-action/ 我今天分享的主题是 Android 上低功耗蓝牙的实践.这个主题比较小众.我在过去的一年多的时间里 ...

  3. TI低功耗蓝牙(BLE)介绍

    TI低功耗蓝牙(BLE)介绍 本文档翻译和修改自参考资料:CC2540Bluetooth Low Energy Software Developer's Guide (Rev. B),部分图片直接引用 ...

  4. 安卓低功耗蓝牙——手机作为外围设备

    概述 自安卓5.0后,谷歌加入了对安卓手机作为低功耗蓝牙外围设备,即服务端的支持.使得手机可以通过低功耗蓝牙进行相互通信. 开发步骤 实现这一功能其实只需要分为设置广播和设置服务器两个部分完成即可 设 ...

  5. 基于低功耗蓝牙的手机APP(文末有整个工程的下载连接哦!)

    文章目录 前言 一.低功耗蓝牙BLE是什么? 二.基于低功耗蓝牙的手机APP 1.工程源码 2.蓝牙接收数据函数 3.数据处理函数(可自定义) 一.低功耗蓝牙BLE是什么? 低功耗蓝牙是一种全新的技术 ...

  6. 网络调试助手:安卓APP集成TCP、UDP、经典蓝牙、低功耗蓝牙的调试demo

    1.前言 我终于不需要调试一个硬件设备装一个APP了,在应用商店下载别人的网络调试助手,又有广告,还不如直接写一个比较完整的网络调试助手.目前支持的功能有tcp客户端和服务端,udp,低功耗蓝牙客户端 ...

  7. 低功耗蓝牙工具APP开发实战

    <低功耗蓝牙工具APP开发实战> 什么是 LightBLE? ​ 一个功能比较全面的蓝牙调试工具.支持所有使用蓝牙4.0低功耗的设备接入调试,提供蓝牙设备搜索.读取服务.浏览特征等操作. ...

  8. Android低功耗蓝牙BLE

    低功耗蓝牙BLE与传统的蓝牙相比最大的优势是功耗降低90%,同时传输距离增大(超过100米).安全和稳定性提高(支持AES加密和CRC验证),允许Android应用程序与具有更严格电源要求的BLE设备 ...

  9. 【Android】蓝牙开发——BLE(低功耗蓝牙)(附完整Demo)

    目录 目录 前言 一.相关概念介绍 二.实战开发 三.项目演示 四.Demo案例源码地址 五.更新记录 1.2020/12/29 :修改 setupService()中错误 2.2021/05/14 ...

最新文章

  1. Swift Code Snippet
  2. 线性回归预测PM2.5值
  3. java-基础-ArrayList剖析
  4. json套json_JSON –拯救杰克逊
  5. 使用纯原生JS实现日历
  6. BP神经网络的数学原理及其算法实现
  7. sublime_text_2 注册
  8. python嵌套函数return_python嵌套函数
  9. 下载 一寸照片编辑器
  10. NetBean生成Javadoc文档
  11. android intent传文件,android如何用intent跳转到文件管理器
  12. 取得目录和取得操作系统盘符以及获取系统盘可用空间
  13. 寄给你全宇宙的爱和自太古至永劫的思念
  14. linux CentOS7虚拟机修改静态ip地址
  15. yolo+ocr集装箱字符识别(pytorch版本)
  16. 蓝桥杯 算法训练 印章
  17. FPGA(五):Quartus II 调用Fir IP核使用说明
  18. 百度Apollo计划跟踪:感知与预测中神经网络的分析
  19. [日企面试问题]日本企業面接問題
  20. 快播王欣:我所失去的一切,都要靠区块链双倍奉还!

热门文章

  1. 错误Ox80071AC3:无法完成操作,因为卷有问题。请运行chkdsk并重试(解决方法)
  2. 计算机通过usb连接打印机,USB连接打印机:见招拆招(上)
  3. 科技化浪潮下,金融牌照还有没有价值?
  4. python 立体图 交叉 平面_如何绘制相交平面?
  5. 抖音表情号内容制作方法,项目特点技巧分享,变现方法如何?
  6. 深入剖析 CSS:字体度量、行高和垂直对齐
  7. 给照片换底色(python+opencv)
  8. android 动态显示多张图片,美图秀秀上线“魔法照片”功能:静态图片秒变动态大片!...
  9. 卡片式UI设计详细指南,先收好这8条!
  10. Python量化投资初探:自动化选股策略