我是 Node.js 的新手。我想检测是否有任何 USB/大容量存储设备连接到系统。
因为我们在 C# 中有一些事情要做,比如
// Add USB plugged event watching
watcherAttach = new ManagementEventWatcher();
watcherAttach.EventArrived += watcherAttach_EventArrived;
watcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcherAttach.Start();
// Add USB unplugged event watching
watcherDetach = new ManagementEventWatcher();
watcherDetach.EventArrived += watcherDetach_EventArrived;
watcherDetach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
watcherDetach.Start();
请建议我们如何在 Node.js 中做类似 C# 的事情。
最佳答案
更好的解决方案是使用“usb-detection”https://www.npmjs.com/package/usb-detection
您可以通过 productId 或 vendorId 过滤监听特定的 USB 设备:
// Detect add or remove (change)
usbDetect.on('change', function(device) { console.log('change', device); });
usbDetect.on('change:vid', function(device) { console.log('change', device); });
usbDetect.on('change:vid:pid', function(device) { console.log('change', device); });
// Get a list of USB devices on your system, optionally filtered by `vid` or `pid`
usbDetect.find(function(err, devices) { console.log('find', devices, err); });
usbDetect.find(vid, function(err, devices) { console.log('find', devices, err); });
usbDetect.find(vid, pid, function(err, devices) { console.log('find', devices, err); });
// Promise version of `find`:
usbDetect.find().then(function(devices) { console.log(devices); }).catch(function(err) { console.log(err); });
关于javascript - 如何检测使用 Node.js 连接的 USB 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843946/