我应该在我的UWP应用程序中使用哪些蓝牙功能从我的BLE设备读取字符

时间:2018-05-29 06:23:05

标签: c# uwp bluetooth-lowenergy

我正试图从我的BLE设备读取温度特征(Blue gecko bgm13p22) 我试过C #Windows BLE代码 此

GattReadResult result = await selectedCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);//ReadValueAsync-->Performs a Characteristic Value read from the value cache maintained by Windows or retrieve from Windows

和这个

GattReadResult result = await selectedCharacteristic.ReadValueAsync();//ReadValueAsync-->Performs a Characteristic Value read from the value cache maintained by Windows or retrieve from Windows

并在package.appxmanifest中 我添加了这个

 <DeviceCapability Name="bluetooth.genericAttributeProfile">
       <Device Id="any">
          <Function Type="name:healthThermometer" />
       </Device>
    </DeviceCapability>   
  </Capabilities>

从BLE设备读取温度值但仍无法读取

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

Blue Gecko使用Bluetooth SIG采用的健康温度计服务。 SIG的文档说明如下: 资料来源:https://www.bluetooth.com/specifications/gatt

  

一旦由收集器配置,通常会保留温度计   在使用之间断电,只会通告并允许收集器   当温度计有数据要发送时连接。在这种情况下,   温度计将进入GAP可连接模式并启动   在有数据发送给收集器时进行广告宣传。收藏者   通常会执行这样的GAP连接建立过程   它正在使用白名单扫描温度计。当一个   连接建立后,温度计发送一个或多个   收集者的指示或通知。当数据传输时   完成后,温度计通常会终止连接。

收藏家是你的应用程序,温度计是你的Blue Gecko。

对于UWP,这意味着:使用 AdvertisementWatcher 收听设备并获取设备。 获得服务和连接。 获取特征并设置 ClientCharacteristicConfigurationDescriptor 以进行通知。 实现特征值更改方法。

这意味着温度计值在通知中,或者您必须在通知到达后读取值。 如果没有通知,则无需阅读。

我在Github上做了一个例子来解释广告游戏和订阅通知:https://github.com/GrooverFromHolland/SimpleBleExample_by_Devicename

该示例是针对Visual Studio 2017编写的,但您可以阅读最重要的方法并获得一个想法 在Github上打开MainPage.xaml.cs。

你读取一个字节数组的温度,包含flagsfield(8bits),温度(4个字节)和可选的时间戳(7个字节),temptype(8bits) 来自https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.temperature_measurement.xml
您的appxmanifest必须包含:

 <Capabilities>
    <Capability Name="internetClient" />
    <DeviceCapability Name="bluetooth" />
</Capabilities>

在我的示例中,在以下位置添加以下行:

 characteristicFoundMilis = stopwatch.ElapsedMilliseconds;
                           Debug.WriteLine("Characteristic found in " +
                                          (characteristicFoundMilis - serviceFoundMilis) + " ms");

要添加的行:

 GattCharacteristicProperties properties = charac.CharacteristicProperties;

                           if (properties.HasFlag(GattCharacteristicProperties.Read))
                           {
                              Debug.WriteLine("This characteristic supports reading from it.");                              
                           }
                           if (properties.HasFlag(GattCharacteristicProperties.Write))
                           {
                              Debug.WriteLine("This characteristic supports writing to it.");                              
                           }
                           if (properties.HasFlag(GattCharacteristicProperties.Notify))
                           {
                              Debug.WriteLine("This characteristic supports subscribing to notifications.");
                           }
                           if (properties.HasFlag(GattCharacteristicProperties.Indicate))
                           {
                              Debug.WriteLine("This characteristic supports subscribing to Indication");
                           }