Compiling LibUSB for Android

Last Updated:

  1. t.elimpus

    t.elimpus Member

    Hey Guys,
    I'm writing an application that requires a custom USB driver, and i need LibUSB to be able to do it. I downloaded the following: Git - platform/external/libusb.git/summary

    My problem is that when i try to cross compile using ./configure this lib, i get the error: unsupported operating system. I've been trying to configure like this:

    Code (Text):
    2. ./configure --host=arm-eabi CC=arm-linux-androideabi-gcc CPPFLAGS="-I$NDK_ROOT/platforms/android-3/arch-arm/usr/include/" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/" LIBS="-lc"
    The main aim is to be able to use the library with the Android NDK. If anybody has managed this - or could help me with this, it would be most appreciated! (Also - i am using Ubuntu)

    Cheers, Tony.

  2. arsalan.haqs

    arsalan.haqs New Member

    Hi Tony,

    I have to do a similar stuff with android NDK using Libusb. Please can you help via sharing how did you get on with the libusb and did you achieved the communication?

    Highly appreciated and thanks in advance.

  3. t.elimpus

    t.elimpus Member

    Hey Arsalan,
    Sorry about the late reply. I managed to build the library in the same makefile that you build your C native library in. I took out all the C/header files from the uncompiled libusb folder which is the link i provided in my first post, and put them in the jni folder of the App's root folder. In the makefile, i included all the .c files in the jni folder as LOCAL_C_INCLUDES. This is the way that has worked for me, hope it helps you too.

  4. jonbonazza

    jonbonazza Well-Known Member

    When compiling libraries using android, you need to do the following:

    1) Make sure the files are all in the jni folder
    2) Create an file for each library to be compiled
    3) OPTIONAL: Create an file that sets various compilation flags
    4) compile using the ndk-build shell script. Configuration/Make files will not work correctly because an alternate toolchain is used for android.
  5. ndnfan

    ndnfan New Member

    Tony -- I got lsusb working, but when I attempt to write anything to the USB peripheral, the application restarts. Were you able to send commands to the USB peripheral without having to first get root access?
  6. ndnfan

    ndnfan New Member

    Here is how I got libusb working in Android without rooting the phone.
    1) In Java, enumerate the USB and find the device you want. Reference this URL: USB Host | Android Developers
    2) Once you find the device, get the file descriptor to it. Reference this method:UsbDeviceConnection | Android Developers
    3) Pass the file descriptor to the ported libusb code. I had to change the libusb code slightly to accept a file descriptor instead of trying to open its own descriptor.

    Once that's complete, I was able to make use of libusb w/o issue. As far as I know, this is the only way to get isochronous USB support on Android.
  7. jonbonazza

    jonbonazza Well-Known Member

    Please note that USB Host capabilities were not added until Android 4.0, so if your application requires those capabilities, you will need to either (a) purchase an android 4.0 device for testing, or (b) root your current device and find a functional Android 4.0 ROM to install on it.
  8. jazzatar

    jazzatar New Member

    Can you explain a bit better what changes were made to libusb to make this work? Or better yet, source? Thanks!
  9. MagicAsh

    MagicAsh New Member

    This is exactly what I need to do for my school project. How do I do step 3? :D I already can get the file descriptor. How do I pass it to libusb?

    Really need help to do it.

  10. martinmarinov

    martinmarinov New Member

    I modified libusb open function as follows:

    [HIGH]static int op_open2(struct libusb_device_handle *handle, int fd) {
    struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);

    hpriv->fd = fd;

    return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);

    where fd is the file descriptor obtained in Java via

    [HIGH]final UsbDeviceConnection connection = manager.openDevice(device);
    return connection.getFileDescriptor();[/HIGH]

    Unfortunatelly I keep getting an error when I call

    [HIGH]static int op_claim_interface(struct libusb_device_handle *handle, int iface)
    int fd = _device_handle_priv(handle)->fd;

    int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface);

    if (r) {
    if (errno == ENOENT)
    else if (errno == EBUSY)
    else if (errno == ENODEV)

    "claim interface failed, error %d errno %d", r, errno);
    return 0;

    claim interface failed, error -1 errno 9

    which is translated to "Bad file number". The file descriptor I get from Java is a positive integer!

    The only other small detail is that my native code runs as a separate binary that is spawned with a Java ProcessBuilder. But they share the same uid, so I presume the USB permissions I have from Java should still apply for libusb.

    Any thoughts would be much appreciated!
  11. jonbonazza

    jonbonazza Well-Known Member

    Can you post your native function that is called from java? There might be an error in it.

Share This Page