在 Android 上持续检测 NFC 标签 [英] Continuously detect an NFC tag on Android

查看:38
本文介绍了在 Android 上持续检测 NFC 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的阅读和实验,我发现 Android 设备使用某种方法来防止重复读取标签(如此处所述).这对节能很有好处,但限制了该技术的可能应用.

From my reading and experimentation I have found that Android devices use some method to prevent duplicate reading of a tag (as discussed here). This is great for power conservation however limits the possible application of this technology.

例如我面临的问题是,我有一个内容不断更新的被动标签.一旦标签被读取,手机就会停止检测标签,直到它被移除,在此期间,如果不将其从字段中移除并重新点击,您将无法继续读取标签中的更新内容.此外,在标签最初被读取后,磁场要么被关闭至极弱以节省电力.因此,您不能持续为无源设备供电.

For example the problem I face is, I have a passive tag where the content is continuously updated. Once the tag has been read the phone stops detecting tags until it is removed, during this time you cannot continue to read the updated content in the tag without removing it from the field and re-tapping. Further more the magnetic field is either powered down of extremely weak to save power after the tag has been initially read. As such you cannot continuously power passive devices.

有没有办法强制 Android 设备持续供电并读取更新的被动标签?

Is there a way to force an Android device to continuously power and read an updating passive tag?

注意:我不介意这是否涉及对 Android 设备进行 root 以实现对硬件的直接控制.

NB: I do not mind if it involves rooting the Android device in order to achieve direct control of the hardware.

推荐答案

您在问题中陈述的内容对于 Android NFC 设备根本不正确.一旦检测到标签(= 响应所有强制性命令的有效标签,并可能被分派到应用程序),NFC 阅读器将持续为标签供电(HF 载波保持开启)并与其交换一些命令.在该保活阶段(存在性检查")交换的命令取决于标签类型、Android 版本和 Android NFC 堆栈实现.这通常是

What you state in your question is simply not true for Android NFC devices. Once a tag has been detected (= valid tag that responsed to all mandatory commands and could potentially be dispatched to an app), the NFC reader will continuously power the tag (HF carrier is kept on) and exchange some commands with it. The commands that are exchanged during that keep-alive phase ("presence check") depend on the tag type, the Android version and the Android NFC stack implementation. This is typically either

  1. 重复停用和重新激活循环,​​
  2. 反复读取某个内存区域,或
  3. 其他一些乒乓命令序列

这允许 NFC 堆栈查明标签是否仍然响应.仅当存在检查失败时,Android 才会关闭 NFC 阅读器的 HF 载波,并以完整轮询序列(测试所有支持的标签技术)或感应阶段(短 HF 载波脉冲到检测表明可能存在标签的失谐).

that allows the NFC stack to find out if the tag is still responsive. Only if the presence check fails, Android will switch off the HF carrier of the NFC reader and will re-start with either a full polling sequence (testing for all kinds of supported tag technologies) or with a sensing phase (short HF carrier pulses to detect detuning that indicates the potential presence of a tag).

因此,如果您的代码行为正常并且您的用户设法将标签附加到 Android 设备的时间更长,那么没有什么可以阻止您从同一标签读取新数据(同时持续附加)多次.只要您想访问标签和标签读取活动,您只需确保保留标签句柄(Tag 对象,甚至是从该标签句柄实例化的特定标签技术对象)的应用程序需要持续保持在前台.

Thus, if your tag behaves properly and your users manage to keep a tag attached to the Android device for a longer period of time, there is nothing that would prevent you from reading new data from the same tag (while continuously attached) multiple times. You just need to make sure that you keep the tag handle (Tag object or even a specific tag technology object instantiated from that tag handle) for as long as you want to access the tag and the tag reading activity of your app needs to stay continuously in the foreground.

例如,您可以执行类似的操作来从标签中读取持续更新的 NDEF 消息(请注意,您最好使用 AsyncTask (或类似的)而不是简单地生成线程并且您可能想要实现一些机制来中断线程):

You could, for instance, do something like this to read a continuously updated NDEF message from a tag (note that you might better want to use an AsyncTask (or similar) instead of simply spawning that thread AND you might want to implement some mechanism to interrupt the thread):

new Thread(new Runnable() {
    public void run() {
        Ndef ndef = Ndef.get(tag);

        try {
            while (true) {
                try {
                    Thread.sleep(30000);

                    ndef.connect();
                    NdefMessage msg = ndef.getNdefMessage();

                    // TODO: do something

                } catch (IOException e) {
                    // if the tag is gone we might want to end the thread:
                    break;
                } finally {
                    try {
                        ndef.close();
                    } catch (Exception e) {}
                }
            }
        } catch (InterruptedException e) {
        }
    }
}).start();

这篇关于在 Android 上持续检测 NFC 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆