Search code examples
androidflutternfc

How can i Prevent the default "New Tag Scanned" pop-up using the 'nfc_manager' Plugin in Flutter on Android Devices?


I am having a problem with reading NFC chips on Android phones. The problem is that when I scan one in my Flutter app, it opens a new window and it says "New Tag Scanned".

This is annoying because it leaves the app and I have to go back to it. I have tried searching for it but every if any solutions are for the "nfc_in_flutter" plugin, but I am using the "nfc_manager" one. I can't use "nfc_in_flutter" because it is outdated and incompatible with Dart 3.

Here are some sources I found:
https://github.com/semlette/nfc_in_flutter/issues/50

nfc_in_flutter: How to prevent "New Tag Scanned" in android version?

When app receives NFC data it opens "New Tag collected" // Best I found, but still doesn't work

Does anyone know how I can keep this window from opening?


Solution

  • I recently worked with the same package of Flutter NFC Manager and had the same problem:

    Android system's NFC handler is interfering and opening the new window on tag scans, rather than the Flutter app handling it directly.

    From what I've seen, basically, you need to have the correct flutter code to listen for the NFC tag, otherwise, the Android one will intercept the tag.

    So, in your initState if you don't call:

    NfcManager.instance.startSession(
      onDiscovered: (NfcTag tag) async {
        // Do something with an NfcTag instance.
      },
    );
    
    

    It will open the Android one since there's no code for handling the card.

    So, the solution that I have is to always listen for an NFC tag, with:

    @override
    void initState() {
      super.initState();
      NfcManager.instance.startSession(
        onDiscovered: (NfcTag tag) async {
          // Even if you're not doing anything here, it will still prevent the Android NFC handler from triggering.
        },
      );
    }
    

    whether I need to read the card or not. And since we're using the flutter nfc manager, it will override the Android manager.

    Also, don't forget to dispose:

    @override
    void dispose() {
      NfcManager.instance.stopSession();
      super.dispose();
    }