[Nexus S Themes and Apps] krohnjw:
So, you may have a few problems with this. Warning this is a bit long, but I've been down this road many times so some of this you may not have considered :)

There isn't a way in the Android API to "poll" for a tag really. The tag data is delivered via an intent when the tag is detected. You could grab this, connect to the tag and then poll isConnected(). However this leads into part two. Your activity would need to handle all incoming new NDEF_DISCOVERED or TAG_DISCOVERED intents and register the new UUID when a new tag is introduced (as there's no event broadcast when a tag is lost and it may be lost during your sleep between polls). This is really easy as you could just register a foreground dispatch for all NFC activity.

This will work well if you don't let the screen turn off, ever. Once the screen turns off though the NFC service turns off and the tag is lost as a result (no event is dispatched, but you can't read any new tag and a tag lost event is logged). At this point your polling loops fires a disconnect at a time when the tag may or may not be present, you don't know. You could very easily track screen state and re-scan once the device is back on to see if the tag is still there.

However, this introduces a problem as the NFC service isn't re-enabled until the screen is on and the device is unlocked. There's an inherent issue of a user that places a device on a tag, the phone turns off and then turn the screen back on but never unlocks the device. The NFC service will not start in this instance. If they *never* unlock the device you can't successfully detect a "tag lost" event and they could be 20 miles from the tag before they unlock the device to check something else, the polling loop starts and detects no tag event incoming (firing your tag lost).

You could try to get around this by setting an arbitrary timer (say tags automatically time out after an hour), but what if they just set it on their desk and don't turn it on and unlock for the rest of the work day? Now your time out is firing improperly as the device is still on the tag but your service has no way of knowing that (unless you start actively monitoring motion/location based sensors with fairly high accuracy).

The other issue is that not all passive tags respond properly when the device has detected/read them, is placed on them, screen turned off and then back on. I've tested many tags of all types - within the same type even there is a large variance. Some respond instantly, some after a few seconds. Other times I've had tags not respond at all until the device is physically removed from them and re-introduced. This is going to cause some issues with the detection algorithm as it could easily fire off false alarms for tag removed when the tag is still there. You'd need some way to get around this in a manner that's consistent and correct. For the tags that don't fire 10 seconds after the service is on (requiring a re-introduction of the device) I haven't found an answer that's any good.

Now, all of this could be avoided by modifying the NFC service to *always* run, but you're not releasing that to a larger consumer base - however for personal deployment it could work well (you may see some impact in battery life though).

If you have some ideas and want to talk through some of them let me know. This is an interesting problem as it's one that could be really useful from an implementation side, but the nature of passive tags and Android's NFC implementation make it problematic for developing a solution that works on consumer devices.