这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

requestDevice() method of the USB interface returns a Promise that resolves with an instance of USBDevice if the specified device is found. Calling this function triggers the user agent's pairing flow.

句法

USB.requestDevice([filters])
					

参数

过滤
An array of filter objects for possible devices you would like to pair. Each filter object can have the following properties:
  • vendorId
  • productId
  • classCode
  • subclassCode
  • protocolCode
  • serialNumber

返回值

A Promise that resolves with an instance of USBDevice .

范例

The following example looks for one of two USB devices. Notice that two product IDs are specified. Both are passed to requestDevice() . This triggers a user-agent flow that prompts the user to select a device for pairing. Only the selected device is passed to then() .

The number of filters does not specifiy the number of devices shown by the user agent. For example, if only a USB device with product ID 0xa800 is found, then only one device will be listed by the user agent. On the other hand if the user agent finds two of the first listed device and one of the second, then all three devices will be listed.

const filters = [
        {vendorId: 0x1209, productId: 0xa800},
        {vendorId: 0x1209, productId: 0xa850}
      ];
navigator.usb.requestDevice({filters: filters})
.then(usbDevice => {
  console.log("Product name: " + usbDevice.productName);
})
.catch(e => {
  console.log("There is no device. " + e);
});
					

规范

规范 状态 注释
WebUSB
The definition of 'requestDevice' in that specification.
草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
requestDevice Chrome 61 Edge 79 Firefox No IE No Opera 48 Safari No WebView Android No Chrome Android 61 Firefox Android No Opera Android 45 Safari iOS No Samsung Internet Android 8.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

元数据

  • 最后修改:
  1. USB
  2. 方法
    1. getDevices()
    2. onconnect
    3. ondisconnect
    4. requestDevice()