Answer the question
In order to leave comments, you need to log in
How to fix error: Exception in thread "main" org.usb4java.LibUsbException: USB error 6: Unable to claim interface: Resource busy?
Here is the code for line 121.
I want to connect to the camera.
res = LibUsb.releaseInterface(handle, INTERFACE);
if (res != LibUsb.SUCCESS) throw new LibUsbException("Unable to release interface", res);
package com.company;
import org.usb4java.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
public class Main {
private static final byte[] CONNECT_HEADER = new byte[] { 0x43, 0x4E, 0x58,
0x4E, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x17, 0x00, 0x00,
0x00, 0x42, 0x06, 0x00, 0x00, (byte) 0xBC, (byte) 0xB1, (byte) 0xA7,
(byte) 0xB1 };
private static final byte[] CONNECT_BODY = new byte[] { 0x68, 0x6F, 0x73,
0x74, 0x3A, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x3A, 0x41,
0x44, 0x42, 0x20, 0x44, 0x65, 0x6D, 0x6F, 0x00 };
private static final byte INTERFACE = 1;
private static final int IN_ENDPOINT = 162;
private static final int OUT_ENDPOINT = 34;
private static final int TIMEOUT = 5000;
public static void write(DeviceHandle handle, byte[] data)
{
ByteBuffer buffer = BufferUtils.allocateByteBuffer(data.length);
buffer.put(data);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(handle, (byte) OUT_ENDPOINT, buffer,
transferred, TIMEOUT);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to send data", result);
}
System.out.println(transferred.get() + " bytes sent to device");
}
public static ByteBuffer read(DeviceHandle handle, int size)
{
ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(
ByteOrder.LITTLE_ENDIAN);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(handle, (byte) IN_ENDPOINT, buffer,
transferred, TIMEOUT);
if (result != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to read data", result);
}
System.out.println(transferred.get() + " bytes read from device");
return buffer;
}
public static void main(String[] args) throws Exception {
Context context = new Context();
final UVCCamera camera = new UVCCamera();
DeviceHandle handle = new DeviceHandle();
int res = LibUsb.init(context);
if (res < 0) {
throw new LibUsbException("Unable to initialize libusb", res);
}
DeviceList list = new DeviceList();
res = LibUsb.getDeviceList(context, list);
if (res < 0) {
throw new LibUsbException("Unable to get device list", res);
}
try {
for (Device device: list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
res = LibUsb.getDeviceDescriptor(device, descriptor);
if (res < 0) {
throw new LibUsbException("Unable to read device descriptor", res);
}
if((descriptor.bDeviceClass() & 255) == 239 && (descriptor.bDeviceSubClass() & 255) == 2){
System.out.println("FINDED");
short VID = descriptor.idVendor();
short PID = descriptor.idProduct();
System.out.println(VID);
System.out.println(PID);
handle = LibUsb.openDeviceWithVidPid(context, VID, PID);
if (handle == null) System.err.println("Test device not found.");
res = LibUsb.claimInterface(handle, INTERFACE);
if (res != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to claim interface", res);
}
System.out.println(res);
write(handle, CONNECT_HEADER);
write(handle, CONNECT_BODY);
ByteBuffer header = read(handle, 24);
header.position(12);
int dataSize = header.asIntBuffer().get();
// Receive the body of the ADB answer
@SuppressWarnings("unused")
ByteBuffer data = read(handle, dataSize);
// Release the ADB interface
res = LibUsb.releaseInterface(handle, INTERFACE);
if (res != LibUsb.SUCCESS)
{
throw new LibUsbException("Unable to release interface", res);
}
LibUsb.close(handle);
LibUsb.exit(null);
}
}
} finally {
LibUsb.freeDeviceList(list, true);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question