Bluetooth Low Energy (BLE) - How to get UUIDs of Service, Characteristic and Descriptor separately

纵饮孤独 提交于 2020-07-08 20:38:06

问题


Struggling quite a lot with an issue regarding Bluetooth Low Energy protocol. For example, a device has a Service, and this service contains a Characteristic which contains a Descriptor. UUIDs of the Service, Characteristic and Descriptor are not known in advance. My question is how to get UUIDs of them in a way that we know that this certain UUID is a type of Service/Charactersitic/Descriptor?

BluetoothGatt.getServices() doesn't help, because it returns all UUIDs together and we don't know which one belongs to the service. I'm sure there is a way to split the UUIDs. At least nRF Connect app (you can find it in the Play Store) can do this.


回答1:


The only way for me to resolve the issue was to use ScanRecord which is retrieved from ScanResult. ScanRecord stores some information about each scanned device including services' UUIDs. We can have access to ScanRecord object once the scanning is started by initScanning() method and returned any result in 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;
}

Thus, when we know service UUIDs, we can get UUIDs of Characteristics and Descriptors:

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());
                }
            }
        }
    }
}

I hope, I can help also others which will struggle with the similar issue.




回答2:


Here you have a list of all the available characteristics: https://www.bluetooth.com/specifications/gatt/characteristics

Now you can run through the list of UUIDs and compare with them. Here is a class containing some of them:

// 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");
}

Then, when you receive the characteristic from the gatt callback, try to check (against the list) which characteristic it is:

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);
        }
    }
}

Hope this helps.



来源:https://stackoverflow.com/questions/47887029/bluetooth-low-energy-ble-how-to-get-uuids-of-service-characteristic-and-des

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!