JulianAssmann / flutter_background

A flutter plugin to keep apps running in the background via foreground services. Android only.
https://pub.dev/packages/flutter_background
MIT License
86 stars 46 forks source link

Error : WakeLock finalized while still held #35

Open GaelleJoubert opened 3 years ago

GaelleJoubert commented 3 years ago

Hello, I'm using this plugging to run a function (bluetooth scan) in background, even if the app is not "open". As a way to try this plugging to see if it fit my need, I code a very easy app where when I tap a button, it launch the bluetooth scan as a "forground service", and when the same button is tap again, it stop the scan and the foreground service.

A few second after closing the foreground service, I have the following warning on my console :

"E/PowerManager(14614): WakeLock finalized while still held: FlutterBackgroundPlugin:Wakelock"

Even though the app seems to work fine. When I tap pthe button I can see my bluetooth scan starting (it prints the snaned devices), and when I tap it again, I can see the foreground service stoping (notification disapearing), and the bluetooth scan stopping as well.

Have you aver have this error ? Can i just igniore it, or did I do someting wrong ? I pasted bellow the way I'm using the plugging :


//Android Config
final androidConfig = FlutterBackgroundAndroidConfig(
  notificationTitle: "My title",
  notificationText: "Bluetooth Scan in process",
  notificationImportance: AndroidNotificationImportance.Default,
  notificationIcon: AndroidResource(name: 'icon_foregroundService.png', defType: 'drawable'), 
);

//Function To start the foreground Service
void StartFGS() async {
  bool hasPermissions = await FlutterBackground.hasPermissions;
  bool success = await FlutterBackground.initialize(androidConfig: androidConfig);
  await FlutterBackground.enableBackgroundExecution();
  startBluetoothScan();
}

//Function to Stop The foreground service
void stopForegroundService() async{
  stopScanForDevice();
  //await FlutterBackground.initialize();
  await FlutterBackground.disableBackgroundExecution();
}

//The following function is in another file, and is called when the button is tapped :
void _toggleForegroundServiceOnOff() async {

    if (fgsIsRunning) {
      stopForegroundService();
      setState(() {
        fgsIsRunning = false;
      });
    }
    else {
      maybeStartFGS();
      setState(() {
        fgsIsRunning = true;
      });
    }
    }

Smartphone:

JulianAssmann commented 2 years ago

Strange. I'm explicitly setting the WakeLock not to be referenced counted: https://github.com/JulianAssmann/flutter_background/blob/8f2dec361990d0e119701fd191ad641dc1d664f7/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt#L77 So one call to release() should release the WackeLock no matter how many acquire() calls there were prior to it.

This bug should not be problem for your app though.

JulianAssmann commented 2 years ago

Which version did you use and is the problem fixed in the latest version?

blackbusters commented 2 years ago

I'm still experiencing this warning with latest version, and in real samsung devices, it used 100% cpu and freeze hang the app, was that had anything to do with this?