HIDDevice: phương thức sendFeatureReport()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Note: This feature is available in Web Workers, except for Shared Web Workers.

Phương thức sendFeatureReport() của giao diện HIDDevice gửi báo cáo tính năng đến thiết bị HID. Báo cáo tính năng là cách để các thiết bị HID và ứng dụng trao đổi dữ liệu HID không được chuẩn hóa.

reportId cho mỗi định dạng báo cáo mà thiết bị này hỗ trợ có thể được lấy từ HIDDevice.collections.

Cú pháp

js
sendFeatureReport(reportId, data)

Tham số

reportId

Một ID báo cáo 8-bit. Nếu thiết bị HID không sử dụng ID báo cáo, truyền 0.

data

Các byte dưới dạng ArrayBuffer, TypedArray, hoặc DataView.

Giá trị trả về

Một Promise được giải quyết với giá trị undefined khi báo cáo đã được gửi.

Ngoại lệ

NotAllowedError DOMException

Được ném nếu việc gửi báo cáo thất bại vì bất kỳ lý do nào.

Ví dụ

Trong ví dụ sau, sendFeatureReport() làm thiết bị nhấp nháy. Bạn có thể xem thêm ví dụ và bản demo trực tiếp trong bài viết Kết nối với các thiết bị HID không phổ biến.

js
const reportId = 1;
for (let i = 0; i < 10; i++) {
  // Turn off
  await device.sendFeatureReport(reportId, Uint32Array.from([0, 0]));
  await new Promise((resolve) => setTimeout(resolve, 100));
  // Turn on
  await device.sendFeatureReport(reportId, Uint32Array.from([512, 0]));
  await new Promise((resolve) => setTimeout(resolve, 100));
}

Thông số kỹ thuật

Specification
WebHID API
# dom-hiddevice-sendfeaturereport

Tương thích trình duyệt