I am not familiar with chrome-nfc, but taking a picture in the dark, paying attention to the source , it looks like you want to use the wait_for_tag method, for example:
chrome.nfc.wait_for_tag(device, 3000, function(tag_type, tag_id) {
... Where device is your reader, 3000 is the maximum latency (in ms) and replacement // Do your magic here. to the desired logic. If it expires, both tag_type and tag_id will be null .
If you want to wait forever, you can just recursively call the function with the code above. Example:
function waitAllDay(device) { chrome.nfc.wait_for_tag(device, 1000, function(tag_type, tag_id) { if(tag_type !== null && tag_id !== null) {
Suppose you want it to continue to wait even after the tag has been presented. Wrap waitAllDay(device); in else if you want it to stop after reading the tag.
UPDATE: The wait_for_tag method does not seem to work as intended, so I am proposing a second solution. I leave the existing solution in place if the method is fixed by the chrome-nfc developers.
Another thing worth trying is to use chrome.nfc.read , passing in the timeout options inside window.setInterval .
var timer = window.setInterval(function () { chrome.nfc.read(device, { timeout: 1000 }, function(type, ndef) { if(!!type && !!ndef) {
Be sure to call window.clearInterval(timer) whenever you want to stop viewing tags.