USBDevice: claimInterface() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

USBDevice 接口的 claimInterface() 方法会返回一个 Promise,当请求的接口被成功声明为独占访问时,该 Promise 会解析。

语法

js
claimInterface(interfaceNumber)

参数

interfaceNumber

设备支持的接口之一的索引。接口是设备特定的。

返回值

一个 Promise

示例

以下示例展示了在连接到 USB 设备的上下文中使用的 claimInterface()

js
async function connectDevice(usbDevice) {
  await usbDevice.open();
  if (usbDevice.configuration === null) await usbDevice.selectConfiguration(1);
  await usbDevice.claimInterface(0);
}

规范

规范
WebUSB API
# dom-usbdevice-claiminterface

浏览器兼容性