我试图在Chromebook上构建一个网页应用程序,我需要它用ACR122U NFC读取射频识别卡的序列号。我正在使用铬-nfc。
我正在愉快地阅读卡片,但我不知道当一张卡片出现时,如何触发一个事件。
有什么事件在铬-nfc中,我可以用来知道什么时候一张卡已经呈现给读者?
编辑:我一直在尝试使用chrome.nfc.wait_for_tag,但它的行为并不像我所期望的那样。
// 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两者都会立即返回结果,这似乎并不重要,我使用的一个超时.
如果我在读取器上调用没有卡片的函数,然后在函数调用之后立即将该卡片放在读取器上,则控制台中没有输出。
发布于 2015-09-09 15:25:27
我不太熟悉chrome,但是通过反向工程来源,您可能会想要使用wait_for_tag方法,比如:
chrome.nfc.wait_for_tag(device, 3000, function(tag_type, tag_id) {
// Do your magic here.
});...Where device是您的读取器,3000是等待的最大时间(以ms表示),并以所需的逻辑替换// Do your magic here.。如果超时,tag_type和tag_id都将是null。
如果您想无限期地等待,可以使用上面的代码递归地调用一个函数。示例:
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);
});
}这是假设您希望它继续等待,即使在标记已经显示之后。如果您想让waitAllDay(device);在读取标记后停止,请将它包装在else中。
更新:--似乎wait_for_tag方法不像预期的那样工作,所以我提出了第二种解决方案。我将保留现有的解决方案,以防该方法被chrome的开发人员修复。
另一件事是使用chrome.nfc.read,在window.setInterval中传递一个timeout选项。
var timer = window.setInterval(function () {
chrome.nfc.read(device, { timeout: 1000 }, function(type, ndef) {
if(&& ndef) {
// Do your magic here.
// Uncomment the next line if you want it to stop once found.
// window.clearInterval(timer);
}
});
}, 1000);当您希望window.clearInterval(timer)停止监视标记时,一定要打电话给它。
发布于 2015-09-10 14:14:33
虽然我不认为这是一个适当的解决方案,但我暂时使用的是一个解决办法。
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);
}https://stackoverflow.com/questions/32483308
复制相似问题