How do I know if the NFC tag is now in the range from Android?

As I know, when an Android phone receives an NFC tag, it sends an event (intent NDEF_DISCOVERED), but Android does not seem to care if this tag remains in place. My solution is to lock the screen and then unlock it. If the tag is still there, I can read it again. This is obviously stupid. Is there a smarter way to do this?

+4
source share
2 answers

As part of the NFC intention received by your activity, you will also receive a tag tag (object Tag) in addition to:

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

. , NDEF, :

Ndef ndefTag = Ndef.get(tag);

connect():

ndefTag.connect();

, "" , :

try {
    ndefTag.getNdefMessage();
} catch (IOException e) {
    // if you receive an IOException, contact to the tag has been lost
}

, , .

+3

, , isConnected(). :

try {
    ndef.connect();
    while(ndef.isConnected()){

    //Your code here

    }
} catch (IOException e) {
    //Error
}
+1

Source: https://habr.com/ru/post/1539092/


All Articles