WICG / webhid

Web API for accessing Human Interface Devices (HID)
Other
142 stars 35 forks source link

On mac computer, the oninputreport event has been registered, but the data cannot be received when it is sent for the first time. Only the second time it is sent can the data be received. Continuous one success and one failure #117

Open erixyuan opened 1 year ago

erixyuan commented 1 year ago

The oninputreport event has been registered, but the data cannot be received when it is sent for the first time. Only the second time it is sent can the data be received. Continuous one success and one failure;

  <body>
    <button id="fuck">send</button>
  </body>
<script>
  let requestButton = document.getElementById("fuck");
  let device;

  requestButton.addEventListener("click", async () => {
    try {
      const devices = await navigator.hid.requestDevice({
        filters: [
          {
            vendorId: 123,
          },
        ],
      });
      device = devices[0];
    } catch (error) {
      console.log("An error occurred.");
    }

    if (!device) {
      console.log("No device was selected.");
    } else {
      console.log(`HID: ${device.productName}`);
        try {
          if (!device.opened) {
            console.log("open device")
            await device.open().then(()=>{
              device.oninputreport = ({device, reportId, data}) => {
                console.log(`Input report ${reportId} from ${device.productName}:`, new Uint8Array(data.buffer));
              };
            });
          }
          const outputReportData = new Uint8Array([0xaa]);
          console.log(device);
          await device.sendReport(0, outputReportData).then(()=>{
            console.log("send success")
          });
          console.log("Output report sent");
        } catch (error) {
          console.log("An error occurred.", error)
        }
    }
  });
  </script>
</html>
nondebug commented 1 year ago

What is the USB device you are connecting to?

Does it work if you set device.oninputreport before calling device.open()?

device.oninputreport = ({device, reportId, data}) => {
  console.log(`Input report ${reportId} from ${device.productName}:`, new Uint8Array(data.buffer));
};
await device.open();
erixyuan commented 1 year ago

What is the USB device you are connecting to?

Does it work if you set device.oninputreport before calling device.open()?

device.oninputreport = ({device, reportId, data}) => {
  console.log(`Input report ${reportId} from ${device.productName}:`, new Uint8Array(data.buffer));
};
await device.open();

It is a hardware device developed by ourselves. There is no abnormality in Chrome communication on Windows. This abnormality only occurs on Mac. The first time the data is sent, it cannot be received and returned, only the second time. I tried your way of writing and the result is the same.

Another phenomenon is that when sending for the first time, receiving data fails. The second time you send it, you receive the result of the first time.