我正在编写一个检测usb设备的java应用程序。当我执行程序时,它实际上显示了pc中的所有usb设备,但我想要连接的usb设备,这只是我的java代码:
package com.lakshman.sundeep;
import org.usb4java.Context;
import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;
public class UTMusb
{
public static int vid = 6790;
public static int pid = 29987;
public static void main(String[] args)
{
Context context = new Context();
int result = LibUsb.init(context);
if(result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to initialize the usb device",result);
}
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(null, list);
if(result < 0 )throw new LibUsbException("Unable to get device list",result);
try
{
for(Device device : list)
{
DeviceDescriptor device_descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, device_descriptor);
if(result != LibUsb.SUCCESS)throw new LibUsbException("Unable to get device descriptor : ",result);
System.out.println("Product id is : "+device_descriptor.idProduct()+" "+"Vendor id is : "+device_descriptor.idVendor());
if(device_descriptor.idProduct()==pid && device_descriptor.idVendor()==vid)
{
System.out.println("Product id and vendor id was matched");
}
else
{
System.out.println("Product id and vendor id was not matched");
}
}
}
finally
{
LibUsb.freeDeviceList(list, true);
}
}
}
这是我的输出:
Product id is : -29658 Vendor id is : -32634
Product id and vendor id was not matched
Product id is : -29651 Vendor id is : -32634
Product id and vendor id was not matched
Product id is : -16294 Vendor id is : 1133
Product id and vendor id was not matched
Product id is : -15587 Vendor id is : 1133
Product id and vendor id was not matched
Product id is : 29987 Vendor id is : 6790
Product id and vendor id was matched
Product id is : -32768 Vendor id is : -32633
Product id and vendor id was not matched
Product id is : -32760 Vendor id is : -32633
Product id and vendor id was not matched
Product id is : -29647 Vendor id is : -32634
Product id and vendor id was not matched
答案 0 :(得分:0)
您可以使用http://usb4java.org
这段代码:
Context context = new Context();
int result = LibUsb.init(context);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to initialize libusb.", result);
}
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(null, list);
if (result < 0) throw new LibUsbException("Unable to get device list", result);
try {
// Iterate over all devices and scan for the right one
for (Device device: list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, descriptor);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to read device descriptor", result);
System.out.println(descriptor.idVendor()+" "+descriptor.idProduct());
}
} finally {
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, true);
}
答案 1 :(得分:0)
您可以尝试打开设备来测试设备是否已连接:
DeviceHandle handle = new DeviceHandle();
result = LibUsb.open(device, handle);
if (result < 0) {
System.out.println(String.format("Unable to open device: %s. "
+ "Continuing without device handle.",
LibUsb.strError(result)));
handle = null;
}