简体   繁体   English

无需root即可在Android上使用libusb

[英]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.

相关问题 Android手机中的屏幕截图没有生根 - Screenshot in Android phone without rooting 没有生根的android无法运行shell命令 - Cannot run the shell command without rooting android 如何在不扎根Android设备的情况下访问文件 - How to access files without rooting on an Android device Android内核在没有生根的情况下运行fanotify? - Android kernel run fanotify without rooting? 隐藏导航栏而不生根于android - Hiding navigation bar without rooting on android 使用网络监控来自所有应用程序的连接的IP地址,而无需植根Android手机 - Monitor the IP addresses from connections being made from all apps using the network without rooting the Android phone 如何在没有root的情况下在Nook(Not Nook Color)+中安装Android应用程序 - How to install an android application in a Nook (Not Nook Color) + without rooting Android:可以直接访问内核模块而无需扎根手机吗? - Android: possible to access kernel modules directly without rooting the phone? 如何在不生根的情况下禁用Android设备的hdmi端口? - How do i disable hdmi port of a android device without rooting it? 如何在没有root设备的情况下捕获其他Android应用程序的屏幕截图? - How to capture a screenshot of other Android application without rooting device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM