Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

android - How to detect NFC tag was removed

I am new at NFC technology. I am trying to read and write Mifare Classic 4K tags. I succeeded reading and writing. I can detect the tag on onNewIntent action.

My problem is that I could't detect when an NFC tag was removed (similar to onNewIntent when detecting a tag).

How can I solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no event that notifies your app if a tag is removed from the reader. A typical approach to detect removal is to actively poll the tag (e.g. by reading an NDEF message or a data block) until you receive IOException indicating that communication with the tag was lost.


UPDATE

As of Android API level 24 this is no longer true. There is now a method

NfcAdapter.ignore(tag, debounceMs, tagRemovedListener, handler);

that can be used to register a callback for getting notifications upon tag removal. This method may have some issue though, so carefully read the documentation on side-effects/corner cases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...