USBDevice: opened 屬性
注意:此功能在 Web Workers 中可用。
USBDevice 介面的只讀屬性 opened 指示是否已與配對的 USB 裝置啟動了會話。在網頁可以控制裝置之前,必須先開啟該裝置。
值
一個 Boolean 值。
示例
此示例適用於具有多色 LED 的假設 USB 裝置。它演示了在呼叫 USBDevice.controlTransferOut 設定特定 LED 顏色之前,如何測試裝置是否已開啟。
注意: 可以傳遞給 USB 裝置的資料以及如何傳遞這些資料是每個裝置特有的。
js
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,
);
}
}
規範
| 規範 |
|---|
| WebUSB API # dom-usbdevice-opened |
瀏覽器相容性
載入中…