在java中识别一个新设备
我想知道新设备何时连接。
我了解到,可以使用 C# 获取所有当前设备:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select VolumeName, DeviceID from Win32_LogicalDisk Where Not (VolumeName is null)");
然后 searcher.get()
返回设备的集合。
有没有与 ManagementObjectSearcher 类似的东西可以在 java 中使用? 如果没有,是否有其他方法获取已连接设备的列表?或者另一种方式来了解何时连接新设备?
谢谢。
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.
我知道在 Java 中没有真正的跨平台方法来做到这一点。如果您对仅限 Windows 的解决方案感到满意,最简单的解决方案是生成一个 C# / VBScript 可执行文件,您可以使用
Runtime.exec()
从 Java 程序调用。如果您想要一个“更好”的解决方案,需要更多的工作(但仍然仅限 Windows),您可以使用 iKVM 用于 C# 和 Java 程序之间的接口。
从跨平台的角度来看,我唯一能想到的(这仍然使用本机库是使用 RXTX 轮询 COM 端口上的枚举,并检查何时发生变化。这不会为您提供所有设备的列表,并且它仅适用于使用 COM 端口的设备,但根据您的需要,这种方法的优点是 RXTX 二进制文件可以自由地存在于每个通用平台上。
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.