1. Download libusb library.
You can go to the official website of the library libusb ( https://libusb.info/ ) or its official github repository ( https://github.com/libusb/libusb/releases download the latest source code library libusb on).
2. Add libusb library to android studio project.
Here's the way to compile the source added, is still used ndk-build approach as opposed to cmake, use the source code to compile advantage is that you can always libusb library source code debugging, plus convenient log.
2.1 Create a new directory under jni app / src / main directory for storing libusb library source code.
2.2 Open build.gradle file in the app directory, specify the directory JNI:
2.3 New libusb-support directories and files in the makefile jni directory, libusb-support directory for storing libusb library source code.
Here a single new directory libusb-support object, there could be a number of third-party modules, such libusb, libyuv, sdl, can be divided into sub-directory for more clearly, can directly libusb jni code into the directory, As long as makefile can be found on the line.
Android.mk
# # Copyright (c) 2019 Realsil.Inc. All rights reserved. # include $(call all-subdir-makefiles)
Application.mk
# # Copyright (c) 2019 SEP.Inc. All rights reserved. # APP_ABI := all APP_LDFLAGS := -llog
The final directory structure is as follows:
2.3 Copy libusb library source code directory to libusb-support
Open the downloaded libusb library source code, find libusb folder, copy the entire file to the directory libusb-support, I downloaded the 1.0.23 version:
Open the android folder, copy the file to the config.h libusb-support directory
Jni folder open, copy Android.mk, libusb.mk libusb-support files to the directory:
The final directory structure is as follows:
Then modify Android.mk and libusb.mk file, the default compiler libraries include some sample code and test cases, we do not need it.
Modify Android.mk, commented example.mk and tests.mk
# Android build config for libusb, examples and tests # Copyright © 2012-2013 RealVNC Ltd. <[email protected]> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # LOCAL_PATH:= $(call my-dir) include $(LOCAL_PATH)/libusb.mk # include $(LOCAL_PATH)/examples.mk # include $(LOCAL_PATH)/tests.mk
修改libusb.mk, 更新当前源代码的位置:
# Android build config for libusb # Copyright © 2012-2013 RealVNC Ltd. <[email protected]> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # LOCAL_PATH:= $(call my-dir) LIBUSB_ROOT_REL:= . LIBUSB_ROOT_ABS:= $(LOCAL_PATH) # libusb include $(CLEAR_VARS) LIBUSB_ROOT_REL:= . LIBUSB_ROOT_ABS:= $(LOCAL_PATH) LOCAL_SRC_FILES := \ $(LIBUSB_ROOT_REL)/libusb/core.c \ $(LIBUSB_ROOT_REL)/libusb/descriptor.c \ $(LIBUSB_ROOT_REL)/libusb/hotplug.c \ $(LIBUSB_ROOT_REL)/libusb/io.c \ $(LIBUSB_ROOT_REL)/libusb/sync.c \ $(LIBUSB_ROOT_REL)/libusb/strerror.c \ $(LIBUSB_ROOT_REL)/libusb/os/linux_usbfs.c \ $(LIBUSB_ROOT_REL)/libusb/os/poll_posix.c \ $(LIBUSB_ROOT_REL)/libusb/os/threads_posix.c \ $(LIBUSB_ROOT_REL)/libusb/os/linux_netlink.c \ $(LIBUSB_ROOT_REL)/libusb/realsil_usb_audio.c \ $(LIBUSB_ROOT_REL)/libusb/jnihelp.cpp LOCAL_C_INCLUDES += \ $(LOCAL_PATH)/ \ $(LIBUSB_ROOT_ABS)/libusb \ $(LIBUSB_ROOT_ABS)/libusb/os LOCAL_EXPORT_C_INCLUDES := \ $(LIBUSB_ROOT_ABS)/libusb LOCAL_LDLIBS := -llog LOCAL_MODULE := usb include $(BUILD_SHARED_LIBRARY)
其中的 realsil_usb_audio.c 和 jnihelp.cpp是我自己添加的调用libusb库的code, 可不用在意。
3. 编译链接
在任意文件夹下右击,选择“Link C++ Project with Gradle”
在Build System的下拉列表中选择ndk-build的编译方式,并指定所有模块的makefile文件位置:
点OK, 此时编译系统就会自动开始构建,注意到此时文件夹的目录颜色已修改,且app目录下的build.gradle文件也进行了更新。
接下来就可以在相应的C代码里编写调用libusb库的相应逻辑了。
参考链接:
1. Android从USB声卡录制高质量音频-----USB API测试
2. Android从USB声卡录制高质量音频-----使用libusb读取USB声卡数据