大网数据广告活动
Web 标准与前端开发

USB: requestDevice() method

· 52CE 我爱测

The **`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.

WebUSB API

The 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.

Syntax

requestDevice(options)

Parameters

  • options
    • : An object that sets options for selecting an appropriate device. The available options are:
      • 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
      • exclusionFilters
        • : An array of filter objects representing devices to exclude from the pairing flow. These objects have the same properties as those in filters. Exclusion takes priority over inclusion.

Return value

A Promise that resolves with an instance of USBDevice.

Security

Transient user activation is required. The user has to interact with the page or a UI element in order for this feature to work.

Examples

Requesting specific USB devices

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 specify 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 })
  .then((usbDevice) => {
    console.log(`Product name: ${usbDevice.productName}`);
  })
  .catch((e) => {
    console.error(`There is no device. ${e}`);
  });

Excluding devices

The following example requests a device with vendor ID 0x1209; it excludes devices with that vendor ID that have the product ID 0xa850:

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

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。