0

我让 LibNFC 在 Linux 终端上工作,识别我的 ACR122U 阅读器,我想知道是否有一种方法可以通过 Linux 桌面上的 Chrome 工作,因为它与 Android 支持非常相似,所有 NFC 设备处理由libnfc 和浏览器只需要知道这个库,而不是每个类型的 usb 或其他可以执行 NFC 的设备。

我尝试使用 WebNFC API 来连接它:

document.getElementById("scanButton").addEventListener("click", async () => {
    log.innerHTML = "NFC Register started...";

    try {
      const ndef = new NDEFReader();
      await ndef.scan();
      log.innerHTML = ("> Scan started");
  
      ndef.addEventListener("readingerror", () => {
        log.innerHTML = ("Argh! Cannot read data from the NFC tag. Try another one?");
      });
  
      ndef.addEventListener("reading", ({ message, serialNumber }) => {
        log.innerHTML = ("ID  ${serialNumber} logged @" + dt.toLocaleTimeString());   });
    } catch (error) {
      log.innerHTML = (error);
    }
  });

  document.getElementById("stopButton").onclick = function(){
    log.innerHTML = "NFC Register stopped @ " + new Date().toLocaleTimeString();
    }; 

但我遇到了Error: NFC Permission Request denied

和连接它的 WebUSB API:

var usbd = {};
let device;
let deviceEndpoint = 0x02;

let powerUpDevice = new Uint8Array([0x62,0x00, 0x00, 0x00, 0x00,0x00,0x00,0x01,0x00, 0x00]).buffer;
let getCardUID = new Uint8Array([0xff,0xca,0x00,0x00,0x04]).buffer;

(function() {
    'use strict';

    usbd.authorize = function(){
        navigator.usb.requestDevice({ filters: [{ vendorId: 0x072f }] })
            .then(selectedDevice => {
                device = selectedDevice;
                console.log(device.configuration.interfaces[0].interfaceNumber);
                console.log(device.manufacturerName);
                console.log(device.productName);
                console.log(device);
                return device.open()
                    .then(() => {
                        if (device.configuration === null) {
                            return device.selectConfiguration(1);
                        }
                    });
            })
            .then(() => device.claimInterface(0))

但我遇到了Error: ...blocked because it implements a protected interface classie 不支持,所以这是不行的。

有没有办法合并 libusb/libnfc 库或任何其他方法来直接连接 NFC 阅读器以读取到 Web 浏览器/应用程序?

4

1 回答 1

2

自 2021 年 2 月起,Android 仅支持Web NFC 。请参阅https://web.dev/nfc/

WebUSB错误表明您正在请求一个实现受保护类的接口(在以下这些接口中)

  // USB Class Codes are defined by the USB-IF:
  // https://www.usb.org/defined-class-codes
  const uint8_t kProtectedClasses[] = {
      0x01,  // Audio
      0x03,  // HID
      0x08,  // Mass Storage
      0x0B,  // Smart Card
      0x0E,  // Video
      0x10,  // Audio/Video
      0xE0,  // Wireless Controller (Bluetooth and Wireless USB)
  };

我想知道这是否是 linux 的事情,因为我能够通过 WebUSB 与 ACR122U 和 SCL3711 NFC 读取器 USB 设备进行通信。见https://github.com/beaufortfrancois/chrome-nfc

你有没有先尝试过WebHID?见https://web.dev/hid

于 2021-02-10T06:54:35.707 回答