【问题标题】:Using libusb on Android without rooting在 Android 上使用 libusb 而不生根
【发布时间】:2013-04-24 12:19:50
【问题描述】:

我正在尝试通过 OTG 从基于 Android 的智能手机与 USB 设备进行通信。我能够使用 Android USB Host API 与我的设备进行通信。 USB Host API 解决方案的问题在于性能(单次批量传输以 16384 字节为界)。

libusb 可以执行更大的请求,现在我正在尝试使用 Android NDK 集成它。我成功编译了适用于 Android 甚至 initUSB() 的 libusb 源代码,但 libusb_open(dev, &dev_handle) 返回 -3(拒绝访问)。

如何传递文件描述符

int fd = connection.getFileDescriptor()

在 Android USB Host API 下获得 USB_PERMISSION 后到 libusb 并在 libusb 下获得 USB 设备访问权限?

【问题讨论】:

标签: android permissions android-ndk usb libusb


【解决方案1】:

这就是你要找的。
https://github.com/kuldeepdhaka/libusb/tree/android-open2
只需编译它并将其放入。:)
有关完整用法,请参阅“Android 操作方法”部分。

我对 libusb 进行了所有必要的修改(我也在使用它)。
它也有针对“Android 5.0”+ 的 SELinux 修复。

【讨论】:

  • 这听起来很有希望,我已经查看了您的 github 项目,但我仍然不确定如何使用它。你是用jni从java代码调用libusb吗?
  • @dweebo 是的,Java -> JNI -> libusb
  • @dweebo 一切都在这里gitlab.com/madresistor。联系我(在 Freenode 上的 #madresistor 上 kuldeep)(或给我发邮件 - kuldeepdhaka9 gmail 的东西) - 我可以向你详细解释。
  • 这个github和主github有什么区别:github.com/libusb/libusb?
  • 它仍然相关吗?这对于处理 USB 音频类 2 很有用
【解决方案2】:

另请参阅https://github.com/libusb/libusb/wiki/Android,它现在稍微讨论了 android。以下是提议的自述文件变更 (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.
  }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
相关资源
最近更新 更多