Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当出示NFC卡时触发事件_Javascript_Nfc_Google Chrome App_Chromebook_Acr122 - Fatal编程技术网

Javascript 当出示NFC卡时触发事件

Javascript 当出示NFC卡时触发事件,javascript,nfc,google-chrome-app,chromebook,acr122,Javascript,Nfc,Google Chrome App,Chromebook,Acr122,我正在尝试在Chromebook上构建一个webapp,我需要它读取带有ACR122U NFC的RFID卡序列号。我正在使用 我很高兴地读着卡片,但我不知道如何在出示卡片时触发事件 chrome nfc中是否有任何事件我可以用来知道何时有一张卡被呈现给读者 编辑:我一直在尝试使用chrome.nfc.wait_for_标记,但它的行为与我预期的不同 // With a card on the reader chrome.nfc.wait_for_tag(device, 10000, functi

我正在尝试在Chromebook上构建一个webapp,我需要它读取带有ACR122U NFC的RFID卡序列号。我正在使用

我很高兴地读着卡片,但我不知道如何在出示卡片时触发事件

chrome nfc中是否有任何事件我可以用来知道何时有一张卡被呈现给读者

编辑:我一直在尝试使用chrome.nfc.wait_for_标记,但它的行为与我预期的不同

// 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
两者都会立即返回上面的结果,我使用的超时数字似乎并不重要


如果我在读卡器上没有卡的情况下调用函数,然后在函数调用后立即将卡放在读卡器上,我在控制台中没有得到任何输出。

我不熟悉chrome nfc,但通过反向工程在黑暗中拍摄,看起来您可能希望使用
wait_for_标记
方法,如:

chrome.nfc.wait_for_tag(device, 3000, function(tag_type, tag_id) {
    // Do your magic here.
});
…当
设备
是您的读卡器时,
3000
是最长等待时间(毫秒),并替换
//在此处施展您的魔法。
使用您所需的逻辑。如果超时,
tag\u type
tag\u 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);
    });
}
这是假设您希望它在显示标记后继续等待。包装
waitall-day(设备)else
中选择code>

更新:似乎
wait\u for\u标记
方法无法按预期工作,因此我提出了第二种解决方案。我将保留现有的解决方案,以防chrome nfc的开发者修复该方法

另一种尝试是使用
chrome.nfc.read
,在
窗口内传递
超时
选项。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);

确定并调用<代码>窗口。CurryTalk(计时器)< /C>每当您希望它停止观看标签时。

< P>而我不认为这是正确的解决方案;这是我目前正在使用的一个变通方法

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);
}
函数侦听\u以查找\u标记(回调、侦听\u超时){
var poll_delay=400;//毫秒
var listen_loop=null;
如果(!侦听超时){
监听超时=9999999;
}
_标签()的功能检查{
如果(侦听超时<0){
clearInterval(侦听\u循环);
log(“我们没有找到标记.finished”);
}
chrome.nfc.wait_for_标记(dev_manager.devs[0]。客户端[0],10,函数(标记类型,标记id){
console.log(“找到标记!!”;
clearInterval(侦听\u循环);
//处理回调(立即调用)
var C=回调;
如果(C){
callback=null;
setTimeout(函数(){
C(标签类型、标签id);
}, 0);
}
});
侦听超时-=轮询延迟;
}
侦听循环=设置间隔(检查标记、轮询延迟);
}

谢谢@Grinn的建议。我有问题,但是,它实际上没有做任何等待,它总是立即返回。我用一个输出的例子更新了这个问题。@Hank当读卡器中没有卡时,
tag\u id
的值是多少?它是未定义的。。。这就好像功能被取消了,它永远不会到达我的邮箱callback@Hank似乎,
wait\u for\u标记
没有在所有执行路径中调用回调。看到我的更新,让我知道它如何进行。谢谢你的努力!我尝试了你的第二个解决方案,结果也一样。使用
DEBUG立即返回:找到0个目标,tg=144