Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a USB device is plugged in Javascript from browser

Is there Javascript based mechanism with which i can detect, through a browser, that a user has a specific usb device plugged in?

like image 216
Aziz Avatar asked Oct 20 '25 01:10

Aziz


1 Answers

For security reasons, JavaScript in web browsers provides only restricted access to computer resources. It is not possible to store files in an arbitrary folder, start applications or communicate with a USB device.

But there are some exceptions :

  • In legacy browsers such as IE6 ~ IE8 you can use ActiveX objects or Java applets to achieve this .
  • Use a chrome.usb API .
  • Write a browser plugin using NPAPI , (Here, it is not for long though)

2023 Edit

The USB interface of the WebUSB API provides attributes and methods for finding and connecting USB devices from a web page.

( browser support is still limited, for more information refer to browser compatibility table )

Example code:

navigator.usb.getDevices().then((devices) => {
  console.log(`Total devices: ${devices.length}`);
  devices.forEach((device) => {
    console.log(
      `Product name: ${device.productName}, serial number ${device.serialNumber}`
    );
  });
});
like image 128
Alexander Avatar answered Oct 21 '25 17:10

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!