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 设备的行为,从而实现更复杂的蓝牙通信功能。

相关推荐
一笑的小酒馆1 天前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺1 天前
Android BLE 扫描完整实战
android
TeleostNaCl1 天前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang95271 天前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong9511 天前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海1 天前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿1 天前
毕业三年后,我离职了
android·面试
编程乐学1 天前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐1 天前
Android14 init.rc中on boot阶段操作4
android