Enumerate BLE devices with a given service GUID

Daniele 1,996 Reputation points
2020-10-30T09:17:52.607+00:00

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.

Developer technologies Universal Windows Platform (UWP)
{count} vote

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points Microsoft Employee Moderator
    2020-11-02T01:53:19.257+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.