简体   繁体   English

只需获取连接到系统的USB设备的名称?

[英]just get the names of USB devices attached to a system?

Aren't there any system calls or OS specific functions that can be called by Java to get just the names of the USB devices attached? Java是否可以调用任何系统调用或操作系统特定的函数来获取所连接的USB设备的名称?

I've seen probably 6-7 questions here only, but everyone mentions the C++ functions GetRawInputDeviceList() etc, and they are not cross-platform compliant. 我在这里只看到了6到7个问题,但是每个人都提到了C ++函数GetRawInputDeviceList()等,它们并不是跨平台兼容的。 Either for Windows in C# or C++ or for Linux only. 适用于C#或C ++中的Windows或仅适用于Linux。

But I'm working in Java. 但我在Java工作。 Also, this needs to be cross platform. 此外,这需要跨平台。 Atleast, it needs to be working for Windows,Linux and Mac. 至少,它需要适用于Windows,Linux和Mac。 I can work with terminal/shell/command-prompt commands also. 我也可以使用terminal / shell / command-prompt命令。 I guess I can run them with Java. 我想我可以用Java运行它们。

I've tried getFileSystemView and listRoots . 我试过getFileSystemViewlistRoots But they give out names of all drives [dvd, hdd partitions,floppy etc]. 但是他们给出了所有驱动器的名称[dvd,hdd分区,软盘等]。

I need to get only USB devices. 我只需要USB设备。

Please don't mention jUSB or JSR080. 请不要提及jUSB或JSR080。 Why: 为什么:

jUSB: access to USB devices currently requires that they be connected to a GNU/Linux host system jUSB: 访问USB设备目前要求它们连接到GNU / Linux主机系统

javax.usb: pre-alpha Windows implementation is not certified and requires a kernel driver. javax.usb: pre-alpha Windows实现未经过认证,需要内核驱动程序。

usb4java: basically, it just implements JSR80 with little more abstraction, perhaps usb4java: 基本上,它只是实现了JSR80而不是更多的抽象

Although to be honest I haven't tried libusb since it is in C++. 虽然老实说我还没有尝试过libusb,因为它是用C ++编写的。

If you are going to mention APIs, mention completely tested and tried ones, that work across Linux,Windows and Mac. 如果您要提及API,请提及完全经过测试和尝试过的API,这些API可以在Linux,Windows和Mac上运行。 If that wasn't the case, I wouldn't have put this question up. 如果情况并非如此,我不会提出这个问题。 I've seen the mention of jUSB, javax.usb, etc on many other posts. 我在很多其他帖子上看到了jUSB,javax.usb等的提及。

You can use the jUsb API , for Linux. 您可以使用适用于Linux的jUsb API

Or you could launch the terminal in Linux using the Process class, and run ls -la /dev/disk/by-id/usb-* and catch the stdout to know the results. 或者您可以使用Process类在Linux中启动terminal ,并运行ls -la /dev/disk/by-id/usb-*并捕获stdout以了解结果。

For Windows, you can try this : How to find my USB flash drive's path with PowerShell 对于Windows,您可以尝试: 如何使用PowerShell查找USB闪存驱动器的路径

EDIT: 编辑:

For Windows, another helpful utility is the devcon.exe . 对于Windows,另一个有用的实用程序是devcon.exe

For more info, check this. 有关详细信息,请检查信息

EDIT 2: For Mac, you could launch the terminal using the Process class, and run system_profiler SPUSBDataType 编辑2:对于Mac,您可以使用Process类启动terminal ,并运行system_profiler SPUSBDataType

Yoy can try javahidapi . 你可以试试javahidapi I think it some c/c++ code and JNI. 我认为它是一些c / c ++代码和JNI。 Declarated linux, mac and windows support. 声明了linux,mac和windows支持。 I have tried it with linux (ok), with clean windows in virtual box (not ok, UnsatisfiedLinkError, i think some MSVS libs was missed). 我已经尝试过用linux(ok),在虚拟框中使用干净的窗口(不好,UnsatisfiedLinkError,我认为有些MSVS库已经错过了)。 If you'll compile it from source, it should work, i belive. 如果你将从源代码编译它,它应该工作,我相信。

here is example: 这是一个例子:

import com.codeminders.hidapi.HIDDeviceInfo;
import com.codeminders.hidapi.HIDManager;

public class TestHid {

    public static void main(String[] args) throws Exception {
        try {
            com.codeminders.hidapi.ClassPathLibraryLoader.loadNativeHIDLibrary();
            HIDManager hidManager = HIDManager.getInstance();
            HIDDeviceInfo[] infos = hidManager.listDevices();
            for (HIDDeviceInfo info : infos) {
                System.out.println("info: " + info.toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

EDIT output shows only one plugged in usb device, genius laser mouse. EDIT输出显示只有一个插入usb设备,天才激光鼠标。

[grigory@gr testRSA]$ pwd
/home/grigory/testRSA/out/production/testRSA
[grigory@gr testRSA]$ whoami 
grigory
[grigory@gr testRSA]$ java -cp ".:hidapi-1.1.jar" Test
libusb couldn't open USB device /dev/bus/usb/003/002: Permission denied.
libusb requires write access to USB device nodes.
info:HIDDeviceInfo [path=0003:0002:00, vendor_id=1112, product_id=58, serial_number=null, release_number=0, manufacturer_string=null, product_string=null, usage_page=0, usage=0, interface_number=0]
[grigory@gr testRSA]$ sudo java -cp ".:hidapi-1.1.jar" Test
[sudo] password for grigory: 
info:HIDDeviceInfo [path=0003:0002:00, vendor_id=1112, product_id=58, serial_number=null, release_number=0, manufacturer_string=Genius, product_string=Laser Mouse, usage_page=0, usage=0, interface_number=0]
[grigory@gr testRSA]$ 

and for fresh Windows XP it isn't work (only one windows i can find. I haven't Visual Studio for compile lib from source): 对于新的Windows XP它是行不通的(我只能找到一个窗口。我没有从源代码编译lib的Visual Studio):

E:\testRSA\out\production\testRSA>java -cp ".;hidapi-1.1.jar" -Djava.library.pat
h="e:\testRSA\out\production\testRSA" Test
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.codeminders.hidap
i.HIDManager.init()V
        at com.codeminders.hidapi.HIDManager.init(Native Method)
        at com.codeminders.hidapi.HIDManager.<init>(HIDManager.java:53)
        at com.codeminders.hidapi.HIDManager.getInstance(HIDManager.java:121)
        at Test.main(Test.java:14)

Maybe things have improved since you first asked this question. 也许自从你第一次提出这个问题以来情况有所改善 I recently began exploring usb4java on the Mac, and it seems to work. 我最近开始在Mac上探索usb4java ,它似乎有效。 There is example code available, both for the low-level (libusb-like) API, and the high-level (javax) API. 有一些示例代码可用于低级(类libusb)API和高级(javax)API。

To list all USB-devices, look the examples . 要列出所有USB设备,请查看示例

I downloaded all libraries directly from usb4java.org and the examples from github. 我直接从usb4java.org下载了所有库,并从github下载了这些例子。 I did not manage to get the maven build working, but I could import the libraries and examples in Eclipse and run them all. 我没有设法让maven构建工作,但我可以在Eclipse中导入库和示例并运行它们。

There are same native code included in usb4java, but the library wraps them all quite beautifully and hides all the messy details, only extracting and deploying the native code at demand. usb4java中包含相同的本机代码,但是库将它们完美地包装起来并隐藏所有杂乱的细节,仅在需要时提取和部署本机代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM