rxm
rxm

Reputation: 81

Migrating Chrome USB App API to Web USB API

I currently have source code for a Chrome App, but as the platform is being deprecated I need to migrate my API to a Progressive Web Application.

As I still want support for USB in my web platform application, it was suggested that I use the Web USB API to retain functionality, but I can't seem to figure out the equivalent's for the following and how to implement them:

chrome.usb.releaseInterface()
chrome.usb.closeDevice()
chrome.usb.claimInterface()
chrome.usb.findDevices()
chrome.usb.bulkTransfer()

(Also, I also found there is a USB Library for Node.JS that works similarly also; is this a good alternative too?)

Upvotes: 0

Views: 678

Answers (1)

Reilly Grant
Reilly Grant

Reputation: 6083

The WebUSB API provides a USBDevice interface which is returned by navigator.usb.getDevices() and navigator.usb.requestDevice(). This interface has methods equivalent to all but one of those listed above:

chrome.usb.releaseInterface() -> releaseInterface()
chrome.usb.closeDevice()      -> close()
chrome.usb.claimInterface()   -> claimInterface()
chrome.usb.bulkTransfer()     -> transferIn() or transferOut().

chrome.usb.findDevices() is more complex to replace and first requires explaining the differences between the permission model for the WebUSB API and the chrome.usb API. The WebUSB API does not provide an install-time permission to access USB devices. A site must call navigator.usb.requestDevice() to ask the user for permission to access new USB devices. For devices with serial numbers permissions are remembered and so you can call navigator.usb.getDevices() to get a list of currently connected devices a site previously got permission to access. This is the same model as the chrome.usb.getUserSelectedDevices() function. The chrome.usb.findDevices() function also implicitly opened the devices in the process of returning them to the application. There is no equivalent to this behavior. The site must explicitly call open() on the USBDevice interfaces returned by these methods.

Note, that if this application is being deployed into a managed environment the WebUsbAllowDevicesForUrls policy can be used to mimic the Chrome Apps permission model. Devices allowed by policy will be returned by navigator.usb.getDevices() without the need to call navigator.usb.requestDevice() and prompt the user first.

Upvotes: 3

Related Questions