libusb简介

1
2
3
4
libusb 是一个 C 库,提供对 USB 设备的通用访问。开发人员旨在使用它来促进与 USB 硬件通信的应用程序的生产。
它是可移植的:使用单个跨平台 API,它提供对 Linux、macOS、Windows 等 USB 设备的访问
它是用户模式:应用程序与设备通信不需要特殊权限或提升权限。
它与版本无关:支持从 1.0 到 3.1(最新)的所有 USB 协议版本。

官网:https://libusb.info/

仓库:https://github.com/libusb/libusb/tree/master

git clone https://github.com/libusb/libusb.git

编译

官方的android目录下的编译指导文档原文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
libusb for Android
==================

Building:
---------

To build libusb for Android do the following:

1. Download the latest NDK from:
http://developer.android.com/tools/sdk/ndk/index.html

2. Extract the NDK.

3. Open a shell and make sure there exist an NDK global variable
set to the directory where you extracted the NDK.

4. Change directory to libusb's "android/jni"

5. Run "$NDK/ndk-build".

The libusb library, examples and tests can then be found in:
"android/libs/$ARCH"

Where $ARCH is one of:
armeabi
armeabi-v7a
mips
mips64
x86
x86_64

Installing:
-----------

If you wish to use libusb from native code in own Android application
then you should add the following line to your Android.mk file:

include $(PATH_TO_LIBUSB_SRC)/android/jni/libusb.mk

You will then need to add the following lines to the build
configuration for each native binary which uses libusb:

LOCAL_C_INCLUDES += $(LIBUSB_ROOT_ABS)
LOCAL_SHARED_LIBRARIES += libusb1.0

The Android build system will then correctly include libusb in the
application package (APK) file, provided ndk-build is invoked before
the package is built.


Runtime Permissions:
--------------------

The Runtime Permissions on Android can be transferred from Java to Native
over the following approach:

JAVA:

--> Obtain 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_NO_DEVICE_DISCOVERY, NULL);
libusb_init(&ctx);
libusb_wrap_sys_device(NULL, (intptr_t)fileDescriptor, &devh);
}
/* From this point you can regularly use all libusb functions as usual */

About LIBUSB_OPTION_NO_DEVICE_DISCOVERY:

The method libusb_set_option(&ctx, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL)
does not affect the ctx.
It allows initializing libusb on unrooted Android devices by skipping
the device enumeration.

Rooted Devices:
---------------

For rooted devices the code using libusb could be executed as root
using the "su" command. An alternative would be to use the "su" command
to change the permissions on the appropriate /dev/bus/usb/ files.

Users have reported success in using android.hardware.usb.UsbManager
to request permission to use the UsbDevice and then opening the
device. The difficulties in this method is that there is no guarantee
that it will continue to work in the future Android versions, it
requires invoking Java APIs and running code to match each
android.hardware.usb.UsbDevice to a libusb_device.

For a rooted device it is possible to install libusb into the system
image of a running device:

1. Enable ADB on the device.

2. Connect the device to a machine running ADB.

3. Execute the following commands on the machine
running ADB:

# Make the system partition writable
adb shell su -c "mount -o remount,rw /system"

# Install libusb
adb push obj/local/armeabi/libusb1.0.so /sdcard/
adb shell su -c "cat > /system/lib/libusb1.0.so < /sdcard/libusb1.0.so"
adb shell rm /sdcard/libusb1.0.so

# Install the samples and tests
for B in listdevs fxload xusb sam3u_benchmark hotplugtest stress
do
adb push "obj/local/armeabi/$B" /sdcard/
adb shell su -c "cat > /system/bin/$B < /sdcard/$B"
adb shell su -c "chmod 0755 /system/bin/$B"
adb shell rm "/sdcard/$B"
done

# Make the system partition read only again
adb shell su -c "mount -o remount,ro /system"

# Run listdevs to
adb shell su -c "listdevs"

4. If your device only has a single OTG port then ADB can generally
be switched to using Wifi with the following commands when connected
via USB:

adb shell netcfg
# Note the wifi IP address of the phone
adb tcpip 5555
# Use the IP address from netcfg
adb connect 192.168.1.123:5555

执行编译

1.cd libusb
2.cd android/jni
3.找到自己的ndk目录,例如下面直接使用绝对路径 /home/tyl/Android/Sdk/ndk/26.1.10909125
4./home/tyl/Android/Sdk/ndk/26.1.10909125/ndk-build
5.生成的so文件在libusb/android/libs中

编译好的库可以直接集成到Android中了。