gpt4 book ai didi

javascript - 出示 NFC 卡时触发事件

转载 作者:搜寻专家 更新时间:2023-11-01 04:30:41 25 4
gpt4 key购买 nike

我正在尝试在 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

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

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

最佳答案

我不熟悉 chrome-nfc,但通过对 source 进行逆向工程在黑暗中进行了一次尝试。 , 看起来您想使用 wait_for_tag 方法,例如:

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

...其中 device 是您的阅读器,3000 是最长时间等待(以毫秒为单位),并替换 //在这里发挥您的魔力。 与你想要的逻辑。如果超时,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);
});
}

这是假设您希望它在标 checkout 现后继续等待。如果您希望它在读取标签后停止,请将 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)

关于javascript - 出示 NFC 卡时触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32483308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com