98
How to request and detect the USB Devices (Experimental)
2021-06-10 - JavaScript
1// JS Tip of the Day
2// How to request and detect the USB Devices (Experimental)
3
4// Request a Device
5const filters = {filters: []}; // You could filter by vendorId/productId
6
7await navigator.usb.requestDevice(filters)
8.then(device => console.log(`Device: ${device.productName}`))
9.catch(e => console.log(`No Device: ${e}`));
10
11// Get all a paired attached devices
12await navigator.usb.getDevices()
13.then(devices => {
14 console.log(`Devices: ${devices.length}`);
15 devices.forEach(device => console.log(`Device: ${device.productName}`));
16});
17
18// NB: Yes...I used top level await! :D
#JStipoftheday