【问题标题】:Fire an event when an NFC card is presented出示 NFC 卡时触发事件
【发布时间】:2015-12-05 15:27:59
【问题描述】:

我正在尝试在 Chromebook 上构建一个网络应用程序,我需要它使用 ACR122U NFC 读取 RFID 卡序列号。我正在使用chrome-nfc

我正在愉快地阅读卡片,但是当卡片出现时我不知道如何触发事件。

chrome-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

两者都立即返回上面的结果,我用什么数字来超时似乎并不重要......

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

【问题讨论】:

    标签: javascript nfc google-chrome-app chromebook acr122


    【解决方案1】:

    我不熟悉 chrome-nfc,但是通过对 source 进行逆向工程在黑暗中进行拍摄,看起来你会想要使用 wait_for_tag 方法,例如:

    chrome.nfc.wait_for_tag(device, 3000, function(tag_type, tag_id) {
        // Do your magic here.
    });
    

    ...device 是您的读者,3000 是等待的最长时间(以毫秒为单位),并用您想要的逻辑替换 // Do your magic here.。如果超时,tag_typetag_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-nfc 的开发人员修复了该方法,我将保留现有解决方案。

    要尝试的另一件事是使用chrome.nfc.read,在window.setInterval 中传入timeout 选项。

    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);
    

    请确保在您希望它停止监视标签时致电window.clearInterval(timer)

    【讨论】:

    • 感谢@Grinn 的建议。但是我遇到了问题,它实际上并没有做任何等待,它总是立即返回。我已经用输出示例更新了问题。
    • @Hank 读卡器没有卡时tag_id的值是多少?
    • 它是未定义的......它好像该函数被取消并且它永远不会使它成为我的回调
    • @Hank 看来wait_for_tag 并没有在所有执行路径中调用回调。查看我的更新并告诉我进展如何。
    • 感谢您的努力!我尝试了您的第二个解决方案,它给出了相同的结果。立即返回DEBUG: found 0 target, tg=144
    【解决方案2】:

    虽然我不认为这是一个合适的解决方案;这是我暂时使用的解决方法。

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2015-07-08
      • 2013-04-02
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 2019-06-07
      相关资源
      最近更新 更多