首页 > 其他分享 >Android开发 - BluetoothGattCallback 类处理蓝牙 (BLE) 设备的连接和通信解析

Android开发 - BluetoothGattCallback 类处理蓝牙 (BLE) 设备的连接和通信解析

时间:2024-08-23 16:29:14浏览次数:5  
标签:status gatt GATT int BluetoothGatt BLE Android BluetoothGattCallback

BluetoothGattCallback 是什么

  • BluetoothGattCallback 是一个抽象类,用于接收 BLE 设备的各种回调事件。这些事件包括连接状态的变化服务的发现特性的读取和写入

BluetoothGattCallback 的主要方法

  • onConnectionStateChange(BluetoothGatt gatt, int status, int newState):当设备的连接状态发生变化时调用。比如:设备连接成功或断开连接

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        // 检查连接状态是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 检查新的连接状态是否为已连接
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.i("BluetoothGattCallback", "Connected to GATT server.");
                // 连接成功,开始发现服务
                gatt.discoverServices();
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.i("BluetoothGattCallback", "Disconnected from GATT server.");
                // 连接断开
            }
        } else {
            // 连接状态变化失败,记录警告
            Log.w("BluetoothGattCallback", "Connection state change failed: " + status);
        }
    }
    
    • 参数解析
      • gatt:代表当前的 GATT 连接
      • status:连接的状态码(通常是 BluetoothGatt.GATT_SUCCESS 表示成功)
      • newState新的连接状态(BluetoothProfile.STATE_CONNECTEDBluetoothProfile.STATE_DISCONNECTED
  • onServicesDiscovered(BluetoothGatt gatt, int status):当 BLE 设备的服务被发现时调用。这是在连接成功后进行服务发现时触发的回调

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        // 检查服务发现是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.i("BluetoothGattCallback", "Services discovered.");
            // 遍历发现的服务
            for (BluetoothGattService service : gatt.getServices()) {
                // 打印每个服务的 UUID
                Log.i("BluetoothGattCallback", "Service UUID: " + service.getUuid());
            }
        } else {
            // 服务发现失败,记录警告
            Log.w("BluetoothGattCallback", "Service discovery failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • status:服务发现的状态码(通常是 BluetoothGatt.GATT_SUCCESS 表示成功)

  • onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status):当读取特性值的操作完成时调用。此时可以获取特性的值

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        // 检查读取操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 获取读取到的特性值
            byte[] value = characteristic.getValue();
            // 打印特性值
            Log.i("BluetoothGattCallback", "Characteristic read: " + Arrays.toString(value));
        } else {
            // 读取操作失败,记录警告
            Log.w("BluetoothGattCallback", "Characteristic read failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • characteristic被读取的特性对象

      • status:读取操作的状态码

  • onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status):当写入特性值的操作完成时调用

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        // 检查写入操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 写入成功,记录信息
            Log.i("BluetoothGattCallback", "Characteristic written successfully.");
        } else {
            // 写入操作失败,记录警告
            Log.w("BluetoothGattCallback", "Characteristic write failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • characteristic被写入的特性对象

      • status:写入操作的状态码

  • onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic):当特性值发生变化时调用。这通常是因为设备向手机推送了新的数据

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        // 设备推送的数据发生变化
        byte[] value = characteristic.getValue();
        // 打印变化后的特性值
        Log.i("BluetoothGattCallback", "Characteristic changed: " + Arrays.toString(value));
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • characteristic发生变化的特性对象

  • onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status):当读取描述符的操作完成时调用

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        // 检查读取操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 获取读取到的描述符值
            byte[] value = descriptor.getValue();
            // 打印描述符值
            Log.i("BluetoothGattCallback", "Descriptor read: " + Arrays.toString(value));
        } else {
            // 读取操作失败,记录警告
            Log.w("BluetoothGattCallback", "Descriptor read failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • descriptor被读取的描述符对象

      • status读取操作的状态码

  • onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status):当写入描述符的操作完成时调用

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        // 检查写入操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 写入成功,记录信息
            Log.i("BluetoothGattCallback", "Descriptor written successfully.");
        } else {
            // 写入操作失败,记录警告
            Log.w("BluetoothGattCallback", "Descriptor write failed: " + status);
        }
    }
    
    • 参数解析

      • gatt:代表当前的 GATT 连接

      • descriptor被写入的描述符对象

      • status写入操作的状态码

  • onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status):当读取远程设备的信号强度指示 (RSSI) 完成时调用

    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        // 检查读取操作是否成功
        if (status == BluetoothGatt.GATT_SUCCESS) {
            // 打印远程设备的信号强度
            Log.i("BluetoothGattCallback", "RSSI read: " + rssi);
        } else {
            // 读取操作失败,记录警告
            Log.w("BluetoothGattCallback", "RSSI read failed: " + status);
        }
    }
    
    • gatt:代表当前的 GATT 连接

    • rssi:设备的信号强度

    • status读取操作的状态码

总结

  • BluetoothGattCallback 类的主要作用是监听和处理与 BLE 设备交互的各种事件。通过重写这些方法,你可以处理设备的连接读取和写入数据接收数据变化等操作,从而实现与 BLE 设备的有效通信

标签:status,gatt,GATT,int,BluetoothGatt,BLE,Android,BluetoothGattCallback
From: https://www.cnblogs.com/ajunjava/p/18376325

相关文章

  • Android T adout replace bootanimation
    idea_1:useotareplacebootanimation.zipidea_2:创建一个新的分区,(用于存放bootanimation.zip)可以让上层读写.idea_3:sucp前提条件:userdebug版本,默认关闭selLinux,可root//df查看设备分区情况,有些分区系统是不让去写的adbshellc4_t:/$dfFilesystem......
  • Android 关于设备定屏/黑屏/冻屏/ANR那些事
    定屏/黑屏常见问题我的理解是冻屏和定屏是一个意思.冻屏:目的就是防止执行默写操作的过程出现黑屏,冻屏的过程只是不接收输入和不执行动画,并且会截取屏幕进行显示.A:系统问题(底层/framework层)A_1:system_server_watchdog:现象多为卡顿/黑屏A_2:WMS(WindowManagerService)......
  • Android Qcom USB Driver学习(八)
    因为要看usbcharging的问题,所以需要补充一下battery的相关知识,算是入门吧BATSCH(1)VBATT_VSNS_P(2)BAT_THERM(3)I2C_SDA(4)I2C_SCL(5)VBATT_VSNS_Msbl1_hw_pre_ddr_init:(1)pm_device_init(2)pm_driver_init(3)pm_sbl_chg_init(1)pm_device_init没有研究过,也是......
  • 2024最新Stable Diffusion安装部署教程五分钟学会(附下载地址)
    附上秋葉aaaki大佬整合包下载地址......
  • Android Qcom USB Driver学习(七)
    最近遇到了USB插拔后,系统重启的问题,抓取串口log发现如下问题,log中查看trace分析就是空指针造成的panicUnabletohandlekernelreadfromunreadablememoryatvirtualaddress0000000000000000Memabortinfo:ESR=0x96000005Exceptionclass=DABT(currentEL),......
  • 【AI绘画】零基础学会Stable Diffusion!保姆级教程!附全套学习教程,你离成为大佬只差看完
    大家好,我是SD教程菌。最近,我惊奇地发现,还有不少粉丝朋友还没用过AI绘画的顶流工具——StableDiffusion,简称“SD”。今天来出一期SD版零基础的AI绘画课,分为4个部分:SD能有多强?电脑配置要求软件安装基础使用流程1.SD能有多强?其实,2022年8月StableDiffusion就开源发布了,经......
  • vue3实现拖拽效果 (vuedraggable)
    效果图使用vuedraggable实现拖拽真的是特别丝滑和简单!!下载这里是vue3版本的对应vuedraggable版本4.1.0不要下错了!!!npminstallvuedraggable@4pnpmaddvuedraggable@4官网https://github.com/SortableJS/Vue.Draggable中文网https://www.itxst.com/vue-dragg......
  • 【AI绘画入门】Stable diffusion安装教程,Windows+Mac系统,新手也能学会,看不懂算我输,文
    大家好,我是设计师子衿一、Stablediffusion简介Stablediffusion(简称SD),这是一个文本到图像生成模型,简单来说就是目前一个比较流行且效果较好的AI文生图工具,对比其他AI文生图工具,SD的最大优势就是开源免费,定制化强,目前有很多大佬帮我们弄了sd的安装包,整个安装流程难度几......
  • Android Qcom USB Driver学习(六)
    眼图基础知识与详解10分钟教会你看眼图USB2.0HUB眼图调试经验总结一篇文章教你如何全面了解眼图测试!预加重与去加重对眼图的影响关于USB通信阻抗匹配的问题硬件调试——眼图几个经典案例眼图常见问题分析包含双眼皮的情况PHYTunningdevicetree:qusb_phy0:qusb@1613......
  • AI绘画 Stable Diffusion【SD入门】:如何体验AI绘画工具Stable Diffusion,附SD安装教程
    大家好,我是程序员晓晓关于如何使用AI绘画StableDiffusion工具,一直是很多小伙伴经常咨询的问题之一。今天就和大家一起聊聊关于如何体验AI绘画工具StableDiffusion。总的来说,使用体验StableDiffusion工具主要有3种方式。方式1:本地电脑安装部署(对电脑配置要要求)方式2:......