Android 低功率蓝牙之BluetoothGattDescriptor详解

BluetoothGattDescriptor 详解

BluetoothGattDescriptor 是 Android 中用于表示蓝牙低功耗(BLE)设备中 GATT(Generic Attribute Profile)描述符 的类。描述符是 GATT 架构中的一种属性,用于提供关于 特征值(Characteristic) 的附加信息或配置选项。它是 BLE 通信中非常重要的一部分。


1. GATT 架构中的描述符

在 BLE 的 GATT 架构中,数据是以分层结构组织的:

  • 服务(Service):代表设备提供的功能(例如电池服务、心率服务等)。

  • 特征(Characteristic):服务中的具体数据点(例如电池电量、心率值等)。

  • 描述符(Descriptor):为特征提供额外的信息或配置选项。

描述符通常用于:

  • 配置特征的行为(例如启用通知或指示)。

  • 提供特征的元数据(例如描述、格式等)。


2. 常见的描述符

以下是 BLE 中一些常见的标准描述符:

(1) Client Characteristic Configuration Descriptor (CCCD)
  • UUID : 00002902-0000-1000-8000-00805f9b34fb

  • 作用: 用于启用或禁用特征的通知(Notification)或指示(Indication)。

  • :

    • 0x0000: 禁用通知和指示。

    • 0x0001: 启用通知。

    • 0x0002: 启用指示。

(2) Characteristic User Description Descriptor
  • UUID : 00002901-0000-1000-8000-00805f9b34fb

  • 作用: 提供人类可读的特征描述(例如字符串 "Battery Level")。

(3) Characteristic Presentation Format Descriptor
  • UUID : 00002904-0000-1000-8000-00805f9b34fb

  • 作用: 描述特征值的格式(例如数据类型、单位、精度等)。

(4) Characteristic Aggregate Format Descriptor
  • UUID : 00002905-0000-1000-8000-00805f9b34fb

  • 作用: 用于描述多个特征值的组合格式。


3. BluetoothGattDescriptor 类详解

BluetoothGattDescriptor 是 Android 中用于操作描述符的类,以下是其核心方法和属性:

(1) 属性
  • UUID: 描述符的唯一标识符。

    java

    复制

    复制代码
    UUID descriptorUuid = descriptor.getUuid();
  • Permissions: 描述符的权限(例如读、写权限)。

    java

    复制

    复制代码
    int permissions = descriptor.getPermissions();
  • Value: 描述符的值(字节数组)。

    java

    复制

    复制代码
    byte[] value = descriptor.getValue();
(2) 常用方法
  • 读取描述符的值 :

    使用 BluetoothGatt.readDescriptor(descriptor) 方法读取描述符的值。

    java

    复制

    复制代码
    boolean success = gatt.readDescriptor(descriptor);

    读取结果会通过 BluetoothGattCallback.onDescriptorRead() 回调返回。

  • 写入描述符的值 :

    使用 BluetoothGatt.writeDescriptor(descriptor) 方法写入描述符的值。

    java

    复制

    复制代码
    descriptor.setValue(newValue); // 设置值
    boolean success = gatt.writeDescriptor(descriptor);

    写入结果会通过 BluetoothGattCallback.onDescriptorWrite() 回调返回。

  • 启用通知或指示 :

    通过写入 CCCD 描述符的值来启用通知或指示。

    java

    复制

    复制代码
    BluetoothGattDescriptor cccd = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    cccd.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // 启用通知
    gatt.writeDescriptor(cccd);

4. 使用示例

以下是一个典型的 BLE 通信流程,涉及描述符的操作:

(1) 启用通知

java

复制

复制代码
// 获取特征
BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid);

// 获取 CCCD 描述符
BluetoothGattDescriptor cccd = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));

// 启用通知
cccd.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(cccd);

// 设置特征的通知监听
gatt.setCharacteristicNotification(characteristic, true);
(2) 读取描述符的值

java

复制

复制代码
// 读取描述符
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
gatt.readDescriptor(descriptor);

// 在回调中处理读取结果
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        byte[] value = descriptor.getValue();
        // 处理描述符的值
    }
}
(3) 写入描述符的值

java

复制

复制代码
// 写入描述符
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
descriptor.setValue(newValue); // 设置新值
gatt.writeDescriptor(descriptor);

// 在回调中处理写入结果
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 写入成功
    }
}

5. 注意事项

  • 权限: 确保在 AndroidManifest.xml 中声明了蓝牙权限:

    xml

    复制

    复制代码
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!-- Android 12 及以上需要 -->

    运行 HTML

  • 异步操作 : BLE 操作是异步的,所有操作结果通过 BluetoothGattCallback 回调返回。

  • 线程安全: 确保 BLE 操作在主线程中执行。


通过 BluetoothGattDescriptor,开发者可以灵活地配置和控制 BLE 设备的行为,从而实现更复杂的蓝牙通信功能。

相关推荐
*星星之火*2 小时前
【GPT入门】第5课 思维链的提出与案例
android·gpt
EasyCVR3 小时前
EasyRTC嵌入式视频通话SDK的跨平台适配,构建web浏览器、Linux、ARM、安卓等终端的低延迟音视频通信
android·arm开发·网络协议·tcp/ip·音视频·webrtc
韩家老大4 小时前
RK Android14 在计算器内输入特定字符跳转到其他应用
android
张拭心6 小时前
2024 总结,我的停滞与觉醒
android·前端
夜晚中的人海6 小时前
【C语言】------ 实现扫雷游戏
android·c语言·游戏
ljx14000525507 小时前
Android AudioFlinger(一)——初识AndroidAudio Flinger
android
ljx14000525507 小时前
Android AudioFlinger(四)—— 揭开PlaybackThread面纱
android
Codingwiz_Joy7 小时前
Day04 模拟原生开发app过程 Androidstudio+逍遥模拟器
android·安全·web安全·安全性测试
叶羽西7 小时前
Android15 Camera框架中的StatusTracker
android·camera框架
梦中千秋7 小时前
安卓设备root检测与隐藏手段
android