C#从BT LE设备转换字节数组

时间:2018-12-04 17:23:33

标签: c# uwp iot btle

我正在使用Nordic Thingy:52在UWP应用程序中记录环境数据,并按照Windows Universal Sample应用程序中的示例连接到BT LE设备。

到目前为止,我已经能够连接到设备以检索服务和特征信息,但是当从传感器接收实际数据时,我无法设法将字节数组转换为可用数据。

async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
    // An Indicate or Notify reported that the value has changed.
    var reader = DataReader.FromBuffer(args.CharacteristicValue);
    byte[] input = new byte[reader.UnconsumedBufferLength];
    reader.ReadBytes(input);
}

在检查字节数组的内容时,您可以看到已经收到了一些内容,但是在了解如何将该数组转换为有用数据时,我陷入了困境。

Code to read the byte array

Data specification for data sent by the device

1 个答案:

答案 0 :(得分:0)

document我们可以看到压力数据的定义:

enter image description here

5个字节包含一个整数部分的int32和一个十进制部分的uint8。 Uint是hPa。

您将得到一个这样的字符串:

        Int32 pressureInteger = BitConverter.ToInt32(input, 0); //252-3-0-0
        string pressureString = pressureInteger.ToString() + "." + input[4].ToString() + "hPa";

字符串将为“ 1020.28hPa”

更多参考文献“ BitConverter Class”,并注意小尾数/大尾数。