androidgoogle-chromewebusb

Understanding the WebUSB connection workflow


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!


Solution

  • 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-access

    It 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.

    enter image description here