USBDevice: opened プロパティ

Limited availability

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

Experimental: これは実験的な機能です。
本番で使用する前にブラウザー互換性一覧表をチェックしてください。

USBDevice インターフェイスの読み取り専用プロパティ opened は、ペアリングした USB デバイスとのセッションが開始しているかを表します。デバイスは、ウェブページから制御する前に開かれなければなりません。

boolean です。

この例では、複数の色がある LED を搭載した仮想の USB デバイスを扱います。この例は、USBDevice.controlTransferOut を呼び出して LED の色を設定する前にデバイスが開かれているかを確認する方法を示します。

メモ: USB デバイスにどのようなデータを渡すか、そしてどのようにそれを渡すかは、各デバイスに固有で独特です。

js
async function setDeviceColor(usbDevice, r, g, b) {
  if (device.opened) {
    // この仮想 USB デバイスに渡すデータは Uint8Array でなければならない
    const payload = new Uint8Array([r, g, b]);

    await usbDevice.controlTransferOut(
      {
        requestType: "vendor",
        recipient: "device",
        request: 1,
        value: 0,
        index: 0,
      },
      payload,
    );
  }
}

仕様書

Specification
WebUSB API
# ref-for-dom-usbdevice-opened

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
opened
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.