admin 管理员组文章数量: 1086019
I'm trying the new Chrome WebUSB API, but can't see any connected device.
Tried this for example, with different USB devices connected to my Windows 7 PC:
<html>
<body>
<button onclick="myFunction()">Click me</button>
<script>
function myFunction() {
console.log('Clicked');
navigator.usb.getDevices()
.then(devices => {
devices.map(device => {
console.log('Device:');
console.log(device.productName);
console.log(device.manufacturerName);
});
});
}
</script>
</body>
</html>
But got no device.
What am I doing wrong? Should it work with any device?
I'm trying the new Chrome WebUSB API, but can't see any connected device.
Tried this for example, with different USB devices connected to my Windows 7 PC:
<html>
<body>
<button onclick="myFunction()">Click me</button>
<script>
function myFunction() {
console.log('Clicked');
navigator.usb.getDevices()
.then(devices => {
devices.map(device => {
console.log('Device:');
console.log(device.productName);
console.log(device.manufacturerName);
});
});
}
</script>
</body>
</html>
But got no device.
What am I doing wrong? Should it work with any device?
Share Improve this question edited Aug 21, 2019 at 16:43 BenMorel 36.7k52 gold badges205 silver badges337 bronze badges asked Sep 8, 2017 at 20:59 GuyGuy 1,5971 gold badge19 silver badges30 bronze badges1 Answer
Reset to default 6Until your page has requested permission to access a device navigator.usb.getDevices()
will return an empty list. Inside your onclick
handler call navigator.usb.requestDevice()
instead with a filter selecting the vendor and product IDs of the devices you would like to support. See the example from the specification:
let button = document.getElementById('request-device');
button.addEventListener('click', async () => {
let device;
try {
device = await navigator.usb.requestDevice({ filters: [{
vendorId: 0xABCD,
classCode: 0xFF, // vendor-specific
protocolCode: 0x01
}]});
} catch () {
// No device was selected.
}
if (device !== undefined) {
// Add |device| to the UI.
}
});
本文标签: javascriptChrome WebUSB API returns no device when using navigatorusbgetDevices()Stack Overflow
版权声明:本文标题:javascript - Chrome WebUSB API returns no device when using navigator.usb.getDevices() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743992336a2514961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论