I am using the WebHID api. After a couple of test, I realized that the popup to select the an HID device appears even when no HID are available, which can be annoying the the end user. I read the doc and I don't how to tell the api to just stay silent in case of no devices are connected. Here is my code :
let device = null;
async function connectToHID() {
if ("hid" in navigator) {
let alreadyAssignedDevices = await navigator.hid.getDevices();
const alreadyAssignedDevice = alreadyAssignedDevices.filter((device) => {
return device.vendorId === 1151 && device.productId === 64161;
});
try {
if (alreadyAssignedDevice.length !== 1) {
[device] = await navigator.hid.requestDevice({
filters: [
{
vendorId: 1151,
productId: 64161,
},
],
});
} else {
device = alreadyAssignedDevice[0];
}
} catch (error) {
console.log(error);
}
if (device) {
await device.open();
}
}
}
Chrome browser will always show a device prompt because user may plug a peripheral matching your filters after
navigator.hid.requestDevice()
has been called.