Fire event when NFC card is presented

I'm trying to create a webapp on a Chromebook, I need it to read the serial numbers of RFID cards using the NFC ACR122U. I am using chrome-nfc .

I read cards happily, but I don’t know how to trigger an event when a card is presented.

Are there any events in chrome-nfc that I can use to find out when the card was presented to the reader?

EDIT: I am trying to use chrome.nfc.wait_for_tag, but it does not behave as I expected.

// With a card on the reader chrome.nfc.wait_for_tag(device, 10000, function(tag_type, tag_id){ var CSN = new Uint32Array(tag_id)[0]; console.log ( "CSN: " + CSN ); }); [DEBUG] acr122_set_timeout(round up to 1275 secs) DEBUG: InListPassiveTarget SENS_REQ(ATQA)=0x4, SEL_RES(SAK)=0x8 DEBUG: tag_id: B6CA9B6B DEBUG: found Mifare Classic 1K (106k type A) [DEBUG] nfc.wait_for_passive_target: mifare_classic with ID: B6CA9B6B CSN: 1805372086 // with no card on the reader chrome.nfc.wait_for_tag(device, 10000, function(tag_type, tag_id){ var CSN = new Uint32Array(tag_id)[0]; console.log ( "CSN: " + CSN ); }); [DEBUG] acr122_set_timeout(round up to 1275 secs) DEBUG: found 0 target, tg=144 

Both return results immediately, as indicated above, it doesn't matter what number I use for the timeout ...

If I call a function without a card on the reader, and then immediately put the card in the reader after calling the function, I do not get any output in the console.

+5
source share
2 answers

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) { // Do your magic here. }); 

... 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) { // Do your magic here. } waitAllDay(device); }); } 

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) { // Do your magic here. // Uncomment the next line if you want it to stop once found. // window.clearInterval(timer); } }); }, 1000); 

Be sure to call window.clearInterval(timer) whenever you want to stop viewing tags.

+2
source

Although I do not think this is the right decision; here is the workaround i am currently using.

 function listen_for_tag(callback, listen_timeout){ var poll_delay = 400; //ms var listen_loop = null; if(!listen_timeout){ listen_timeout = 99999999; } function check_for_tag(){ if(listen_timeout < 0) { clearInterval(listen_loop); console.log("we didnt find a tag. finished"); } chrome.nfc.wait_for_tag(dev_manager.devs[0].clients[0], 10, function(tag_type, tag_id){ console.log ( "FOUND A TAG!!" ); clearInterval(listen_loop); // handle the callback (call it now) var C = callback; if (C) { callback = null; window.setTimeout(function() { C(tag_type, tag_id); }, 0); } }); listen_timeout -= poll_delay; } listen_loop = setInterval(check_for_tag, poll_delay); } 
0
source

All Articles