I want to know when a new device is connected.
I learned that it is possible to get all the current devices in C# using:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select VolumeName, DeviceID from Win32_LogicalDisk Where Not (VolumeName is null)");
and then searcher.get()
returns a collection of the devices.
Is there something parallel to ManagementObjectSearcher that i can use in java? If not, is there another way to get the list of connected devices? or another way to find out when a new device is being connected?
Thanks.
I know of no real cross platform way to do this in Java. If you're happy with a Windows only solution, the simplest solution would be to generate a C# / VBScript executable you can call from your Java program with Runtime.exec()
.
If you want a "nicer" solution that takes a bit more work (but still only Windows) you could use iKVM to interface between your C# and Java programs.
The only thing I can think of from a cross platform perspective (and this still uses native libraries is to use RXTX to poll an enumerate on the COM ports, and check when something changes. This won't get you a list of all the devices, and it will only work for devices that use a COM port, but depending on your needs it may be enough. The plus side with this approach is that RXTX binaries freely exist for every common platform out there.
来源:https://stackoverflow.com/questions/5847376/identify-a-new-device-in-java