簡體   English   中英

無需root即可在Android上使用libusb

[英]Using libusb on Android without rooting

我正在嘗試通過 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設備訪問權限?

這就是你要找的。
https://github.com/kuldeepdhaka/libusb/tree/android-open2
只需編譯它並將其放入。:)
有關完整用法,請參閱“Android 操作方法”部分。

我對 libusb 進行了所有必要的修改(我也在使用它)。
它也有針對“Android 5.0”+ 的 SELinux 修復。

另請參閱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.
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM