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