简体   繁体   English

如何检测 NFC 标签被移除

[英]How to detect NFC tag was removed

I am new at NFC technology.我是 NFC 技术的新手。 I am trying to read and write Mifare Classic 4K tags.我正在尝试读写 Mifare Classic 4K 标签。 I succeeded reading and writing.我成功地阅读和写作。 I can detect the tag on onNewIntent action.我可以检测onNewIntent操作上的标签。

My problem is that I could't detect when an NFC tag was removed (similar to onNewIntent when detecting a tag).我的问题是我无法检测到 NFC 标签何时被移除(类似于检测标签时的onNewIntent )。

How can I solve this problem?我怎么解决这个问题?

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 (eg by reading an NDEF message or a data block) until you receive IOException indicating that communication with the tag was lost.检测移除的典型方法是主动轮询标签(例如,通过读取 NDEF 消息或数据块),直到您收到 IOException 指示与标签的通信丢失。


UPDATE更新

As of Android API level 24 this is no longer true.从 Android API 级别 24 开始,这不再是正确的。 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.不过,这种方法可能存在一些问题,因此请仔细阅读有关副作用/角落案例的文档

You can check by calling isoDep.isConnected();您可以通过调用 isoDep.isConnected(); 来检查; in IsoDep在 IsoDep

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM