1

I am new to flutter and I am working on an app that reads data from a BLE beacon. I have scanned the device and got the manufacturer data as {256:[0,0,0,16,1,57,33,18,0,0,154,10,0,0,94,0]}

the device manufacturer told me to device data like :

KCBAdvDataManufacturerData = <.. .. .. .. .. .. .. .. be 21 01 00 50 08 00 00 5e ..>

The UUID - kCBAdvDataManufacturerData packet contains the sensor data as shown below:

Byte index 8 – 11 = Pressure 32bit value

Byte index 12 – 15 = Temperature 32bit value

Byte index 16 = Battery level in percentage

I am totally not getting any idea that in Dart how can I achieve it from

{256:[0,0,0,16,1,57,33,18,0,0,154,10,0,0,94,0]}

to

Byte index 8 – 11 = Pressure 32bit value

Byte index 12 – 15 = Temperature 32bit value

Byte index 16 = Battery level in percentage

and then in a human-understandable form here temperature is in C pressure in PSI and battery is in %.

ramashish tomar
  • 815
  • 11
  • 35

1 Answers1

1

There is a list of bytes so you can get sublists from that list with https://api.dart.dev/stable/2.9.1/dart-core/List/sublist.html

That sublist can be converted from 4 bytes into a signed integer for the pressure and temperature values:

Convert 4 byte into a signed integer

I am not sure the index values you have been given look quite right. I am assuming the data is in little endian format so my assumption on the data is:

Pressure = [33,18,0,0] = 4641 (Are you expecting a value of about 46.41psi?)

Temperature = [154,10,0,0] = 2714 (Are you expecting a value of about 27.14c?)

Battery = [94] = 94 (Are you expecting a value of 94%?)

This might be done like the following:

import 'dart:typed_data';

var manufacturerData = Uint8List.fromList([0,0,0,16,1,57,33,18,0,0,154,10,0,0,94,0]);
var pressure = ByteData.sublistView(manufacturerData, 6, 10);
var temperature = ByteData.sublistView(manufacturerData, 10, 14);
var battery = ByteData.sublistView(manufacturerData, 14, 15);
main() {
  print(pressure.getUint32(0, Endian.little));
  print(temperature.getUint32(0, Endian.little));
  print(battery.getUint8(0));
}

Gives me the output:

4641
2714
94
ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • thank you @ukBaz but i am not getting any way to decode these substrings, how to do this [33,18,0,0] = 4641? – ramashish tomar Aug 25 '20 at 11:29
  • @ramashishtomar, did you follow the link to https://stackoverflow.com/questions/13322327/convert-4-byte-into-a-signed-integer ? – ukBaz Aug 25 '20 at 12:02
  • yes, but did not get the result as expected, i am getting error The method 'asByteArray' isn't defined for the type 'Int8List'. – ramashish tomar Aug 25 '20 at 12:07
  • Add code example which I have run at https://dart.dev/#try-dart – ukBaz Aug 25 '20 at 12:37
  • @ukBaz Hi, I have a simular problem. My manufacture data looks like {76:[2, 21, 226, 197, 109, 181, 223, 251, 72, 210, 176, 96, 208, 245, 167, 16, 150, 224, 0, 0, 0, 4, 232]} the 14, 15 indexes are for the battery but I cannot figure out why are 167 and 16. The output from the code you provided returns 167. I can scan the beacon with official app and it gives 100% battery and 3102 mV. Can you help me out with this? – kmignt Apr 08 '21 at 13:14
  • @kmignt, that looks like it is an iBeacon packet and so doesn't contain the battery level. See elsewhere on SO. e.g. https://stackoverflow.com/a/25675698/7721752 – ukBaz Apr 08 '21 at 13:50