简体   繁体   中英

How can I use C# to get all the services for a Bluetooth LE (BLE) device?

I am connecting to a Magicblue Bluetooth LE LED light with C# on Windows 10 and want to be able to change its colors.

I am able to get the GattDeviceService and access its single characteristic just fine. However, I'm not able to figure out how to get to the device's other two services. One of them has a write characteristic to set the RGB Color.

Using both my iPhone with the LightBlue App and Microsofts BthGATTDump.exe I'm able to see the services and characteristics. I was thinking once I get the GattDeviceService, I could "GetAllIncludeServices()" but this returns an empty list. If I try to get a specific service for which I have the Guid, it also fails (see below):

//Watcher for Bluetooth LE Services
private void StartBLEWatcher()
{
    int discoveredServices = 0;
    // Hook up handlers for the watcher events before starting the watcher
    OnBLEAdded = async (watcher, deviceInfo) =>
    {
        Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
        {
            Debug.WriteLine("OnBLEAdded: " + deviceInfo.Id);
            GattDeviceService service = await GattDeviceService.FromIdAsync(deviceInfo.Id);
            var services = service.GetAllIncludedServices();
            int count0 = services.Count;  //returns 0 
            Guid G = new Guid("0000ffe5-0000-1000-8000-00805f9b34fb");
            var services2 = service.GetIncludedServices(G);  
            int count = services2.Count; //returns 0 although this service "should" exist
            var characteristics = service.GetAllCharacteristics();
            int count2 = characteristics.Count;  //return 1  This is the Gatt service with Notify

In case it is useful for anyone helping me, below is the bthgattdump.exe output for the LED BLE device.

C:\Program Files (x86)\Windows Kits\10\Tools\x64\Bluetooth\BthGATTDump>bthgattdump
Microsoft Bluetooth GATT database viewer v1.00 Copyright (c) Microsoft Corp.
Please select device
        0 - LEDBLE-CA913BE2
        1 - HID OVER GATT
        2: To quit
0
Selected device - LEDBLE-CA913BE2
Device Address - eb0cca913be2  (STATIC)
[Service] Handle=0x0001 Type=0x1800(GAP)
    [Characteristic] Handle=0x0002 ValueHandle=0x0003 Type=0x2a00(Device Name) Properties=(Read/Write)
        [Value] LEDBLE-CA913BE2
    [Characteristic] Handle=0x0004 ValueHandle=0x0005 Type=0x2a01(Appearance) Properties=(Read)
        [Value] [4000]
    [Characteristic] Handle=0x0006 ValueHandle=0x0007 Type=0x2a04(Peripheral Preferred Connection Parameters) Properties=(Read)
        [Value] [100018000000C800]
[Service] Handle=0x0008 Type=0x1801(GATT)
[Service] Handle=0x0009 Type=0xfff0
[Service] Handle=0x000a Type=0xffe5
    [Characteristic] Handle=0x000b ValueHandle=0x000c Type=0xffe9 Properties=(WriteWithoutResponse)
[Service] Handle=0x000d Type=0xffe0
    [Characteristic] Handle=0x000e ValueHandle=0x000f Type=0xffe4 Properties=(Notify)
        [Descriptor]  Handle=0x0010 Type=0x2902(Client Configuration)
            [Value]  No subscription

C:\Program Files (x86)\Windows Kits\10\Tools\x64\Bluetooth\BthGATTDump>

What silly thing am I missing?

You probably don't want to get the "included services". Included services is a special concept in BLE which I doubt you are using that is used to link one service from another.

In your watcher, watch for BLE devices rather than a specific service. With that BLE device you get, you can get a list of all primary services.

  var services = service.GetAllIncludedServices(); var services2 = service.GetIncludedServices(G); 

If RGB color service is included in the service you have get, this works. Otherwise, it doesn't.

Maybe you can try this:

var GatDevices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(Guid.Parse("0000ffe5-0000-1000-8000-00805f9b34fb")));
var service = await GattDeviceService.FromIdAsync(GatDevices[0].Id);
var characteristics = service.GetAllCharacteristics();

For more information on FindAllAsync API, click here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM