• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Compiling LibUSB for Android

t.elimpus

Lurker
Aug 12, 2011
6
0
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:

android.git.kernel.org 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:
./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.
 
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:

android.git.kernel.org 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:
./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.


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.

-Arsalan
 
Upvote 0
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.

Tony.
 
Upvote 0
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 Android.mk file for each library to be compiled
3) OPTIONAL: Create an Application.mk 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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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:
2) Once you find the device, get the file descriptor to it. Reference this method:
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.

Can you explain a bit better what changes were made to libusb to make this work? Or better yet, source? Thanks!
 
Upvote 0
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.

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.

Thanks!
 
Upvote 0
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);
}[/HIGH]

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)
return LIBUSB_ERROR_NOT_FOUND;
else if (errno == EBUSY)
return LIBUSB_ERROR_BUSY;
else if (errno == ENODEV)
return LIBUSB_ERROR_NO_DEVICE;

usbi_err(HANDLE_CTX(handle),
"claim interface failed, error %d errno %d", r, errno);
return LIBUSB_ERROR_OTHER;
}
return 0;
}[/HIGH]

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!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones