Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Oct 12, 2023 · 2 comments

Comments

@erixyuan
Copy link

erixyuan commented Oct 12, 2023

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>
@erixyuan erixyuan changed the title 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 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 Oct 12, 2023
@nondebug
Copy link
Collaborator

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
Copy link
Author

erixyuan commented Oct 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants