This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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.
var promise = USB.requestDevice([filters])
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); });
Specification | Status | Comment |
---|---|---|
WebUSB The definition of 'requestDevice' in that specification. | Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 61 | No | No | No | 48 | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | No | 61 | No | No | 48 | No | No |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/USB/requestDevice