I have been working in Java for several years but have never had a need to directly access a USB device. I am starting a side project working with Bluetooth USB dongles. The goal is to scan for a USB Bluetooth dongles and make them behave like a headphone/speaker or heart rate monitor. I am told that this part is possible.
My research for a USB api in Java lead me to usb4java. I am using an Eclipse environment and OpenJDK 11 on Windows 10. After reading the documentation I am able to get access to the usb4java classes and I got the .dll file included in the class path. To familiarize my self with the code I copied a couple different examples from the site. The issue I run into is a EXCEPTION_ACCESS_VIOLATION, see the attached hs_er_pid file. From what I can tell it is not an issue with my configuration in Eclipse. I have spent most of the day trying to figure out how to resolve this issue without any luck. Any help would be greatly appreciated.
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 Main {
static {
System.load("C:/Eclipse_Workspace/BlueToothSimulator/lib/native/org/usb4java/win32-x86-64/libusb4java.dll");
}
public static void main(String[] args) {
// Create the libusb context
Context context = new Context();
// Initialize the libusb context
int result = LibUsb.init(context);
if (result < 0) {
throw new LibUsbException("Unable to initialize libusb", result);
}
// Read the USB device list
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(context, list);
if (result < 0) {
throw new LibUsbException("Unable to get deice list", result);
}
try {
// Iterate over all devices and list them
for (Device device : list) {
int address = LibUsb.getDeviceAddress(device);
int bussNumber = LibUsb.getBusNumber(device);
DeviceDescriptor deviceDescriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, deviceDescriptor);
if (result < 0) {
throw new LibUsbException("Unbale to read device descriptor", result);
}
System.out.format("Bus %03d, Device %03d, Vendor %04x, Product %04x%n",
bussNumber,
address,
deviceDescriptor.idVendor(),
deviceDescriptor.idProduct());
}
} finally {
LibUsb.freeDeviceList(list, true);
}
LibUsb.exit(context);
}
}