在蓝牙低功耗协议问题上苦苦挣扎。例如,设备具有服务,并且此服务包含包含描述符的特征。服务,特征和描述符的UUID事先不知道。我的问题是如何以我们知道某个UUID是一种服务/特征/描述符的方式获取它们的UUID?
BluetoothGatt.getServices()
没有帮助,因为它将所有UUID一起返回,我们不知道哪一个属于该服务。我确定有一种方法可以拆分UUID。至少nRF Connect应用程序(您可以在Play商店中找到它)可以做到这一点。
答案 0 :(得分:2)
我解决此问题的唯一方法是使用从ScanRecord
检索到的ScanResult
。 ScanRecord
存储有关每个扫描设备的一些信息,包括服务'的UUID。通过ScanRecord
方法启动扫描后,我们可以访问initScanning()
对象,并在onScanResult()
中返回任何结果:
List<UUID> serviceUUIDsList = new ArrayList<>();
List<UUID> characteristicUUIDsList = new ArrayList<>();
List<UUID> descriptorUUIDsList = new ArrayList<>();
private void initScanning(BluetoothLeScannerCompat bleScanner)
{
bleScanner.startScan(getScanCallback());
}
private ScanCallback getScanCallback()
{
return new ScanCallback()
{
@Override
public void onScanResult(int callbackType, ScanResult scanResult)
{
super.onScanResult(callbackType, scanResult);
serviceUUIDsList = getServiceUUIDsList(scanResult);
}
};
}
private List<UUID> getServiceUUIDsList(ScanResult scanResult)
{
List<ParcelUuid> parcelUuids = scanResult.getScanRecord().getServiceUuids();
List<UUID> serviceList = new ArrayList<>();
for (int i = 0; i < parcelUuids.size(); i++)
{
UUID serviceUUID = parcelUuids.get(i).getUuid();
if (!serviceList.contains(serviceUUID))
serviceList.add(serviceUUID);
}
return serviceList;
}
因此,当我们知道服务UUID时,我们可以获得特征和描述符的UUID:
private void defineCharAndDescrUUIDs(BluetoothGatt bluetoothGatt)
{
List<BluetoothGattService> servicesList = bluetoothGatt.getServices();
for (int i = 0; i < servicesList.size(); i++)
{
BluetoothGattService bluetoothGattService = servicesList.get(i);
if (serviceUUIDsList.contains(bluetoothGattService.getUuid()))
{
List<BluetoothGattCharacteristic> bluetoothGattCharacteristicList = bluetoothGattService.getCharacteristics();
for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattCharacteristicList)
{
characteristicUUIDsList.add(bluetoothGattCharacteristic.getUuid());
List<BluetoothGattDescriptor> bluetoothGattDescriptorsList = bluetoothGattCharacteristic.getDescriptors();
for (BluetoothGattDescriptor bluetoothGattDescriptor : bluetoothGattDescriptorsList)
{
descriptorUUIDsList.add(bluetoothGattDescriptor.getUuid());
}
}
}
}
}
我希望,我也可以帮助其他会遇到类似问题的人。
答案 1 :(得分:0)
此处列出了所有可用的特征:https://www.bluetooth.com/specifications/gatt/characteristics
现在您可以浏览UUID列表并与它们进行比较。这是一个包含其中一些的类:
// All BLE characteristic UUIDs are of the form:
// 0000XXXX-0000-1000-8000-00805f9b34fb
// The assigned number for the Heart Rate Measurement characteristic UUID is
// listed as 0x2A37, which is how the developer of the sample code could arrive at:
// 00002a37-0000-1000-8000-00805f9b34fb
public static class Characteristic {
final static public UUID HEART_RATE_MEASUREMENT = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
final static public UUID CSC_MEASUREMENT = UUID.fromString("00002a5b-0000-1000-8000-00805f9b34fb");
final static public UUID MANUFACTURER_STRING = UUID.fromString("00002a29-0000-1000-8000-00805f9b34fb");
final static public UUID MODEL_NUMBER_STRING = UUID.fromString("00002a24-0000-1000-8000-00805f9b34fb");
final static public UUID FIRMWARE_REVISION_STRING = UUID.fromString("00002a26-0000-1000-8000-00805f9b34fb");
final static public UUID APPEARANCE = UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb");
final static public UUID BODY_SENSOR_LOCATION = UUID.fromString("00002a38-0000-1000-8000-00805f9b34fb");
final static public UUID BATTERY_LEVEL = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");
final static public UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
}
然后,当您从gatt回调中收到特征时,尝试检查(针对列表)它的特征:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
...
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
getCharacteristicValue(characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
getCharacteristicValue(characteristic);
}
}
private void getCharacteristicValue(BluetoothGattCharacteristic characteristic) {
if(characteristic.getUuid().equals(Characteristic.HEART_RATE_MEASUREMENT)) {
if (mType == Accessory.Type.HRM && mBtLeGattServiceHeartrate != null) {
mBtLeGattServiceHeartrate.onCharacteristicChanged(mContext, BtLeDevice.this, characteristic);
}
}
}
希望这有帮助。