Identify a new device in java

风格不统一 提交于 2019-12-07 18:29:53

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!