[英]Receive data in background (flutter_reactive_ble)
我有一个 arduino nano 33 ble,大约每秒从传感器发送数据。 现在我还有一个应用程序,用 Flutter 和接收数据的 flutter_reactive_ble 编写。 问题是我需要在后台继续接收数据,但是我对Dart/Flutter的经验太少,所以很难做到。 现在我正在使用在https://github.com/ubiqueIoT/flutter-reactive-ble-example上找到的实现我正在使用 subScribeToCharacteristic 方法。 我的理解是它仅在屏幕上有内容时才检查新数据(使用 StreamBuilder<List>)。 有没有办法在后台对数据执行一些基本的数学运算并将其全部转储到数组中? 我阅读了 isolates 和其他一些有趣的技术,但我想知道是否有更简单的解决方案,因为这些看起来有点太难了。 我看到显然如果我不完全关闭我的应用程序(iOS),有一种方法可以让它继续接收数据一段时间,比如 15-30 分钟,这足以满足我的目的,但是没有代码/示例/ETC。
我试图将 Stream 从 StreamBuilder 中取出并仅在 Widget 构建的主体中接收数据,但没有任何效果,我停止接收任何数据。
提前致谢!
这是一个很好的起点,你只需使用这个插件
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart'
as bg;
// Initialize the plugin.
bg.BackgroundGeolocation.ready(bg.Config(
desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
distanceFilter: 10.0,
stopOnTerminate: false,
startOnBoot: true,
));
// Start tracking location in the background.
bg.BackgroundGeolocation.start();
// Subscribe to the BLE device's characteristic to receive data.
bg.BackgroundGeolocation.subscribeToCharacteristic(
'DEVICE_ID',
'CHARACTERISTIC_ID',
(bg.CharacteristicEvent event) {
// Do some basic math operations on the data.
double data = event.value;
double result = data * 2 + 3;
// Dump the data to an array.
List<double> dataArray = [result];
// Do something with the data (e.g., save it to a database, send it to a server, etc.).
},
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.