简体   繁体   English

获取 BLE 广告数据 - Android scanRecord 到 Flutter Blue ScanResult

[英]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.我需要在 Flutter 中重建一个 android 应用程序,它将侦听 BLE 广告数据包并解释数据。

The existing protocol can interpret Android's scanRecord, as it is a bytearray.现有协议可以解释Android的scanRecord,因为它是一个字节数组。

However, in flutter I am using flutter blue plugin, which gives the BLE ScanResult as a map.但是,在颤振中,我使用了颤振蓝色插件,它将 BLE ScanResult 作为地图。 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]}示例:{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.任何人都可以帮助我如何将 Flutter blue 的 ScanResult 转换为类似于 scanRecord 格式的 Android 字节数组,以便我可以使用现有协议解释数据。

Thanks in advance.提前致谢。

Flutter Blue ScanResult embeds all what you need in an easily interpretable form: Flutter Blue ScanResult以易于解释的形式嵌入了您需要的所有内容:

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.您可以在BluetoothDevice对象中找到 BLE ID、蓝牙名称和一些其他值。 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. AdvertisementData提供有关传输功率水平的更多详细信息,如果设备可连接,设备实现哪些 BLE GATT 服务 UUID ( serviceUuids ) 以及它与这些 UUID ( serviceData ) 相关的服务数据有哪些。

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.这是在那里,所以你不必担心endinanness 和所有的解码。 It's done under the hood by Protobuf objects.它是由 Protobuf 对象在幕后完成的。 The example application demonstrates how to format these data nicely, like hexa output of the UUIDs.示例应用程序演示了如何很好地格式化这些数据,例如 UUID 的六进制输出。

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.这些由 Protobuf 协议栈创建的 Dart 类不适合访问原始字节数据,因此无法通过 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.所以我猜你可能想在你的 Android 代码中表达更高层次的逻辑,并实际考虑它的作用。 You might be migrating an Android to Flutter?您可能正在将 Android 迁移到 Flutter? Even Android provides higher level functions in ScanRecord .甚至 Android 在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.除非你正在处理一些非常专有的东西,但即使是这样的东西也必须遵守 BLE 协议,所以你应该在一个更好的地方结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Android中不同数据包的BLE广告 - BLE advertisement with different data packets in android 动态更改 BLE 广告数据 Android - Dynamically Changing BLE Advertisement Data Android Android中的BLE广告 - BLE Advertisement in Android 在使用不同版本的Android设备扫描时,为同一个BLE设备获取不同的byte [] scanRecord数据 - Getting different byte[] scanRecord data for same BLE device while scanning with different versions of android device Android BLE:将 ScanResult timestampNanos 转换为 System nanoTime - Android BLE: Convert ScanResult timestampNanos to System nanoTime Android BLE 在不可连接模式下接收广告数据 - Android BLE receive advertisement data in none connectable mode 如何从ble设备获取正确的(解码)制造商广告数据? - How to get proper(decode) manufacturer advertisement data from ble devices? Android BLE BluetoothAdapter.LeScanCallback scanRecord长度模糊 - Android BLE BluetoothAdapter.LeScanCallback scanRecord length ambiguity NullPointerException: .... 空对象引用上的虚拟方法 &#39;android.bluetooth.le.ScanRecord android.bluetooth.le.ScanResult.getScanRecord()&#39; - NullPointerException: .... virtual method 'android.bluetooth.le.ScanRecord android.bluetooth.le.ScanResult.getScanRecord()' on a null object reference Android 21 API中的BLE广告模式 - BLE Advertisement mode in Android 21 API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM