在java中识别新设备 [英] Identify a new device in java
问题描述
我想知道连接新设备的时间。
I want to know when a new device is connected.
我了解到可以使用以下方式获取C#中的所有当前设备:
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)");
然后 searcher.get()
返回这些设备的集合。
and then searcher.get()
returns a collection of the devices.
我可以在java中使用与ManagementObjectSearcher并行的东西吗?
如果没有,是否有另一种获取连接设备列表的方法?或者找出新设备何时连接的其他方法?
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?
谢谢。
推荐答案
我知道在Java中没有真正的跨平台方式。如果您对仅支持Windows的解决方案感到满意,最简单的解决方案是生成一个C#/ VBScript可执行文件,您可以使用 Runtime.exec()
从Java程序调用该文件。
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()
.
如果你想要一个更好的解决方案需要更多工作(但仍然只有Windows)你可以使用 iKVM ,用于连接C#和Java程序。
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.
我唯一能想到的就是从跨平台的角度来看(这仍然使用本机库是使用RXTX轮询COM端口上的枚举,并检查什么时候发生了变化。这不会让你得到所有设备的列表,它只会工作对于使用COM端口的设备,但根据您的需要,它可能就足够了。这种方法的优点是RXTX二进制文件可以自由存在于每个公共平台。
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.
这篇关于在java中识别新设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!