androidtagsnfcenergy

Continuously detect an NFC tag on 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.

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

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


Solution

  • 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. repeated deactivation and re-activation cycles,
    2. repeatedly reading a certain memory area, or
    3. some other ping-pong command sequence

    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).

    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.

    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();