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 設備的行為,從而實現更復雜的藍牙通信功能。