了解 WebUSB 连接工作流程
我正在开发一个使用在 Android 设备上运行的 WebUSB 的 Chrome Web 应用程序,并试图了解连接管理的工作原理。
如果我没有正确阅读规范,如果您还没有与 USB 设备交互的权限,则可以调用 requestDevice 来获取用户的权限以触发初始连接工作流程。您还可以首先调用 getDevices() 以查看是否已授予权限,如果是,则可以从那里继续,而无需执行初始连接工作流程。
假设这是一个准确的摘要(如果不是,请纠正我!),那么如何存储和管理该连接信息?如果我转到 chrome://usb-devices,如果当前未插入设备,我就看不到这些设备。如果我转到本地存储,我看不到任何看起来像设备列表的内容。已获得许可的信息位于何处?是否可以手动或以编程方式忘记连接信息?
提前致谢!
I am working on an Chrome webapp that uses WebUSB running on an Android device, and am trying to understand how the connection management works.
If I am reading the spec right, if you don't already have permission to interact with the USB device, you call requestDevice to get permission from the user to trigger the initial connection workflow. You can also call getDevices() first to see if permission has already been granted, and if so can proceed from there without going through the initial connection workflow.
Assuming that's an accurate summary (please correct me if not!), then how does that connection information get stored and managed? If I go to chrome://usb-devices, I don't see the devices if they are not currently plugged in. If I go to Local Storage, I din't see anything that looks like a list of devices. Where does the permission-has-already-been-granted information live? Is it possible to forget connection information, manually or programmatically?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
navigator.usb.requestDevice()
将提示用户在浏览器选择器中选择 USB 设备。选择后,浏览器将在内部记住用户已授予访问权限的设备。从那里,插入后,无需用户通过调用
navigator.usb.getDevices()
进行提示即可访问这些设备。在 Chrome 101 中,网站可以通过调用 USB 设备对象上的
forget()
方法来撤销对设备的访问。参见 https://web.dev/usb/#revoke-access也是可以的供用户直接从浏览器的“页面信息”气泡中撤销对 USB 设备的访问。请参阅下面的屏幕截图。
A call to
navigator.usb.requestDevice()
will prompt user to pick a USB device in a browser picker. When chosen, the browser will remember internally which devices user have granted access to.From there, when plugged in, those devices will be accessible without user prompting from a call to
navigator.usb.getDevices()
.It is possible in Chrome 101 for a website to revoke access to a device by calling the
forget()
method on a USB device object. See https://web.dev/usb/#revoke-accessIt is also possible for a user to revoke access to a USB device directly from the browser in the "page info" bubble. See screenshot below.
权限存储在 Chrome 的用户首选项文件中,如果您好奇,可以在
chrome://prefs-internals
查看原始数据并搜索“usb_chooser_data”
。要查看和编辑 USB 权限,您可以按照其他答案的建议打开页面信息对话框,或者访问桌面上的
chrome://settings/content/usbDevices
。这将显示当前未连接的设备的权限。Permissions are stored in Chrome's user preferences file which, if you're curious, you can view the raw data at
chrome://prefs-internals
and search for"usb_chooser_data"
.To see and edit USB permissions you can either open the page info dialog as suggested by the other answer or visit
chrome://settings/content/usbDevices
on desktop. This will show permissions for devices that are not currently connected.