Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Bluetooth Background Mode iOS - keep app alive, listening to BLE characteristic notifications

actually I'm pretty new to Flutter and mobile app development. Currently I'm working on an app in Flutter, receiving data from a sensor, connected via BLE (flutter_blue_plus) to the mobile device. Now I have problems with iOS, that keeps killing my app, as soon it goes in background mode (lockscreen or minimization). I tried bunch of different approaches (flutter_background_service, etc.), but none of them worked so far. With flutter_background_service, the app is getting killed approx. 20 seconds after entering background mode. I can't find clear informations online, if it is even possible to keep the app alive on iOS, when using Flutter. The statements i found on this are very contradictory.

Thank you very much nad best reagrds

I tried bunch of different approaches (flutter_background_services, etc.), but none of them worked so far. With flutter_background_service, the app is getting killed approx. 20 seconds after entering background mode.

like image 293
Lucas Heitele Avatar asked Oct 16 '25 01:10

Lucas Heitele


1 Answers

Nice to see new flutter devs working with BLE ;)

Info.plist changes

Try adding this change to your Info.plist file (found in ios/Runner/Info.plist). It might do the trick.

<key>NSBluetoothAlwaysUsageDescription</key>
<string>The app uses bluetooth to find, connect and transfer data between different devices</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>The app uses bluetooth to find, connect and transfer data between different devices</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
</array>

Note: after having added this it is wise to run flutter clean and then fully restart and reinstall the app you have on your device, because this touches the underlying config.

Source: this comment on the GitHub repo for a BLE library. It is a different library than you use but it gives a clue into how this file works.

If you want to dive deeper into iOS and Bluetooth here is their official (and complicated) documentation on this.

Alternative solution

If you want evade sleeping in general: consider adding a wakelock. See the wakelock package.

like image 123
Robin Avatar answered Oct 18 '25 14:10

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!