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
89 stars 52 forks source link

NfcManager.instance.startSession doesn't work in Background #81

Open ghost opened 11 months ago

ghost commented 11 months ago

Describe the bug I would like to make an app that periodically reads NFC tags in the background. When it goes into background execution the NfcManager part is not executed.

To Reproduce This is the part of code that we call when a button is pressed. It works when the app is on the screen, when we hide the screen, the part of NfcManager doesn't run. No error appears.

if (hasPermissions) { final backgroundExecution = await FlutterBackground.enableBackgroundExecution(); ValueNotifier resultBro = ValueNotifier(null); if (backgroundExecution) { print("SONO IN back, BRO"); timer = Timer.periodic(Duration(seconds: 5), (Timer t) async { Duration tagtimer = Duration(seconds: 3); resultBro.value = null;

    Timer(tagtimer, () async {
      if (resultBro.value == null) {
        await NfcManager.instance.stopSession();
      }
    });
    NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async {
      resultBro.value = tag.data;
      print("sto in on discover bro");
      print(resultBro.value);
      await NfcManager.instance
          .stopSession()
          .whenComplete(() => print("so completo bro"));
    });
    print("sto fuori bro");
    print(resultBro.value);
  });
}

}

Smartphone: