Android 低功率蓝牙之BluetoothGattCharacteristic详解

发布于:2025-03-07 ⋅ 阅读:(18) ⋅ 点赞:(0)

BluetoothGattCharacteristic 是 Android 蓝牙低功耗(BLE)开发中的一个核心类,用于表示 GATT(Generic Attribute Profile)服务中的特征值(Characteristic)。特征值是 BLE 设备之间通信的基本数据单元,通常用于读取、写入或通知数据。

1. 基本概念

  • GATT: Generic Attribute Profile,定义了 BLE 设备之间通信的数据结构和协议。

  • Characteristic: 特征值,是 GATT 服务中的一个数据点,包含一个值和一组描述符(Descriptors)。

  • Descriptor: 描述符,用于描述特征值的元数据,例如特征值的格式、权限等。

2. BluetoothGattCharacteristic 的主要属性

  • UUID: 唯一标识符,用于区分不同的特征值。

  • Properties: 特征值的属性,定义了可以对特征值执行的操作(如读、写、通知等)。

  • Value: 特征值的实际数据,通常是一个字节数组。

  • Permissions: 特征值的权限,定义了客户端可以执行的操作(如读、写等)。

3. Properties

特征值的属性决定了客户端可以对该特征值执行的操作。常见的属性包括:

  • PROPERTY_READ: 可读。

  • PROPERTY_WRITE: 可写。

  • PROPERTY_NOTIFY: 可通知(服务器可以在值改变时通知客户端)。

  • PROPERTY_INDICATE: 可指示(类似于通知,但需要客户端确认)。

  • PROPERTY_WRITE_NO_RESPONSE: 可写且不需要响应。

4. Permissions

特征值的权限决定了客户端可以执行的操作。常见的权限包括:

  • PERMISSION_READ: 允许读取。

  • PERMISSION_WRITE: 允许写入。

  • PERMISSION_READ_ENCRYPTED: 允许加密读取。

  • PERMISSION_WRITE_ENCRYPTED: 允许加密写入。

5. 常用方法

  • getValue(): 获取特征值的当前值,返回一个字节数组。

  • setValue(byte[] value): 设置特征值的值。

  • setValue(String value): 设置特征值的值为字符串。

  • getProperties(): 获取特征值的属性。

  • getPermissions(): 获取特征值的权限。

  • getUuid(): 获取特征值的 UUID。

  • getDescriptor(UUID uuid): 获取指定 UUID 的描述符。

  • getDescriptors(): 获取所有描述符。

6. 使用示例

以下是一个简单的示例,展示了如何读取和写入一个特征值:

java

复制

BluetoothGattCharacteristic characteristic = ...; // 获取特征值

// 读取特征值
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
    boolean success = mBluetoothGatt.readCharacteristic(characteristic);
    if (success) {
        // 读取成功,可以在 onCharacteristicRead 回调中获取值
    }
}

// 写入特征值
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
    characteristic.setValue("Hello, BLE!");
    boolean success = mBluetoothGatt.writeCharacteristic(characteristic);
    if (success) {
        // 写入成功
    }
}

// 启用通知
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
    mBluetoothGatt.setCharacteristicNotification(characteristic, true);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

7. 回调处理

在 BluetoothGattCallback 中,你可以处理特征值的读取、写入和通知事件:

java

复制

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        byte[] value = characteristic.getValue();
        // 处理读取到的值
    }
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        // 写入成功
    }
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    byte[] value = characteristic.getValue();
    // 处理通知的值
}

8. 总结

BluetoothGattCharacteristic 是 Android BLE 开发中的关键类,用于表示和操作 GATT 服务中的特征值。通过它,你可以读取、写入和监听特征值的变化,从而实现与 BLE 设备的通信。理解其特征值属性、权限以及如何使用它们是开发 BLE 应用的基础。


网站公告

今日签到

点亮在社区的每一天
去签到