首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WEBHID API:条形码扫描器不触发输入端口

WEBHID API:条形码扫描器不触发输入端口
EN

Stack Overflow用户
提问于 2021-05-31 20:22:06
回答 1查看 152关注 0票数 1

我主要使用的是任天堂Switch Joy-Con控制器演示,我对它做了一些修改,使其与我的条形码扫描仪一起工作。它就是不起作用,如果它起作用了,100次站点刷新中就有一次起作用。

代码语言:javascript
复制
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事件根本不会触发。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-07 22:19:37

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

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

代码语言:javascript
复制
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);
}}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67773640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档