0

I am making an app to read BLE beacons using flutter and I am using flutter_blue when I scan 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 ..>

when I scan the device using readily available BLE app like BLE Hero I got the manufacturer data as 00 01 08 1e ac a2 00 37 8f 35 05 00 f0 08 00 00 4b 00

i have no idea how can i convert

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

To

00 01 08 1e ac a2 00 37 8f 35 05 00 f0 08 00 00 4b 00

ramashish tomar
  • 815
  • 11
  • 35

1 Answers1

5

As it says in the flutter_blue documentation, manufactureData is returned as a Map object and you appear to be asking how to get the value for the Map key/value pair.

This is covered in the following tutorial: https://www.tutorialspoint.com/dart_programming/dart_programming_map.htm

The general case is map_name[key]. In your example the key is 256 and the map_name is KCBAdvDataManufacturerData so to get the list, it would be KCBAdvDataManufacturerData[256]

This looks linked to your previous question, so to put that in a more complete example:

import 'dart:typed_data';

var bleData = {256:[0,0,0,16,1,57,33,18,0,0,154,10,0,0,94,0]};
main() {
  parseManufacturerData(bleData);
}
parseManufacturerData(data) { 
  var manufacturerData = Uint8List.fromList(data[256]);
  var pressure = ByteData.sublistView(manufacturerData, 6, 10);
  var temperature = ByteData.sublistView(manufacturerData, 10, 14);
  var battery = ByteData.sublistView(manufacturerData, 14, 15);
  print("Pressure: ${pressure.getUint32(0, Endian.little)/100} psi");
  print("Temperature: ${temperature.getUint32(0, Endian.little)/100} \u{00B0}C");
  print("Battery: ${battery.getUint8(0)} %");
} 

When I run this at https://dart.dev/#try-dart it gives:

Pressure: 46.41 psi
Temperature: 27.14 °C
Battery: 94 %

I have no way of testing the following code, but this how I would try modifying the Scan for devices example at https://pub.dev/documentation/flutter_blue/latest/index.html:

// Start scanning
flutterBlue.startScan(timeout: Duration(seconds: 4));

// Listen to scan results
var subscription = flutterBlue.scanResults.listen((results) {
    // do something with scan results
    for (ScanResult r in results) {
        print('${r.advertisementData.manufacturerData}');
        // Pass it to our previous function
        parseManufacturerData(r.advertisementData.manufacturerData)
    }
});

// Stop scanning
flutterBlue.stopScan();

I am expecting the key value in the advertisementData.manufacturerData to be the Company ID. The thing that is confusing me, is the value of decimal 256 in your example is for TomTom International BV which might not be the device sending the pressure, temperature, and battery values. If that is the case, then you will need some logic in the code to only call parseManufacturerData if it is the device of interest.

ukBaz
  • 6,985
  • 2
  • 8
  • 31