HIDDevice:sendReport() 方法
注意:此功能在 Web Workers 中可用,但 共享 Web Workers 除外。
HIDDevice 接口的 sendReport() 方法将一个输出报告发送到 HID 设备。
该设备支持的每种报表格式的 reportId 可以从 HIDDevice.collections 中检索。
语法
js
sendReport(reportId, data)
参数
reportId-
一个 8 位报表 ID。如果 HID 设备不使用报表 ID,则发送
0。 data-
字节作为
ArrayBuffer、TypedArray或DataView。
返回值
一个 Promise,一旦报告发送完毕,它将以 undefined 解析。
异常
NotAllowedErrorDOMException-
如果发送报告因任何原因失败,则抛出此错误。
示例
下面的示例演示了如何使用输出报告使 Joy-Con 设备产生震动。您可以在文章 Connecting to uncommon HID devices 中找到更多示例和实时演示。
js
// First, send a command to enable vibration.
// Magical bytes come from https://github.com/mzyy94/joycon-toolweb
const enableVibrationData = [1, 0, 1, 64, 64, 0, 1, 64, 64, 0x48, 0x01];
await device.sendReport(0x01, new Uint8Array(enableVibrationData));
// Then, send a command to make the Joy-Con device rumble.
// Actual bytes are available in the sample.
const rumbleData = [
/* … */
];
await device.sendReport(0x10, new Uint8Array(rumbleData));
规范
| 规范 |
|---|
| WebHID API # dom-hiddevice-sendreport |
浏览器兼容性
加载中…