简体   繁体   中英

Receive data in background (flutter_reactive_ble)

I have an arduino nano 33 ble that sends data from sensors every second or so. Right now I also have an app, written with Flutter and flutter_reactive_ble that receives the data. The problem is that I need to continue receiving data in background, but I am too inexperienced with Dart/Flutter, so I have trouble to do it. For now I am using am implementation found on https://github.com/ubiqueIoT/flutter-reactive-ble-example I am using the subScribeToCharacteristic method. My understanding is that it checks for new data only when there is something on the screen (using StreamBuilder<List>). Is there a way to perform some basic math operations on the data in background and to dump it all to an array? I read about isolates and some other interesting techniques, but I was wondering whether there is a simpler solution, because those look a bit too difficult. I saw that apparently if I don't close my app completely (iOS) there is a way for it to continue to receive data for some time like 15-30 minutes, which is enough for my purpose, but there was no code/examples/etc.

I tried to put Stream out of StreamBuilder and to receive data just in the main body of Widget build, but nothing worked, I stopped receiving any data.

Thanks in advance!

here is a good starting point for you just use this plugin

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.).
    },
           );

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