USB.requestDevice() - Web APIs 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

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

USB.requestDevice([filters])

Parameters

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

Return value

A Promise that resolves with an instance of USBDevice.

Example

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);
});

Specifications

SpecificationStatusComment
WebUSB
The definition of 'requestDevice' in that specification.
DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:85 次

字数:3837

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文