libusb: android上集成libusb庫


1. 下載libusb庫。

可以到libusb庫的官網(https://libusb.info/)或者是其官方的github倉庫(https://github.com/libusb/libusb/releases)上下載最新的libusb庫源碼。

2. 添加libusb庫到android studio項目中。

這里以源碼編譯的方式添加,使用的仍然是ndk-build的方式,而非cmake,使用源碼編譯的好處在於,可以隨時調試libusb庫的源碼,方便加log。

2.1 在app/src/main目錄下新建一個jni目錄,用於存放libusb庫的源代碼。

 

2.2  打開app目錄下的build.gradle文件,指定JNI目錄:

2.3  在jni目錄下新建libusb-support目錄和makefile文件,libusb-support目錄用於存放libusb庫的源代碼。

這里單獨新建一個libusb-support目錄的目的在於,將來可能會有很多個第三方模塊,比如libusb,libyuv, sdl,分目錄存放可以划分得更清楚,可以直接把libusb的代碼放到jni目錄下,只要makefile能找到就行。
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

最終目錄結構如下:

 2.3 拷貝libusb庫的源代碼到libusb-support目錄

打開下載的libusb庫源代碼,找到libusb文件夾,將整個文件拷貝到libusb-support目錄下,我下載的是1.0.23版本:

 

打開android文件夾,拷貝config.h文件到libusb-support目錄下

 

打開jni文件夾,拷貝Android.mk、libusb.mk文件到libusb-support目錄下:

 最終目錄結構如下:

接下來修改Android.mk和libusb.mk文件,默認編譯出來的庫會包含一些測試用例和示例代碼,我們不需要它。
修改Android.mk, 注釋掉example.mk和tests.mk

# Android build config for libusb, examples and tests
# Copyright © 2012-2013 RealVNC Ltd. <toby.gray@realvnc.com>
#
# 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. <toby.gray@realvnc.com>
#
# 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聲卡數據

3. Android如何對libusb進行編譯和使用

4. Libusb在Android平台上的環境以及原理

5. Android無驅usb音頻實現

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM