Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire an event when an NFC card is presented

I am attempting to build a webapp on a Chromebook, I need it to read RFID card serial numbers with an ACR122U NFC. I am using chrome-nfc.

I am reading cards happily, but I do not know how to fire an event when a card is presented.

Are there any events in chrome-nfc I can use to know when a card has been presented to the reader?

EDIT: I have been trying to use chrome.nfc.wait_for_tag, but it does not behave as I would expect.

// 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 the results as above immediately, it does not seem to matter what number I use for a timeout...

If I call the function with no card on the reader, and then immediately put the card on the reader after function call, I get no output in the console.

like image 485
Hank Avatar asked Oct 30 '22 19:10

Hank


1 Answers

I'm not familiar with chrome-nfc, but taking a shot in the dark by reverse engineering the source, it looks like you would want to use the wait_for_tag method, like:

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 time to wait (in ms), and replacing // Do your magic here. with your desired logic. If it times out, both tag_type and tag_id will be null.

If you wanted to wait indefinitely, you could just recursively call a function with the above code. 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);
    });
}

That's assuming you want it to continue waiting even after a tag has been presented. Wrap the waitAllDay(device); in an else if you want it to stop once a tag is read.

UPDATE: It seems the wait_for_tag method does not work as intended, so I'm proposing a second solution. I'm leaving the existing solution in place in case the method is fixed by the developers of chrome-nfc.

Another thing to try is to use chrome.nfc.read, passing in a timeout option, inside a 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 and call window.clearInterval(timer) whenever you want it to stop watching for tags.

like image 109
Grinn Avatar answered Nov 11 '22 03:11

Grinn