semlette / nfc_in_flutter

Cross-platform flutter plugin for reading and writing NFC tags. Not maintained anymore - not looking for new maintainer, fork instead.
MIT License
120 stars 119 forks source link

Prevent read/write displaying system NFC "New tag collected" on Android #51

Open vilvic opened 4 years ago

vilvic commented 4 years ago

As soon as the following lines are called;

await NFC.writeNDEF(payload, once: true).first

and

await NFC.readNDEF(once: true).first;

The NFC is opened back up to the system which then causes an instant read of the tag. This then causes the "New tag collected" page to be displayed (pixel 3).

Even though we only want to read one tag we're having to stream reading the NFC. We then pause for a second before cancelling the stream. Similarly we are having to start reading for 1 second directly after writing to prevent the system "New tag collected" page from being displayed.

Is there a way to detect that the tag has been removed before passing control back to the phone.

vilvic commented 4 years ago

I've been playing around with the code and thought I had a simple solution. However, I didn't realise that before writing the tag was being read first.

I've forked your code and added the following branch

ghulamsabir5353 commented 3 years ago

@vilvic can you please tell me the solution to prevent "New Tag Collected" on Android?

vilvic commented 3 years ago

@ghulamsabir5353 you can look at the diff (https://github.com/vilvic/nfc_in_flutter/commit/e668f4c3f266a77cd0761b374690e1bc57e89747)

The problem seemed to be as soon as you write the tag it immediately tries to read the tag. You'll notice I added the following lines of code.

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N && !writeIgnore) {
                adapter.ignore(tag, ONE_SECOND, this, null);
            }

This signals that you are no longer interested in communicating with an NFC tag for as long as it remains in range. See the document here... https://developer.android.com/reference/android/nfc/NfcAdapter#ignore(android.nfc.Tag,%20int,%20android.nfc.NfcAdapter.OnTagRemovedListener,%20android.os.Handler)