nfc_in_flutter
nfc_in_flutter copied to clipboard
Prevent read/write displaying system NFC "New tag collected" on Android
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.
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
@vilvic can you please tell me the solution to prevent "New Tag Collected" on Android?
@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)