Using libusb in Android application: how to allow application to access USB

android-ndklibusb-1.0

I'd like to use libusb in my Android app (java GUI + native(C++) core). I have already compiled libusb and tried calling its functions, but libusb_open return LIBUSB_ERROR_ACCESS. I suppose there is a problem with USB access permissions, but I don't know how to resolve the issue. So, 2 questions here:
1) How to get libusb running on a rooted Android 3.1 device?
2) Is it possible to use libusb on an unrooted, factory-default device?

Thanks in advance.

P. S. As for question 1, I've tried chmod 666 for /dev/bus/usb, but it says "permission denied" (note that my device IS rooted).

P.P.S. mount usbfs none /proc/bus/usb -o devmode=0666 doesn't even execute, as if I have misspeled something (but I didn't).


I know for sure it's possible to get libusb working on a rooted Android device with USB host, but I never managed it, so I had to restrict the app to Android 3.1+ and use USB APIs.

Best Answer

In android, you cannot directly open usb device using libusb (this is the conclusion that you came to :).

why you were not able to open?

You need to get permission from Android system (!= Kernel) to open a device.

Do it in Java:

when you request, a pop is shown to user to accept or reject. so you need to open the device in java, and extract the fd using java and pass on to libusb so that it can communicate with device.

now how will libusb build a handle from fd?

Update: I maintain a libusb version specific modified for Android (with more fixes - tested/working on Android 5.1 and lower). see https://gitlab.com/madresistor/libusb/blob/android/README

OLD LINK (DEAD LINK) here the solution: https://github.com/martinmarinov/rtl_tcp_andro-/blob/master/jni/libusb-andro/libusb/core.c#L993

Related Topic