低功耗蓝牙(BLE)-如何分别获取服务,特征和描述符的UUID [英] Bluetooth Low Energy (BLE) - How to get UUIDs of Service, Characteristic and Descriptor separately
问题描述
与低功耗蓝牙协议有关的问题困扰很多.例如,设备具有服务,并且该服务包含包含描述符的特征.服务,特征和描述符的UUID事先未知.我的问题是如何以一种已知的特定UUID是一种服务/字符/描述符的方式获取它们的UUID?
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()
无济于事,因为它会一起返回所有UUID,而且我们不知道哪个UUID属于该服务.我确定有一种拆分UUID的方法.至少nRF Connect应用程序(您可以在Play商店中找到它)可以执行此操作.
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.
推荐答案
我解决此问题的唯一方法是使用从ScanResult
检索的ScanRecord
. ScanRecord
存储有关每个扫描设备的一些信息,包括服务的UUID.一旦通过initScanning()
方法开始扫描并在onScanResult()
中返回任何结果,我们就可以访问ScanRecord
对象:
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;
}
因此,当我们知道服务UUID时,我们可以获得特征和描述符的UUID:
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.
这篇关于低功耗蓝牙(BLE)-如何分别获取服务,特征和描述符的UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!