当前位置: 首页 > news >正文

Android 低功率蓝牙之BluetoothGattCallback回调方法详解

BluetoothGattCallback 是 Android 中用于处理蓝牙低功耗(BLE)设备通信的核心回调类。它负责处理与 BLE 设备的连接、服务发现、数据读写等操作的结果。以下是对 BluetoothGattCallback 的详细解析:

1. onConnectionStateChange

  • 触发时机:当与 BLE 设备的连接状态发生变化时触发。

  • 参数

    • gattBluetoothGatt 对象,表示当前连接的 GATT 客户端。

    • status:连接状态的变化结果,BluetoothGatt.GATT_SUCCESS 表示成功。

    • newState:新的连接状态,可能的值有 BluetoothProfile.STATE_CONNECTED 或 BluetoothProfile.STATE_DISCONNECTED

  • 常见操作

    • 连接成功后,调用 gatt.discoverServices() 开始发现服务。

    • 断开连接后,释放资源或尝试重新连接。

java

复制

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        // 连接成功,开始发现服务
        gatt.discoverServices();
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        // 断开连接,释放资源
        gatt.close();
    }
}

2. onServicesDiscovered

  • 触发时机:当发现 BLE 设备的服务完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • status:服务发现的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 获取服务列表并查找特定的特征(Characteristic)或描述符(Descriptor)。

java

复制

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        List<BluetoothGattService> services = gatt.getServices();
        for (BluetoothGattService service : services) {
            // 处理每个服务
        }
    }
}

3. onCharacteristicRead

  • 触发时机:当读取特征值完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • characteristic:被读取的特征对象。

    • status:读取操作的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 处理读取到的特征值。

java

复制

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        byte[] data = characteristic.getValue();
        // 处理读取到的数据
    }
}

4. onCharacteristicWrite

  • 触发时机:当写入特征值完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • characteristic:被写入的特征对象。

    • status:写入操作的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 确认写入操作是否成功。

java

复制

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 写入成功
    }
}

5. onCharacteristicChanged

  • 触发时机:当特征值发生变化时触发(通常是由于通知或指示)。

  • 参数

    • gattBluetoothGatt 对象。

    • characteristic:发生变化的特征对象。

  • 常见操作

    • 处理特征值的变化。

java

复制

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    byte[] data = characteristic.getValue();
    // 处理变化的数据
}

6. onDescriptorRead

  • 触发时机:当读取描述符完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • descriptor:被读取的描述符对象。

    • status:读取操作的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 处理读取到的描述符值。

java

复制

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        byte[] data = descriptor.getValue();
        // 处理读取到的描述符数据
    }
}

7. onDescriptorWrite

  • 触发时机:当写入描述符完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • descriptor:被写入的描述符对象。

    • status:写入操作的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 确认写入操作是否成功。

java

复制

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 写入成功
    }
}

8. onReadRemoteRssi

  • 触发时机:当读取远程设备的 RSSI(信号强度)完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • rssi:读取到的 RSSI 值。

    • status:读取操作的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 处理读取到的 RSSI 值。

java

复制

@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 处理 RSSI 值
    }
}

9. onMtuChanged

  • 触发时机:当 MTU(最大传输单元)大小发生变化时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • mtu:新的 MTU 大小。

    • status:MTU 变化的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 处理 MTU 变化后的数据传输。

java

复制

@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 处理 MTU 变化
    }
}

10. onPhyUpdate

  • 触发时机:当物理层(PHY)更新完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • txPhy:发送端的 PHY。

    • rxPhy:接收端的 PHY。

    • status:PHY 更新的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 处理 PHY 更新后的通信。

java

复制

@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 处理 PHY 更新
    }
}

11. onPhyRead

  • 触发时机:当读取物理层(PHY)信息完成时触发。

  • 参数

    • gattBluetoothGatt 对象。

    • txPhy:发送端的 PHY。

    • rxPhy:接收端的 PHY。

    • status:读取操作的结果,BluetoothGatt.GATT_SUCCESS 表示成功。

  • 常见操作

    • 处理读取到的 PHY 信息。

java

复制

@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 处理读取到的 PHY 信息
    }
}

总结

BluetoothGattCallback 是 Android BLE 开发中非常重要的类,它提供了与 BLE 设备交互的各种回调方法。开发者需要根据具体的业务需求,实现这些回调方法来处理连接、服务发现、数据读写等操作的结果。

相关文章:

  • Android 低功率蓝牙之BluetoothGattCharacteristic详解
  • 极狐GitLab 17.9 正式发布,40+ DevSecOps 重点功能解读【一】
  • “深入浅出”系列之Linux篇:(12)C++网络编程
  • nvm 让 Node.js 版本切换更灵活
  • 记录一些面试遇到的问题
  • Linux系统之配置HAProxy负载均衡服务器
  • powermock,mock使用笔记
  • 重生之我在 CSDN 学习 KMP 算法
  • Linux——Docker容器内MySQL密码忘记了如何查看
  • 信息管理之信息的萃取方法--使用渐进归纳法逐步提取高可见性笔记
  • os-copilot安装和使用体验测评
  • PHP 矩形面积和周长的程序(Program for Area And Perimeter Of Rectangle)
  • 前端网络安全面试题及答案
  • MATLAB实现遗传算法优化风电_光伏_光热_储热优化
  • Mysql创建库、表练习
  • RoboDexVLM:基于视觉-语言模型的任务规划和运动控制,实现灵巧机器人操作
  • 中原银行:从“小机+传统数据库”升级为“OceanBase+通用服务器”,30 +系统成功上线|OceanBase DB大咖说(十五)
  • pypi 配置国内镜像
  • IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
  • 数据库安装
  • 浙江一教师被指殴打并威胁小学生,教育局通报涉事人被行拘
  • 俄外长与美国务卿通电话,讨论俄美接触等问题
  • 上海市第二十届青少年科技节启动:为期半年,推出百余项活动
  • 病愈出院、跳大神消灾也办酒,新华每日电讯:农村滥办酒席何时休
  • 自强!助残!全国200个集体和260名个人受到表彰
  • 陕西旱情实探:大型灌区农业供水有保障,大旱之年无旱象