Android Studio中如何区分BLE UUID的读、写操作属性?
区分BLE服务UUID的读写操作
BLE的UUID本身不直接对应读写操作,真正决定读写能力的是UUID所属的BluetoothGattCharacteristic的属性。你可以按照以下步骤判断:
遍历目标服务下的所有Characteristic
拿到BluetoothGattService实例后,调用getCharacteristics()获取该服务下的所有特征值,每个特征值对应具体的读写能力。检查Characteristic的属性标志
调用BluetoothGattCharacteristic.getProperties()获取属性值,通过与以下常量做按位与运算,判断支持的操作:BluetoothGattCharacteristic.PROPERTY_READ:支持读操作BluetoothGattCharacteristic.PROPERTY_WRITE:支持普通写操作(需要设备返回确认)BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE:支持无响应写(无需设备回复)BluetoothGattCharacteristic.PROPERTY_NOTIFY/PROPERTY_INDICATE:支持设备主动推送数据(类似被动读)
关联UUID与操作能力
每个Characteristic都有自己的UUID,结合属性就能明确该UUID对应的操作。示例代码如下:
// 遍历服务下的Characteristic,判断读写能力 for (BluetoothGattCharacteristic characteristic : targetService.getCharacteristics()) { int props = characteristic.getProperties(); String charUuid = characteristic.getUuid().toString(); if ((props & BluetoothGattCharacteristic.PROPERTY_READ) != 0) { // 这个UUID对应的Characteristic支持读 } if ((props & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) { // 这个UUID对应的Characteristic支持写 } }
补充几点:
- 单个Characteristic可能同时支持读写,比如同时带有
PROPERTY_READ和PROPERTY_WRITE标志 - 服务级别的UUID只是用来标识服务类型(比如标准心率服务UUID是
0000180d-0000-1000-8000-00805f9b34fb),不直接决定读写,读写能力完全由其下的Characteristic属性定义
内容的提问来源于stack exchange,提问作者sai Teja
相关产品推荐
相关产品推荐

