javawindowsusblibusbusb4java

Java usb4java reading from usb device on windows 10 platform


I am trying to read a message from RFID reader connected via USB to windows 10pro machine with usb4java library.

I have managed to claim the interface, opened pipe and registered listener for the data, however the listener is never triggered. The reader acts as keyboard and whatever it reads ends up in active application, such as IDE i have open, instead of in listener.

UsbInterface usbInterface = activeInteface(device);
// there is only one endpoint in the list
UsbEndpoint endpoint = (UsbEndpoint)usbInterface.getUsbEndpoints().get(0);
UsbPipe pipe = endpoint.getUsbPipe();
try {
     usbInterface.claim();
     // true
     System.out.println("claimed usb interface: " + usbInterface.isClaimed());
     pipe.open();
     // true
     System.out.println("pipe open: " + pipe.isOpen());
     pipe.addUsbPipeListener(new MessageListener());
     // true
     System.out.println("pipe active: " + pipe.isActive());
     // keep main thread alive, async call should be done from another thread i guess
     Thread.sleep(15000);
    }
   catch (Exception any) {System.out.println(any);}
 }

And the listener:

private static class MessageListener implements UsbPipeListener {

    @Override
    public void errorEventOccurred(UsbPipeErrorEvent event) {
        System.out.println(event.toString() + " , " +event.getUsbException());
    }

    @Override
    public void dataEventOccurred(UsbPipeDataEvent event) {
        // this code block never triggers
        System.out.println("listener ...);
        int actualLength = event.getActualLength();
        System.out.println("length: " + actualLength);
        byte[] data = event.getData();
        System.out.println("data length " + data.length);
    }
}

i have also tried synchronous read instead of asynchronous in the block above, like this:

byte[] buffer = new data[8];
// this fails on its own, don't even need to read something with RFID reader
int received = pipe.syncSubmit(buffer);

fails with:

USB error1: Transfer error on interrupt endpoint: Input/Output error

There is some windows specific property that library supports: org.usb4java.javax.useUSBDK = true but this fails when i try to set it with an exception.

I have 0 experience with USB devices so not sure how to proceed from here. Is there something wrong with the code, do i need USBDK or device does not support libUSB driver ? Sadly this is not my device and i don't have access to documentation of the device so cannot be sure if it is device driver issue.


Solution

  • I know that this is 2 years old, but i've had similar issue and this was one of the first questions that i ran into looking for solution, which took me hours.

    So, basically, windows doesn't let to read/write keyboard devices directly, to do so, you have to override it's driver (That's why you're getting Input/Output error, and it's written in the hid4java's FAQ).

    First way to override device driver is described in libusb wiki. As far as i know you would have to install a new driver every time you connect the device to a new USB port, which is why i recommend you to read further.

    Second way is what you've already mentioned, which is using UsbDk (Usb Drivers Development Kit for Windows). It makes the device accessible for you by detaching the kernel driver and reattaching it back after you're done playing with it.

    In order to use it, you need to do two things:

    1. Set the org.usb4java.javax.useUSBDK = true in you javax.usb.properties file as stated in the manual (this can also be done manually in low-level usb4java, see OPTION_USE_USBDK and setOption(Context, int)).
    2. Download and install UsbDk on your system (simplest way is to download x64 or x86 version msi installer which has GUI and is fully automated), which is sadly not in the manual (maybe it's obvious for some people, but took me amount of time that i am not proud of to realize).

    Im guessing that the lack of second step is why OP has been getting an exception.

    Hope that this will help someone, knowing all this two days ago would save me a lot of headache.