Using libusb in Android application: how to allow application to access USB
Asked Answered
L

1

7

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.

Lujan answered 19/9, 2011 at 10:30 Comment(6)
As you apparently are working with Android 3.1, have you considered using the USB APIs in the Android SDK?Dustan
@Frxstrem: yes, I had to resort to that solution. I'm not quite happy with it, but it works and it's the standard way of using USB on Android, so I guess I should close the question.Lujan
don't close it, just answer it.Homochromous
Have you tried sudo chmod 666 for /dev/bus/usb?Dipteral
@SergeyK.: I believe I did. but can't say for sure. I gave up on using libusb and just utilized standard Android USB API, which is the right way to go anyway.Lujan
Thanks for the update. It would be great if you can summaries your achievements and recommendations here as the answer!Dipteral
Y
1

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

You answered 15/7, 2014 at 12:48 Comment(2)
The link is broken.Inconsistency
@Inconsistency Updated link. Thanks.You

© 2022 - 2024 — McMap. All rights reserved.