Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active February 29, 2016 06:13
Show Gist options
  • Save tresf/c0d596a4891b430eb29a to your computer and use it in GitHub Desktop.
Save tresf/c0d596a4891b430eb29a to your computer and use it in GitHub Desktop.
/**
* Set specified configuration as active for given device using low-level api
*/
public static void setActiveConfiguration(UsbDevice device, UsbConfiguration cfg) throws LibUsbException {
DeviceList list = new DeviceList();
LibUsb.getDeviceList(null, list);
try {
for (Device d : list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
LibUsb.getDeviceDescriptor(d, descriptor);
if (descriptor.idVendor() == device.getUsbDeviceDescriptor().idVendor() &&
descriptor.idProduct() == device.getUsbDeviceDescriptor().idProduct()) {
Context context = new Context();
LibUsb.init(context);
DeviceHandle handle = new DeviceHandle();
LibUsb.open(d, handle);
LibUsb.setConfiguration(handle, cfg.getUsbConfigurationDescriptor().bConfigurationValue());
LibUsb.close(handle);
LibUsb.exit(context);
return;
}
}
} finally { LibUsb.freeDeviceList(list, true); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment