我正在尝试使用 webusb api 从网页连接 USB 设备,但我无法使用以下代码打开配对设备。
<!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>
它的节目
DOMException Access denied 配对后无法打开usb