Java Usb communication Usb4Java (Low Api)

Click here, if you are looking for usb4java high level api

I’m recently developing an application that has to communicate with a POS thermal printer and after a long research and some trials decided best way would be to send commands through usb port. Unfortunately I don’t have a POS printer right now but I managed to claim a USB device (GamePad) with the help of Usb4Java

  • I’m posting their example code because seems to be confusing for some people at first (including me)
  • The code is briefly explained at their website Low Level Api
  • If your device cannot be claimed, install libusb’s own driver with Zadig
  • You will need to find the right end point to communicate, the easiest way is to pop a usb with ubuntu on it, then in terminal use “lsusb” command
  • This will give you a list of usb devices with their vendors and produkt numbers
  • Note the vendor and product number of device you want and use “lsusb -v -d vendor_nr:product_nr” to get your devices endpoint

  • Gede

    Thanks for your post, this really help me. You save my time.
    By the way, how to determine product id and vendor id.
    in your code
    findDevice(context, (short) (0x046d), (short) (0xc077));

    how to know 0x046d and 0xc077 is correct ??
    or this number is same for all device.

    Thanks for your help.

  • lakshmansundeep

    I am using your sending data code and it will showing an error called “USB error 12: Unable to open the usb device: Operation not supported or unimplemented on this platform”

    • metin

      since you are trying to send data, I guess you already have claimed the device. If yes, check the interface number you are using to send data.
      If no check your installed driver or re-install it, this could also happen when trying to get a property which is not implemented in that driver.
      Like trying to get the name of the device, it might not be implemented in that driver but an updated driver has it.
      Also if another programm is using the printer you can’t claim it.

  • rajen

    Hi Metin,

    I am novice at dealing with printer programming and driver communication.Kindly help me for some of below mentioned doubts.

    1)I want to send some of data(ticket data) to printer to print.How to find which USB is associated with printer as it changes by customers in real environments.?

    2)I can list out all attached USB vendor id and product id details.How to proceed for trigger particular printer.?

    Looking for get to know more about workflow .


    • metin

      Hi rajen,
      Since you have the list of id and products you can send data to a particular printer. In this post to send data we go through some steps, one them is entering the id and vendor, which means we already have a destination.
      Another important thing to know is the end point, there is a certain endpoint the printer uses to send data that is also entered in one of steps.
      You don’t need to know in which usb the printer is connected, knowing that is connected its enough.

      Here is the workflow in this post
      1. get the device list
      2. choose device (id and vendor)
      3. claim the device
      4. send data

  • Rinku

    Hello Metin
    I got following output

    Bus 002, Device 003: Vendor 1203, Product 0128
    Device Found
    Device Claimed
    8 bytes sent
    but the TSC-TDP 247 printer did not print anything,Could you please help me with this ?

    • metin

      You have to send bytes that the printer will understand 🙂
      I updated the send method, so that it will send the commands instead of some random bytes. Havent tested it yet. Hope it works

  • Rinku

    Hello Metin

    Thanks for your help .
    but still its showing following error
    Bus 002, Device 001: Vendor 8086, Product 8c2d
    Bus 001, Device 001: Vendor 8086, Product 8c26
    Bus 002, Device 003: Vendor 0bda, Product 0129
    Bus 002, Device 004: Vendor 1203, Product 0128
    Device Found
    Device Claimed
    Exception in thread “main” java.lang.IllegalArgumentException: data must be a direct buffer
    at org.usb4java.LibUsb.controlTransfer(Native Method)
    at detectUSB.DetectUSB.sendData(
    at detectUSB.DetectUSB.claimDevice(
    at detectUSB.DetectUSB.getDeviceHandle(
    at detectUSB.DetectUSB.findDevice(
    at detectUSB.DetectUSB.main(
    How to find what kind of byte particular printer will understand ?

    • metin

      Ah ok. I have another implementation it is a bit different but same library, I will update the whole post.

  • Rinku

    Hello Metin

    I appreciate your help,Could please tell me ,when you will get chance to update the post ?

    • metin

      in 2 or 3 days

  • tanvi

    can i find data transfer history in java.example pendrive to pc and Bluetooth to pc data transfer history?
    Thank you

    • metin

      You will need to log and persist that log of the transferred data in your java program. I don’t know if there is a history of data transfer in windows, if there is maybe you can access that using java.

  • bzkrtmurat

    Hey guys, I have question about this subject. Can you check this question and help me ?

    • metin

      I have updated my post, along with usage example to send strings. This should work.

  • bzkrtmurat

    Metin did you read my question about this subject ?

  • summey

    Awesome! But can you do a tutorial on how to do this with the high-level api?

    Thank you

  • Jesus

    int result = LibUsb.bulkTransfer(handle, endpoint, buffer, transfered, 3000);

    endpoint value??? where from???

    • metin

      I mentioned already at the beginning of the post,

      You will need to find the right end point to communicate, the easiest way is to pop a usb with ubuntu on it, then in terminal use “lsusb” command

      It can be found also with the high level api, I will post it later. If its a printer its probably 1 or 2

  • I seem to be having problems with LibUsb library. I cannot claim an interface of a USB device and the error I keep getting is “USB error 3: Unable to claim interface: Access denied (insufficient permissions)”. Do you know why this might be happening? It happens on both, Windows and Linux…

    Exception thrown looks like this…
    Exception in thread “main” org.usb4java.LibUsbException: USB error 3: Unable to claim interface: Access denied (insufficient permissions)
    at UsbLowPos.claimDevice(
    at UsbLowPos.getDeviceHandle(
    at UsbLowPos.findDevice(
    at UsbLowPos.claimDevice(
    at UsbLowPos.main(

    • metin


      1. make sure the drivers are correctly installed, although in linux shouldn’t be problem if the device is detected
      2. check it in devices list, you probably have done it but do it again
      3. as the error says check to see if the interface number is correct
      4. not all interfaces might be implemented on the driver you are using, simply means a function might not be available for that device with that driver, so make sure to install latest drivers and see if that interface is available

      • I was using a libusb’s inf-wizard and it didn’t work in java with those drivers, although it worked great in c++. Then after I posted this question, I gave Zadig’s drivers a try and it worked perfectly in java. On the other hand it messed up c++ code so there 🙂 you win some, you lose some 😉

        • Metin

          In that case, give the high api a try. If you haven’t already.

  • Don


    I’m trying to use this implementation to establish connection with a gps device via the usb port using java. I already know the product number and vendor id and have installed the libusb driver using zadig.

    When I compile the code it gives me an error saying “Handle must not be null.” Do you know what this means and how I can fix it?

    I’m running windows 7.

    • metin

      does your device get found? I would check the product number and id.

      • Don


        I figured out how to how to match my device with the product and vendor id, but I still can’t open the device. The program will return the matched vendor and product id so the device is found, but I believe the problem is in the functions getDevice() and claimDevice(). Even though my device is found it can’t get the device handle and then claim the device which is why it is throwing that error. The GPS I’m using is a Garmin GPS which leads me to ask:
        1) I read about having to install a driver (libusb or winusb?) in order to communicate with any usb device using usb4java, my GPS comes with the default garmin driver. Do I need to install libusb or some other driver, and is it possible to do it without installing any drivers?
        2) I’m familiar with Zadig. If I use that tool to install a the required driver, do I need to do any further configurations or will the program be able to open the device?
        3) What is the function claimDevice doing? Once you get the device why do you need to claim it?
        4) Some of the earlier comments are asking about getting the endpoint of the usb for communication, you said the easiest way is to use linux. Is there a way to do it on windows if this is the only OS at my disposal?
        5) I don’t need to send data to the GPS just retrieve data from it. How different is the implementation of reading from a device as opposed to writing to it? Am I still using bulk transfer to read data but the bytes on coming out from the device not in from the device?
        6) Once I find the endpoint for the GPS can I use that same endpoint to read data from it or does a device have separate endpoint for in/out or read/write data flow?

        -Thank you so much for the help. I know these are a lot of questions but there are no real examples on how to use usb4java that I’ve found anywhere else and like stated above the website itself is really confusing on how to properly use it.

        • metin

          No problem, it is what I do, making complex stuff that should have been simple, simple 🙂
          1 + 2) If you have the driver for your device good, then give the high api a try,then you don’t have to bother with zadig.
          3) Suppose you have 1 printer and 5 computers want to print something at the same time, that is why you claim the device for use and then release it when you are done.
          4) You can get the endpoint with the high api, will update this and the other post about usb4java in few days.
          5 + 6) Will update the post for reading data but you will have to test it, I will try to test but I will need a suitable device. Same endpoint for read write… probably yes 🙂

          • Don

            So I installed the libusb driver using zadig, and it got rid of the “Error 12- Device must not be null” error but now when my claim function gets called I get “USB Error 3- Unable to claim or take the usb interface: (Access Denied) ” Any idea what this means. Is this an error with my code, driver configurations, or do I need to adjust some settings on my laptop. I haven’t found anything Windows specific to this issue but for example others have used Mac and they get this error because the OS automatically claims the device when it is connected.

            Also updates on endpoints, and reading data would be great.


          • Don

            A side question relating to claiming a device. The example you gave as to why it is necessary with the 1 printer and 5 computers all trying to print at the same time from said printer made sense as to why you would need to claim a device for use and then release it.

            But lets say this printer (or GPS for my purpose) is only ever going to be used by one person when it is connected to there machine through the usb port (the possibility of multiple people from different computers all trying to access the device at the same time is never going to happen). So would having to claim the device for use still be necessary for communication?

            Or once you detect the device and open it, can you start transferring data in or out through the endpoints?

          • metin

            Honestly never tried it without claiming a device and Im not sure this library allows it. Just uninstall zadig and try the high api if you can. Trus me it is actually much easier. Also I will update the post for reading data.

  • Don

    Thanks Metin, an update would be great! I tried the High Api and have a question regarding an error I got.

    It’s over on the High Api comment section, if you could help me out that would be great!

  • murat

    Hello friends, I have question about usb4java usage in android. Can you help me please.

  • victor

    Thank you for your example. I have magmatic card reader. I got error on below. Could you support for this. Thank you.
    int received = pipe.syncSubmit(data);
    javax.usb.UsbPlatformException: USB error 1: Transfer error on interrupt endpoint: Input/Output Error
    at org.usb4java.javax.ExceptionUtils.createPlatformException(
    at org.usb4java.javax.IrpQueue.transferInterrupt(
    at org.usb4java.javax.IrpQueue.transfer(
    at org.usb4java.javax.IrpQueue.processIrp(
    at org.usb4java.javax.AbstractIrpQueue.process(
    at org.usb4java.javax.AbstractIrpQueue$

    • metin

      This is probably the solution to your error!topic/usb4java/Feb2WuIzonA
      So make sure you are sending the right length of bytes that your device will accept. To find out the right length look into the documentation of your device, it should be available.

      • victor

        Thank you for reply. I used High API.
        This is HID magnetic card reader. Can count size in notepad as 70 for below..
        00 000000000000000000000000000000 0000000000000000 0

        1) byte[] data = new byte[70]; int received = pipe.syncSubmit(data);
        2) while (AsyncRead) {
        pipe.asyncSubmit(new byte[70]);

        Tried in both way. but got exception..
        Could you tell me the reason again.
        Thank you for spending good time with me.

        • metin

          The reason is that you might be sending wrong number of bytes, that means the array length.
          Also that string reads 52 bytes on my PC with notepad++.

          So before trying to read the bytes from a file try this:
          final byte[] bytes = new byte[64];
          bytes[0] = (byte)0x04;
          bytes[1] = (byte)0x20;

          This should work. In case it doesn’t look for HID libraries for java as there are some good ones.

  • victor

    Thank you for reply. Actually it length is 69 for below.
    “00 000000000000000000000000000000 0000000000000000 0”
    final byte[] bytes = new byte[69];
    bytes[0] = (byte)0x04;
    bytes[1] = (byte)0x20;
    int received = pipe.syncSubmit(bytes);

    Got exception as below.
    javax.usb.UsbPlatformException: USB error 1: Transfer error on interrupt endpoint: Input/Output Error

    Could you tell me the solution for this.

    Thank you for your cooperation.

  • victor

    length is 69

    • metin

      Okay it is 69. But it is important to send something that your device will accept.
      That means it might be 64, 128 or something similar. Please look into the documantation of your device.

  • router

    I am using your sending data code and it will showing an error called “USB error 5: Unable to open USB device: Entity not found” what`s mean?

    • metin

      Its the usual :), first check drivers and update if you haven’t already, make sure the device is available to use, check if the device can be found in the DeviceList.

      • router

        frist, thank for reply metin,
        Im checked drivers and update and found in the DeviceList.
        following error “USB error 5: Unable to open USB device: Entity not found” in this code

        handle = new DeviceHandle();
        int result =, handle);
        if (result != LibUsb.SUCCESS) {
        throw new LibUsbException(“Unable to open USB device”, result);
        i dont know whats happened
        another project ( C++ project ) it`s work (this device)

      • router

        ah ! Thanks metin it`s problem solved

        my usb driver update using zadig software

Recent Posts

Recent Comments