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

查看:603
本文介绍了在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设备以实现对硬件的直接控制.

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天全站免登陆