README 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. libusb for Android
  2. ==================
  3. Building:
  4. ---------
  5. To build libusb for Android do the following:
  6. 1. Download the latest NDK from:
  7. http://developer.android.com/tools/sdk/ndk/index.html
  8. 2. Extract the NDK.
  9. 3. Open a shell and make sure there exist an NDK global variable
  10. set to the directory where you extracted the NDK.
  11. 4. Change directory to libusb's "android/jni"
  12. 5. Run "$NDK/ndk-build".
  13. The libusb library, examples and tests can then be found in:
  14. "android/libs/$ARCH"
  15. Where $ARCH is one of:
  16. armeabi
  17. armeabi-v7a
  18. mips
  19. mips64
  20. x86
  21. x86_64
  22. Installing:
  23. -----------
  24. If you wish to use libusb from native code in own Android application
  25. then you should add the following line to your Android.mk file:
  26. include $(PATH_TO_LIBUSB_SRC)/android/jni/libusb.mk
  27. You will then need to add the following lines to the build
  28. configuration for each native binary which uses libusb:
  29. LOCAL_C_INCLUDES += $(LIBUSB_ROOT_ABS)
  30. LOCAL_SHARED_LIBRARIES += libusb1.0
  31. The Android build system will then correctly include libusb in the
  32. application package (APK) file, provided ndk-build is invoked before
  33. the package is built.
  34. Runtime Permissions:
  35. --------------------
  36. The Runtime Permissions on Android can be transferred from Java to Native
  37. over the following approach:
  38. JAVA:
  39. --> Obtain USB permissions over the android.hardware.usb.UsbManager class
  40. usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
  41. HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
  42. for (UsbDevice usbDevice : deviceList.values()) {
  43. usbManager.requestPermission(usbDevice, mPermissionIntent);
  44. }
  45. --> Get the native FileDescriptor of the UsbDevice and transfer it to
  46. Native over JNI or JNA
  47. UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(camDevice);
  48. int fileDescriptor = usbDeviceConnection.getFileDescriptor();
  49. --> JNA sample method:
  50. JNA.INSTANCE.set_the_native_Descriptor(fileDescriptor);
  51. NATIVE:
  52. --> Initialize libusb on Android
  53. set_the_native_Descriptor(int fileDescriptor) {
  54. libusb_context *ctx;
  55. libusb_device_handle *devh;
  56. libusb_set_option(&ctx, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
  57. libusb_init(&ctx);
  58. libusb_wrap_sys_device(NULL, (intptr_t)fileDescriptor, &devh);
  59. }
  60. /* From this point you can regularly use all libusb functions as usual */
  61. About LIBUSB_OPTION_NO_DEVICE_DISCOVERY:
  62. The method libusb_set_option(&ctx, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL)
  63. does not affect the ctx.
  64. It allows initializing libusb on unrooted Android devices by skipping
  65. the device enumeration.
  66. Rooted Devices:
  67. ---------------
  68. For rooted devices the code using libusb could be executed as root
  69. using the "su" command. An alternative would be to use the "su" command
  70. to change the permissions on the appropriate /dev/bus/usb/ files.
  71. Users have reported success in using android.hardware.usb.UsbManager
  72. to request permission to use the UsbDevice and then opening the
  73. device. The difficulties in this method is that there is no guarantee
  74. that it will continue to work in the future Android versions, it
  75. requires invoking Java APIs and running code to match each
  76. android.hardware.usb.UsbDevice to a libusb_device.
  77. For a rooted device it is possible to install libusb into the system
  78. image of a running device:
  79. 1. Enable ADB on the device.
  80. 2. Connect the device to a machine running ADB.
  81. 3. Execute the following commands on the machine
  82. running ADB:
  83. # Make the system partition writable
  84. adb shell su -c "mount -o remount,rw /system"
  85. # Install libusb
  86. adb push obj/local/armeabi/libusb1.0.so /sdcard/
  87. adb shell su -c "cat > /system/lib/libusb1.0.so < /sdcard/libusb1.0.so"
  88. adb shell rm /sdcard/libusb1.0.so
  89. # Install the samples and tests
  90. for B in listdevs fxload xusb sam3u_benchmark hotplugtest stress
  91. do
  92. adb push "obj/local/armeabi/$B" /sdcard/
  93. adb shell su -c "cat > /system/bin/$B < /sdcard/$B"
  94. adb shell su -c "chmod 0755 /system/bin/$B"
  95. adb shell rm "/sdcard/$B"
  96. done
  97. # Make the system partition read only again
  98. adb shell su -c "mount -o remount,ro /system"
  99. # Run listdevs to
  100. adb shell su -c "listdevs"
  101. 4. If your device only has a single OTG port then ADB can generally
  102. be switched to using Wifi with the following commands when connected
  103. via USB:
  104. adb shell netcfg
  105. # Note the wifi IP address of the phone
  106. adb tcpip 5555
  107. # Use the IP address from netcfg
  108. adb connect 192.168.1.123:5555