バーコード スキャナーで動作するように少し変更した Nintendo Switch Joy-Con コントローラーのデモをほとんど使用しています。そして、それは機能しません。機能する場合、100回のサイト更新に1回は機能します.
console.log = text => {
log.textContent += `${text}\r\n`;
};
let device;
if (!("hid" in navigator)) {
console.log("WebHID is not available yet.");
}
navigator.hid.getDevices().then(devices => {
if (devices.length == 0) {
console.log(`No HID devices selected. Press the "request device" button.`);
return;
}
device = devices[0];
console.log(`User previously selected "${device.productName}" HID device.`);
console.log(`Now press "open device" button to receive input reports.`);
});
requestDeviceButton.onclick = async event => {
document.body.style.display = "none";
try {
const filters = [
{
vendorId: "8792",
productId: "9032"
}
];
[device] = await navigator.hid.requestDevice({ filters });
if (!device) return;
console.log(`User selected "${device.productName}" HID device.`);
console.log(`Now press "open device" button to receive input reports.`);
} finally {
document.body.style.display = "";
}
};
openButton.onclick = async event => {
if (!device) return;
await device.open();
console.log(`Waiting for user to press button...`);
device.addEventListener("inputreport", event => {
const { data, device, reportId } = event;
if (device.productId != "9032") return;
const value = data.getUint8(0);
if (value == 0) return;
console.log(`Data: ${value}.`);
});
};
openButton.onclick イベントは、バーコード スキャナーで何かをスキャンするたびに発生します。そのため、何かをスキャンするたびに device.open() を再度実行しようとします。そして、inputreport イベントはまったく発生しません。
何が原因なのか誰にもわかりませんか?