【发布时间】:2020-05-10 22:45:19
【问题描述】:
我正在尝试使用以下代码从 Android 上的 chrome 81 读取 NFC 标签:
<html>
<head>
<title>NFC</title>
</head>
<body>
<button onclick="reader()">Scan</button>
<script>
function reader(){
const reader = new NDEFReader();
reader.scan().then(() => {
alert("Scan started successfully.");
reader.onerror = () => {
alert("Cannot read data from the NFC tag. Try another one?");
};
reader.onreading = event => {
alert("NDEF message read.");
};
}).catch(error => {
alert(`Error! Scan failed to start: ${error}.`);
});
}
</script>
</body>
我遇到的问题是它从 nfc 标签中读取条目,但没有像代码建议的那样发出警报,而是尝试将我引导到手机上已安装的应用程序。但是,当我使用使用完整 API 的 https://googlechrome.github.io/samples/web-nfc/ 时,它可以工作并作为数据显示在网页中。主要区别在于我使用通过 chrome://flags 方法启用 NFC API。
除了阅读标签之外,我唯一的目的是将内容作为变量保存到 sessionStorage 以供网站的其他部分使用。
提前致谢
【问题讨论】:
-
您在“catch(error)”部分遇到的错误是什么?
标签: javascript android webnfc