【问题标题】:WEBHID API: Inputreport not triggering with barcode scannerWEBHID API:Inputreport 不使用条码扫描器触发
【发布时间】:2021-05-31 12:22:05
【问题描述】:

我几乎使用的是 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 事件根本不会触发。

有人知道这是什么原因吗?

【问题讨论】:

  • 例如,首先,为什么不用USB协议分析仪之类的东西来研究发送和接收什么样的数据呢?为什么不尝试使用诸如 C++ 之类的程序而不是 Web 浏览器来处理它们呢?
  • @kunif 我有一个在线网络应用程序,我希望能够扫描条形码(其中有 ids),然后它从后端找到 ids 数据并将其显示在前端。
  • 如果您在 Web 应用程序中使用它,最好使用通用键盘输入模拟而不是奇怪的专有协议。
  • 您的条码扫描设备是什么?你有关于它的任何文件吗?查看web.dev/devices-introduction,看看是否有其他方法可以访问此设备。

标签: javascript google-chrome barcode-scanner webusb webhid


【解决方案1】:

嘿,我切换到 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);
}}

【讨论】:

  • 你能与 WebUSB 分享代码,看看它是如何工作的吗?
  • @François Beaufort 我添加了我现在使用的代码。
  • 谢谢!您的设备名称是什么?
  • @François Beaufort 我现在使用的条码扫描器是 Sunlux XL-9309B。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
相关资源
最近更新 更多