android 蓝牙低功耗(BLE)-如何分别获取服务、特征和描述符的UUID

smtd7mpg  于 2023-11-15  发布在  Android
关注(0)|答案(3)|浏览(231)

在蓝牙低功耗协议方面遇到了很多问题。例如,一个设备有一个服务,这个服务包含一个包含描述符的特征。服务的UUID,特征和描述符是事先不知道的。我的问题是如何以一种方式获得它们的UUID,我们知道这个特定的UUID是一种服务/特征/描述符?
BluetoothGatt.getServices()没有帮助,因为它一起返回所有UUID,我们不知道哪个属于服务。我相信有一种方法可以拆分UUID。至少nRF Connect应用程序(你可以在Play商店中找到它)可以做到这一点。

mbzjlibv

mbzjlibv1#

我解决这个问题的唯一方法是使用从ScanResult检索的ScanRecordScanRecord存储有关每个扫描设备的一些信息,包括服务的UUID。一旦通过initScanning()方法开始扫描并在onScanResult()中返回任何结果,我们就可以访问ScanRecord对象:

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


我希望,我也能帮助那些与类似问题作斗争的人。

gg58donl

gg58donl2#

这里有一个所有可用特性的列表:https://www.bluetooth.com/specifications/gatt/characteristics
现在你可以遍历UUID列表并与它们进行比较。下面是一个包含其中一些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);
        }
    }
}


希望这对你有帮助。

mnemlml8

mnemlml83#

以下是如何列出关贸总协定服务的所有服务特性和描述符。

public void PrintAllBLESCD(BluetoothGatt bluetoothGatt)
{
    String s="";
    String c="";
    String d="";
    List<BluetoothGattService> servicesList = bluetoothGatt.getServices();
    for (int i = 0;i < servicesList.size(); i++)
    {
        BluetoothGattService bluetoothGattService = servicesList.get(i);
        s = bluetoothGattService.getUuid().toString();
        Log.d("blelist",s);

        List<BluetoothGattCharacteristic> bluetoothGattCharacteristicList = bluetoothGattService.getCharacteristics();

        for (BluetoothGattCharacteristic bluetoothGattCharacteristic : bluetoothGattCharacteristicList)
        {
            c = bluetoothGattCharacteristic.getUuid().toString();
            Log.d("blelist", s+"->"+c);

            List<BluetoothGattDescriptor> bluetoothGattDescriptorsList = bluetoothGattCharacteristic.getDescriptors();
            for (BluetoothGattDescriptor bluetoothGattDescriptor : bluetoothGattDescriptorsList)
            {
                d = bluetoothGattDescriptor.getUuid().toString();
                Log.d("blelist",s+"->"+c+"->"+d);
            }
        }
    }
}

字符串

相关问题