0

I am developing an app which uses an embedded system connected to through Bluetooth to an embedded device and I want the app to run a function when the device sends a signal. The issue is that I cannot keep the app open all the time and thus need the app to keep running in the background and keep listening for the signal send by the embedded device so that it can run the function I want. I have seen the following questions :

How to run flutter app in background continually?

How can I make my flutter app run in background without stopping?

How to create a service in Flutter to make an app to run always in background?

and many more and so far nothing seems to work.

I have written this code for now :

Future<void> _run_app_in_background() async {
    final config = FlutterBackgroundAndroidConfig(
      notificationTitle: 'MEDICA',
      notificationText:
      'MEDICA is  running in the background',
      notificationIcon: AndroidResource(name: 'background_icon'),
      notificationImportance: AndroidNotificationImportance.Default,
    );

    var hasPermissions = await FlutterBackground.hasPermissions;
    hasPermissions = await FlutterBackground.initialize(androidConfig: config);
    final backgroundExecution =
    await FlutterBackground.enableBackgroundExecution();
}

but it seems to be doing nothing and the same with other packages.

So how can I make my app run in background to keep listening for the Bluetooth data sent by device.

Edit:I want this to run on both Android and iOS and if possible maybe someone can suggest me another framework apart from Flutter (in comments maybe) and I can search the internet for the same.

Hrishabh Nayal
  • 135
  • 1
  • 9
  • PS : I am new to flutter , so I would appreciate if the answer can be kept at a basic/elementary level. – Hrishabh Nayal Aug 14 '22 at 19:44
  • [https://docs.flutter.dev/development/packages-and-plugins/background-processes](https://docs.flutter.dev/development/packages-and-plugins/background-processes) – Chance Aug 14 '22 at 20:04
  • 1) It may help to review github issues for any plugin - https://github.com/JulianAssmann/flutter_background/issues and 2) On Android be aware of OEM background processing restrictions: https://dontkillmyapp.com/ – Morrison Chang Aug 14 '22 at 20:06
  • @Chance I can't understand the article you linked , it seems to too advanced for my technical knowledge. However I have tried something it tells me to do and still nothing – Hrishabh Nayal Aug 14 '22 at 20:25
  • @MorrisonChang I saw the github link and saw 2 issues which seem to be related to me (maybe?) https://github.com/JulianAssmann/flutter_background/issues/57 https://github.com/JulianAssmann/flutter_background/issues/8 but I am still lost on how to solve these. Or maybe use some other package altogether? – Hrishabh Nayal Aug 14 '22 at 20:27
  • Try with this package [link](https://pub.dev/packages/workmanager) – Pothio Aug 14 '22 at 20:58
  • The answer, on iOS (and, I suspect, on Android), is not to try to keep your app running. Rather, you need to use the facilities provided by the operating system for handling Bluetooth in the background. You need to add Bluetooth background mode capability to your iOS Xcode project and then activate notifications on the relevant characteristic. iOS will then deliver these data updates to your app in the background. – Paulw11 Aug 14 '22 at 21:28
  • @Paulw11 that seems like what would help me, are you aware of any articles/guides that would help me for the same? – Hrishabh Nayal Aug 15 '22 at 08:41
  • @Pothio I have tried that package but it doesn't seem to work – Hrishabh Nayal Aug 15 '22 at 08:43
  • I can't help you with Flutter. You may be able to find a flutter plug-in that can help you, but often with low level hardware type functionality you have to write platform specific native code. – Paulw11 Aug 15 '22 at 10:58

1 Answers1

0

@HrishabhNayal Hello bro Try this way!

https://medium.com/dlt-labs-publication/flutter-run-code-in-the-background-461b4d6c635b

Harshil
  • 162
  • 11
  • That article uses `background_fetch` which seems to execute the task only once in 15 mins at best. I cannot afford that as I need it as low as 1 sec. – Hrishabh Nayal Aug 15 '22 at 08:44