3

I am trying to connect usb device from webpage using webusb api ,but i cannot open the paired device using the below code.

<!DOCTYPE html>
<html>
<head allow="usb"></head>
    <body>
   <input type="submit" onclick="connect()" value="connect"/>

    <script>
    var device;
    function setup(device) {
        alert(device.productName+" open");
        return device.open()

        .then(() => device.selectConfiguration(1))
        .then(() => device.claimInterface(0))
    }

    function connect() {
        if (device == null) {
            navigator.usb.requestDevice({ filters: [{ vendorId : 2352 }] })
            .then(selectedDevice => {
                device = selectedDevice;
                console.log(device);
                return setup(device);
            })

            .catch(error => { console.log(error); })
        }


    }
    navigator.usb.getDevices()
    .then(devices => {
        if (devices.length > 0) {
            device = devices[0];
            return setup(device);
        }
    })
    .catch(error => { console.log(error); });
    </script>
    </body>
</html>

Its shows

DOMException Access denied cannot open usb after paired

Shijo Joseph
  • 95
  • 2
  • 8
  • 1
    Possible duplicate of [Webusb: Access Denied trying to open printer on Windows](https://stackoverflow.com/questions/47143148/webusb-access-denied-trying-to-open-printer-on-windows) – Andreas Dec 05 '17 at 09:54

1 Answers1

3

Based on the title of this question it appears that you are running on Linux and that the permissions for the device node /dev/bus/usb/001/007 are not set up so that the user running Chrome can open it.

What you need to do is add a udev rule that will set the permissions for this device node so that it can be opened. First you need to figure out the vendor and product IDs for your device. If you run lsusb it will list the devices on your system in a format like this,

Bus BBB Device NNN: ID VVVV:PPPP Manufacturer Product

Where,

BBB: The bus number (usually one per controller, two for USB 3.0 controllers).
NNN: The device number on that bus.
VVVV: The vendor ID (in hexadecimal).
PPPP: The product ID (in hexadecimal).

Once you know this information you can create a file in /etc/udev/rules.d/ containing the line below after plugging in the IDs you discovered in the step above.

SUBSYSTEM=="usb", ATTRS{idVendor}=="VVVV", ATTR{idProduct}=="PPPP", MODE="0660", GROUP="plugdev"

This will make any device with the given vendor and product IDs accessible to users in the plugdev group. This is a vaguely appropriate group for removable device permissions according to the Debian documentation.

From your code already appear to know the vendor ID, 2352, which would be entered into the rule in hexadecimal as "0930".

Reilly Grant
  • 5,590
  • 1
  • 13
  • 23
  • Thank you Reilly Grant, Its works properly but I cannot claim the usb interface it shows the error ([11:33:42] Failed to claim interface 0: Device or resource busy (16)) – Shijo Joseph Dec 07 '17 at 06:10
  • There are many answers about this error on Stack Overflow however with WebUSB the option to detach the existing kernel driver is not available through the API for security reasons. If you can modify the system settings so that the other driver is not loaded or manually unbind it then you should be able to claim the interface. – Reilly Grant Dec 07 '17 at 17:44
  • I unbind the USB. After that i tried to claim the interface then i got the error like. `"Failed to claim interface 0: No such file or Directory (2)"` . When I restarted the chrome the OS again bind the USB by default and I got the error `Failed to claim interface 0: Device or resource busy (16)` . Is there any solution? – Shijo Joseph Dec 08 '17 at 06:36
  • I found your answer for the question [link](https://stackoverflow.com/questions/45520008/google-chrome-webusb-api-error-while-trying-to-claim-interface) and I got the same result that the person asked. After unbind the device I execute the command `/sys/bus/usb/devices/1-2$ cat bConfigurationValue` but i didn't get any value and the same for `bInterfaceNumber`. How to resolve this problem? – Shijo Joseph Dec 08 '17 at 10:39