-
Notifications
You must be signed in to change notification settings - Fork 35
Description
I have been tested my device which is a headset connector with WebHID for a few days, I encountered a strange behaviour where I can not send the report using the device object received on the hot-plugged event, it throwed a DOMException with permisson not allowed. I tried a few workaround and found that if I set a 3 seconds delay then acquired the device, the output report can be successfully sent to device. Could it be the device is not actually ready for operation when the connect event fired like a race condition for re-granting permisson ? I assume the WebHID internally maintained a list of granted device, so the device object received on the connect callback argument is essentially the same object as the result returned from the navigator.hid.getDevices(), but it's strange I need to set a delay to get the device before can start doing operation, Below is code i used to showcase the situations mentioned above, thanks in advance
navigator.hid.addEventListener("connect", async ({ device }) => {
// Case 1
// Failed to write the report, immediately send
// if (!device.opened) {
// await device.open();
// }
// const data = new Uint8Array([0x6, 0x1, 0x0, 0, 0, 0, 0, 0]);
// device.sendReport(data[0], data.slice(1, data.length));
// Case 2
// Failed to write the report, send after a few seconds
// if (!device.opened) {
// await device.open();
// }
// setTimeout(() => {
// const data = new Uint8Array([0x6, 0x1, 0x0, 0, 0, 0, 0, 0]);
// device.sendReport(data[0], data.slice(1, data.length));
// }, 3000);
// Case 3
// if set a 2-3 seconds delay, then get the device, the report can be successfully sent
setTimeout(async () => {
let [Device] = await navigator.hid.getDevices();
if (!Device.opened) {
await Device.open();
}
const data = new Uint8Array([0x6, 0x1, 0x0, 0, 0, 0, 0, 0]);
Device.sendReport(data[0], data.slice(1, data.length));
}, 3000);
});