Flutter how to discovering bluetooth devices in background (flutter_bluetooth_serial) Flutter how to discovering bluetooth devices in background (flutter_bluetooth_serial) dart dart

Flutter how to discovering bluetooth devices in background (flutter_bluetooth_serial)


To run tasks in the background, you may want to look into using Isolate.

Isolate? isolate;@overridevoid initState() {  /// Start background task  _asyncInit();  super.initState();}_asyncInit() async {  final ReceivePort receivePort = ReceivePort();  isolate = await Isolate.spawn(_isolateEntry, receivePort.sendPort);  receivePort.listen((dynamic data) {    if (data is SendPort) {      if (mounted) {        data.send({          /// Map data using key-value pair          /// i.e. 'key' : String        });      }    } else {      if (mounted) {        setState(() {          /// Update data here as needed        });      }    }  });}static _isolateEntry(dynamic d) async {  final ReceivePort receivePort = ReceivePort();  d.send(receivePort.sendPort);  /// config contains the key-value pair from _asyncInit()  final config = await receivePort.first;   /// send bluetooth data you received  d.send(...);}@overridevoid dispose() {  /// Determine when to terminate the Isolate  if (isolate != null) {    isolate.kill();  }  super.dispose();}

As for BLE support on Flutter, you can also look into using flutter_blue