Noor Nawaz
Noor Nawaz

Reputation: 2225

Nfc tag detected/lost listener

I have been working on NFC MifareClassic card. I have read and written data on card. I want to show a notification to user when tag is detected that what I have,

    @Override
    protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    // 1) Parse the intent and get the action that triggered this intent
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
        //  3) Get an instance of the TAG from the NfcAdapter

        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);


        AppController.getInstance().setTag(tagFromIntent);

    }
}

but when tag is move away that is what I need. I want to show user that tag is no longer available. Is there any listener for it?

Upvotes: 2

Views: 3507

Answers (2)

Egis
Egis

Reputation: 5141

NfcAdapter has this ignore method. It was added in API 24. I haven't tested it yet but is seems to offer what you need. It lets you set OnTagRemovedListener for concrete Tag.

Upvotes: 3

Shane
Shane

Reputation: 114

It is not possible to detect when the tag is no longer available, because the tags don't operate via proximity to the tag reader, they work by reading the tag when passed over the reader.

You could of course implement your own detection of when the tag goes out of range by polling the tag over a period of time. And if it goes out of range you could trigger your own event by using a delegate.

Upvotes: 3

Related Questions