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 |
瀏覽器相容性
載入中…