[英]Using libusb on Android without rooting
I am trying to communicate with USB device from Android-based smartphone via OTG.我正在尝试通过 OTG 与基于 Android 的智能手机的 USB 设备进行通信。 I was able to communicate with my device using Android USB Host API.我能够使用 Android USB Host API 与我的设备进行通信。 The problem of USB Host API solution is performance (single bulk transfer bounded by 16384 bytes). USB Host API 解决方案的问题是性能(以 16384 字节为界的单个批量传输)。
The libusb can perform larger requests and now I am trying to integrate it using Android NDK. libusb 可以执行更大的请求,现在我正在尝试使用 Android NDK 集成它。 I succeeded to compile libusb sources for Android and even initUSB()
, but libusb_open(dev, &dev_handle)
returns -3 (Access denied).我成功地为 Android 甚至initUSB()
编译了 libusb 源代码,但libusb_open(dev, &dev_handle)
返回 -3(拒绝访问)。
How can I pass the file descriptor如何传递文件描述符
int fd = connection.getFileDescriptor()
to libusb after getting USB_PERMISSION under Android USB Host API and get USB device access under libusb?在Android USB Host API下获得USB_PERMISSION后到libusb并在libusb下获得USB设备访问权限?
This is what you are looking for.这就是你要找的。
https://github.com/kuldeepdhaka/libusb/tree/android-open2 https://github.com/kuldeepdhaka/libusb/tree/android-open2
just compile it and drop it in. :)只需编译它并将其放入。:)
see the "How To for Android" section for full usage.有关完整用法,请参阅“Android 操作方法”部分。
i made all the required modification to libusb (and im also using it).我对 libusb 进行了所有必要的修改(我也在使用它)。
It has SELinux fix for "Android 5.0"+ too.它也有针对“Android 5.0”+ 的 SELinux 修复。
See also https://github.com/libusb/libusb/wiki/Android which now discusses android a little bit.另请参阅https://github.com/libusb/libusb/wiki/Android ,现在稍微讨论了 android。 Here is the quote from the proposed readme change (2021-02):以下是提议的自述文件更改 (2021-02) 中的引用:
Runtime Permissions:
--------------------
The Runtime Permissions on Android can be transfered from Java to Native over the following approach:
Java:
// obtaining the Usb Permissions over the android.hardware.usb.UsbManager class
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice usbDevice : deviceList.values()) { usbManager.requestPermission(usbDevice, mPermissionIntent); }
// get the native FileDescriptor of the UsbDevice and transfer it to Native over JNI or JNA
UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(camDevice);
int fileDescriptor = usbDeviceConnection.getFileDescriptor();
// JNA sample method:
JNA.INSTANCE.set_the_native_Descriptor(fileDescriptor);
Native:
// Initialize LibUsb on Android
set_the_native_Descriptor(int fileDescriptor) {
libusb_context *ctx;
libusb_device_handle *devh;
libusb_set_option(&ctx, LIBUSB_OPTION_WEAK_AUTHORITY, NULL); // important for Android
libusb_init(&ctx);
libusb_wrap_sys_device(NULL, (intptr_t)fileDescriptor, &devh);
// From this point you can regulary use all LibUsb functions as usual.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.