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>
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;