|
@@ -0,0 +1,42 @@
|
|
|
+/*
|
|
|
+ * To change this license header, choose License Headers in Project Properties.
|
|
|
+ * To change this template file, choose Tools | Templates
|
|
|
+ * and open the template in the editor.
|
|
|
+ */
|
|
|
+package project82usb;
|
|
|
+
|
|
|
+import org.usb4java.Context;
|
|
|
+import org.usb4java.Device;
|
|
|
+import org.usb4java.DeviceDescriptor;
|
|
|
+import org.usb4java.DeviceList;
|
|
|
+import org.usb4java.LibUsb;
|
|
|
+import org.usb4java.LibUsbException;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author ilysk
|
|
|
+ */
|
|
|
+public class getUSBDevicesId {
|
|
|
+ public static void tryusb4java() {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|