WEBHID API: 条码扫描器未触发输入报告

WEBHID API: Inputreport not triggering with barcode scanner

我几乎一直在使用 Nintendo Switch Joy-Con 控制器演示,我对其进行了一些修改,使其可以与我的条形码扫描仪一起使用。它只是不会工作,如果它确实工作,它在 100 次站点刷新中工作一次。

console.log = text => {
    log.textContent += `${text}\r\n`;
  };
  
  let device;
  
  if (!("hid" in navigator)) {
    console.log("WebHID is not available yet.");
  }
  
  navigator.hid.getDevices().then(devices => {
    if (devices.length == 0) {
      console.log(`No HID devices selected. Press the "request device" button.`);
      return;
    }
    device = devices[0];
    console.log(`User previously selected "${device.productName}" HID device.`);
    console.log(`Now press "open device" button to receive input reports.`);
  });
  
  requestDeviceButton.onclick = async event => {
    document.body.style.display = "none";
    try {
        const filters = [
            {
                vendorId: "8792", 
                productId: "9032"
            }
        ];
  
      [device] = await navigator.hid.requestDevice({ filters });
      if (!device) return;
  
      console.log(`User selected "${device.productName}" HID device.`);
      console.log(`Now press "open device" button to receive input reports.`);
    } finally {
      document.body.style.display = "";
    }
  };
  
  openButton.onclick = async event => {
    if (!device) return;
  
    await device.open();
    console.log(`Waiting for user to press button...`);
  
    device.addEventListener("inputreport", event => {
      const { data, device, reportId } = event;
  
      if (device.productId != "9032") return;
  
      const value = data.getUint8(0);
      if (value == 0) return;
  
   
      console.log(`Data: ${value}.`);
    });
  };

openButton.onclick 每次我用条码扫描仪扫描东西时都会触发事件。因此,每次我扫描某些内容时,它都会尝试再次执行 device.open() 。而 inputreport 事件根本不会触发。

有人知道是什么原因造成的吗?

嘿,我切换到 WEBUSB api 并在使用条形码扫描器的 zadig 重新安装 winusb 驱动程序后使其正常工作。

这是我使用 rn 的代码。如果有人感兴趣。按下按钮即可启动 RFID 功能。

const RFID = async () => {
try {
  const filters = [{
    vendorId: 0x1A86
  }];
  const device = await navigator.usb.requestDevice({ filters })

  const configuration_number = 1  // device.configuration.configurationValue
  const interface_number = 0      // device.configuration.interfaces[1].interfaceNumber
  const interface_class = 255      // device.configuration.interfaces[1].alternates[0].interfaceClass
  console.log(device);
  console.log(`configuration number :  ${configuration_number}`);
  console.log(`interface number : ${interface_number} `);
  console.log(`interface class : ${interface_class} `);

  await device.open();
  await device.selectConfiguration(configuration_number);
  await device.claimInterface(interface_number);
  await device.controlTransferOut({
    requestType: 'class',
    recipient: 'interface',
    request: 0x22,
    value: 0x10,
    index: interface_number
  });

  const read = async (device) => {
    const result = await device.transferIn(2, 64);
    const decoder = new TextDecoder();
    const message = decoder.decode(result.data);
    return message
  }

  var m
  do {
    m = await read(device)
    setQR(oldArr => [...oldArr, m])
    console.log(m)
  } while (m.charCodeAt(0) !== 13)

} catch (error) {
  console.log(error);
}}