Hello,
using
DeviceWatcher
and AQS I can build a query to enumerate all BLE devices, for example "System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\"" .
How can I write a query to enumerate BLE devices with a given service GUID?
Currently to find a BLE device which exposes a service with GUID 0a02ece0-e0e0-4f58-a796-36fe6b457e10 (for example) I follow the steps below
I create a query for BLE devices
string aqAllBluetoothLeDevices = "System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\"";
I create a DeviceWatcher and I start it (Added event attached)
DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(aqAllBluetoothLeDevices, requestedProperties, DeviceInformationKind.AssociationEndpoint);
when a device is added I look for the GattDeviceService with the given GUID
BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceInformation.Id);
GattDeviceServicesResult gattDeviceServicesResult = await device.GetGattServicesAsync();
if (gattDeviceServicesResult == null || gattDeviceServicesResult.Status != GattCommunicationStatus.Success) return;
IReadOnlyList<GattDeviceService> gattDeviceServices = gattDeviceServicesResult.Services;
foreach (GattDeviceService gattDeviceService in gattDeviceServices)
if (gattDeviceService.Uuid == new Guid("0a02ece0-e0e0-4f58-a796-36fe6b457e10"))
// device with the given service found
I would like to know if I could create a query with AQS that allowed me to find the device hosting a service with a given GUID without enumerating all BLE devices.
Hello, when you find a list of devices that meet the requirements through the AQS, you can loop through the devices to see if the value of DeviceInformation.Id
is the same as the given GUID. I'm not sure what you mean by the given service GUID, you can check this document to find the corresponding GUID property.
Hello,
Welcome to Microsoft Q&A.
I noticed that you search for a device with a specified ID through System.Devices.Aep.ProtocolId
. If you want to find a GATT service with a specified ID, you can try System.Devices.AepService.ServiceClassId
.
looks like this:
System.Devices.Aep.ProtocolId:="{bb7bb05e-5972-42b5-94fc-76eaa7084d49}" AND
System.Devices.AepService.ServiceClassId:="{0a02ece0-e0e0-4f58-a796-36fe6b457e10}"
Regarding GATT Service ClassId, you can check this Documents AEP service class IDs.
Thanks.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
System.Devices.Aep.ProtocolId:="{bb7bb05e-5972-42b5-94fc-76eaa7084d49}" AND
System.Devices.AepService.ServiceClassId:="{0a02ece0-e0e0-4f58-a796-36fe6b457e10}"
does not work. Moreover it looks like that System.Devices.AepService.ServiceClassId
is not related to a GattDeviceService.
I tested
System.Devices.Aep.ProtocolId:="{bb7bb05e-5972-42b5-94fc-76eaa7084d49}" AND System.DeviceInterface.Bluetooth.ServiceGuid:="{0a02ece0-e0e0-4f58-a796-36fe6b457e10}"
System.Devices.Aep.ProtocolId:="{bb7bb05e-5972-42b5-94fc-76eaa7084d49}" AND System.Devices.AepService.Bluetooth.ServiceGuid:="{0a02ece0-e0e0-4f58-a796-36fe6b457e10}"
too, and they don't work either.
I used GattDeviceService.GetDeviceSelectorFromUuid to get the AQS filter, and also this does not work.
Any other possibility?
Hello @Daniele , Sorry for the late reply, through communication with the engineer, here are some information that may be helpful to this question:
About obtaining service through ID, It depends if the device is paired or not and where the UUID information is exposed. If the device is paired already then GetDeviceSelectorFromUuid
should work. If the problem persists even after pairing, you can provide the exact code and tracking log.
And the AQS query may not simultaneously scan for all unpaired LE devices and then connect to each of them to query for service UUID, unless we are talking about the UUID being present in the advertisement, in which case the BluetoothLEAdvertismentWatcher
might come in handy. As for a device that is already connected, use the GattDeviceService
to get to the service.