简体   繁体   中英

Get BLE advertisement data - Android scanRecord to Flutter Blue ScanResult

I have a requirement to rebuild an android application in Flutter which will listen to the BLE advertisement packets and interpret data.

The existing protocol can interpret Android's scanRecord, as it is a bytearray.

However, in flutter I am using flutter blue plugin, which gives the BLE ScanResult as a map. But I couldn't find a way to interpret the advertisement packets as it looks like some integer list. Example: {256:[0,0,0,16,1,57,33,18,0,0,154,10,0]}

Could anyone please help me on how to convert the Flutter blue's ScanResult into Android's bytearray similar to the scanRecord format, so that I can interpret the data using the existing protocol.

Thanks in advance.

Flutter Blue ScanResult embeds all what you need in an easily interpretable form:

class ScanResult {
  final BluetoothDevice device;
  final AdvertisementData advertisementData;
  final int rssi;
}

You can find the BLE ID, Bluetooth name and some other values in the BluetoothDevice object. AdvertisementData provides even more details regarding the transmission power level, if the device is connectable, what BLE GATT service UUIDs the device implements ( serviceUuids ) and what service data does it have related to those UUIDs ( serviceData ) among other things.

class AdvertisementData {
  final String localName;
  final int? txPowerLevel;
  final bool connectable;
  final Map<int, List<int>> manufacturerData;
  final Map<String, List<int>> serviceData;
  final List<String> serviceUuids;
}

This is there so you don't have to worry about endinanness and all of the decoding. It's done under the hood by Protobuf objects. The example application demonstrates how to format these data nicely, like hexa output of the UUIDs.

These resulting Dart classes created by Protobuf protocol stack are not geared to access the raw byte data, so that's not available through the API. So I guess you probably want to express the logic at a higher level you have in your Android code and actually think about what it does. You might be migrating an Android to Flutter? Even Android provides higher level functions in ScanRecord . You will end up with a better maintainable and more compatible code if you rely on the decoded data provided to you. Unless you are dealing with some extremely proprietary thing, but even such thingie has to adhere the BLE protocols, so you should end up in a better place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM