简体   繁体   English

获取 WebUSB API 中可用 USB 设备的列表

[英]Get list of available USB devices in WebUSB API

In WebUSB API is there a way to get a list of all available devices?在 WebUSB API 中有没有办法获取所有可用设备的列表?

The situation I have here is that a computer can have more than one identical device, lets say it has 2 identical printers.我在这里遇到的情况是一台计算机可以有多个相同的设备,假设它有 2 台相同的打印机。 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();我可以通过运行navigator.usb.getDevices();来检查设备是否已经被认领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.所以问题是,如果我认为该设备已被认领并且不显示对话框,则用户无法使用第二个设备,如果我从不考虑已认领的设备,我将不得不每次都显示该对话框。

There is no API to get a list of all available devices.没有 API 可以获取所有可用设备的列表。 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.我认为navigator.usb.getDevices()的作用有些混乱。 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.我推荐应用程序做的是使用navigator.usb.getDevices()向用户显示可用的设备集,并有一个“添加设备”按钮,如果用户没有看到,它将调用navigator.usb.requestDevice()他们正在寻找的设备。 If the user knows they have two devices and need to connect to both of them they'd click the button twice.如果用户知道他们有两个设备并且需要连接到这两个设备,他们会单击该按钮两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM