繁体   English   中英

如何使用C#获取Bluetooth LE(BLE)设备的所有服务?

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

我正在Windows 10上使用C#连接到Magicblue蓝牙LE LED灯,并希望能够更改其颜色。

我可以获取GattDeviceService并访问其单一特征。 但是,我无法弄清楚如何获得设备的其他两项服​​务。 其中之一具有设置RGB颜色的写入特性。

将我的iPhone与LightBlue应用程序同时使用和Microsoft的BthGATTDump.exe,我可以看到服务和特性。 我在想,一旦我获得GattDeviceService,我就可以“ GetAllIncludeServices()”,但这将返回一个空列表。 如果我尝试获得具有Guid的特定服务,它也会失败(请参见下文):

//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

如果这对任何对我有帮助的人都有用,则下面是LED BLE设备的bthgattdump.exe输出。

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>

我想念什么傻事?

您可能不想获得“包含的服务”。 包含的服务是BLE中的一个特殊概念,我怀疑您正在使用该概念将一个服务与另一个服务链接起来。

在您的观察程序中,注意BLE设备而不是特定的服务。 使用该BLE设备,您可以获得所有主要服务的列表。

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

如果RGB颜色的服务包含在service你得到的,这个工程。 否则,事实并非如此。

也许您可以尝试以下方法:

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

有关FindAllAsync API的更多信息,请单击此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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