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
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".
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments