USBDevice: thuộc tính opened
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
Thuộc tính chỉ đọc opened của giao diện USBDevice cho biết liệu một phiên làm việc đã được bắt đầu với thiết bị USB đã ghép nối hay chưa. Thiết bị phải được mở trước khi trang web có thể điều khiển nó.
Giá trị
Một boolean.
Ví dụ
Ví dụ này dành cho một thiết bị USB giả định có đèn LED nhiều màu. Nó cho thấy cách kiểm tra xem thiết bị đã được mở chưa trước khi gọi USBDevice.controlTransferOut để đặt màu LED được chỉ định.
Note: Dữ liệu nào có thể được truyền tới thiết bị USB và cách thức truyền là đặc thù và độc đáo đối với từng thiết bị.
async function setDeviceColor(usbDevice, r, g, b) {
if (device.opened) {
// This hypothetical USB device requires that the data passed to
// it be in a Uint8Array.
const payload = new Uint8Array([r, g, b]);
await usbDevice.controlTransferOut(
{
requestType: "vendor",
recipient: "device",
request: 1,
value: 0,
index: 0,
},
payload,
);
}
}
Thông số kỹ thuật
| Specification |
|---|
| WebUSB API> # dom-usbdevice-opened> |