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

相关推荐
千里马学框架4 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台4 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone4 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc4 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo4 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077004 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼4 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone4 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen4 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone4 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui