How to detect NFC tag was removed

笑着哭i 提交于 2019-11-28 09:49:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36126635/how-to-detect-nfc-tag-was-removed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!