0

I am using a Nordic Thingy:52 to record environmental data in a UWP app and have followed the example in the Windows Universal Sample apps to connect to BT LE devices.

So far I have been able to connect to the device to retrieve service and characteristic information but when receiving the actual data from the sensors I can't manage to convert the byte array into usable data.

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

When checking the contents of the byte array you can see that something has been received but I'm stuck when it comes to knowing how to convert this array to useful data.

Code to read the byte array

Data specification for data sent by the device

Dalton Cézane
  • 3,672
  • 2
  • 35
  • 60
  • What do you mean by "useful data"? String? Another type? If you want to use the data as `String`, you can try something like this: `var string_data = reader.ReadString(args.CharacteristicValue.Length);` . – Dalton Cézane Dec 04 '18 at 18:42
  • An example from the documentation is a pressure characteristic: 5 bytes Pressure in hPa • int32_t - integer • uint8_t - decimal – zerozeroforty Dec 04 '18 at 21:04
  • Having tried the example you gave I received the error 'No mapping for the Unicode character exists in the target multi-byte code page.' – zerozeroforty Dec 04 '18 at 21:31

1 Answers1

0

From the document we can see the definition of pressure data:

enter image description here

5 bytes contains one int32 for integer part and one uint8 for decimal part. Uint is hPa.

You get a string like this:

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

The string will be "1020.28hPa"

More reference "BitConverter Class" and note little-endian/big-endian.

Rita Han
  • 9,574
  • 1
  • 11
  • 24