Aziz
Aziz

Reputation: 1614

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?

Upvotes: 4

Views: 9798

Answers (2)

Alexander
Alexander

Reputation: 12795

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 :

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}`
    );
  });
});

Upvotes: 6

bhspencer
bhspencer

Reputation: 13570

Your javascript code is sand boxed and therefore cannot access system resources such as usb devices.

I think the only way to access the USB devices would be to write a browser plugin. How to get USB devices List from Browser

Upvotes: 2

Related Questions