4

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
  • BluetoothGatt.getServices() Returns a list of GATT services offered by the remote device. after that you can go through the services to get the characteristics of each service. idk what you are doing wrong. share some code if its possible. – Salman Naseem Dec 24 '17 at 06:23

2 Answers2

4

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.

Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
  • https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – kike Jul 07 '18 at 09:54
  • @kike, I was waiting for another possible solution before accepting the answer. But somehow forgot about this question. Thanks for reminding me it. – Ayaz Alifov Jul 07 '18 at 10:55
1

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.

Ambran
  • 2,367
  • 4
  • 31
  • 46