如何使用C#获取Bluetooth LE(BLE)设备的所有服务? [英] How can I use C# to get all the services for a Bluetooth LE (BLE) device?

查看:1239
本文介绍了如何使用C#获取Bluetooth LE(BLE)设备的所有服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

同时将我的iPhone与LightBlue App一起使用,以及Microsoft的BthGATTDump.exe,我都能看到这些服务和特征.我在想,一旦我获得GattDeviceService,我就可以"GetAllIncludeServices()",但这将返回一个空列表.如果我尝试获得具有Guid的特定服务,它也将失败(请参见下文):

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

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

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?

推荐答案

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

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.

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

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.

这篇关于如何使用C#获取Bluetooth LE(BLE)设备的所有服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆