0

I am trying to get a disconnect event from a usb device using the WebUSB API.

I am getting access to the device and adding an event listener to "disconnect" by the following:

    navigator.usb.requestDevice({ filters: [{ vendorId: ... ... ...}] })
    .then(device=> {
            navigator.usb.addEventListener('disconnect', device => {
                    printToScreen("Message", "Device disconnect!!! ");
            });
            return device.open();
    })

FYI: The printToScreen() method adds text to a div tag.

The device dose connect and I can see the device permission listed when i click the lock icon in the address bar, and i can retrieve the Landing Page URL no problem. when I disconnect the device the device permission listed is removed (which is normal because the device dose not have a Serial Number, thus the permission granted by the user can'not be persisted) but the disconnect event dose not appear? not sure what I am doing wrong.

So my question is, how can I detect/get the callback from the disconnect event? or is this a bug and I should file it? any help would be appreciated, thank you.

As well, after adding the Serial Number to the device, I get a "NotFoundError: Device unavailable" when I try to open the device with "return device.open()", but the "navigator.usb.requestDevice" returns the correct device.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user1
  • 599
  • 1
  • 3
  • 20

1 Answers1

2

I have just filed issue 737321 to track this problem. It is a failure mode which was worked around in the chrome.usb API but slipped through in WebUSB because reproducing it depends on the order of initialization of internal Chrome components.

Update: This fix will be available in Chrome 61.0.3144.0.

Reilly Grant
  • 5,590
  • 1
  • 13
  • 23