Reputation: 1800
In WebUSB API is there a way to get a list of all available devices?
The situation I have here is that a computer can have more than one identical device, lets say it has 2 identical printers. I want to avoid displaying the popup asking the user to select a device so I would like to check if the device is already claimed.
I can check if a device is already claimed by running navigator.usb.getDevices();
but this only lists devices that are already claimed, if there's an identical device that is not yet claimed I can't know.
So the issue is that if I consider that the device is already claimed and don't show the dialog the user can't use the second device and if I never consider a device claimed I will have to show the dialog every time.
Upvotes: 1
Views: 2095
Reputation: 6083
There is no API to get a list of all available devices. A site can only get access to a device (and information about that device) by asking the user for permission.
I think there's some confusion about what navigator.usb.getDevices()
does. It returns the set of connected devices the user has previously granted the site access to, whether they are claimed or not.
What I recommend applications do is use navigator.usb.getDevices()
to show the user the set of devices that are available and have an "add device" button which will call navigator.usb.requestDevice()
if the user doesn't see the device they are looking for. If the user knows they have two devices and need to connect to both of them they'd click the button twice.
Upvotes: 2