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

我们两清 提交于 2019-12-10 13:05:44

问题


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.


回答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



来源:https://stackoverflow.com/questions/7469518/using-libusb-in-android-application-how-to-allow-application-to-access-usb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!