libhackrf是上層應用程序操做hackrf的入口庫,是軟件操做硬件的中間件,在android上使用hackrf固然也須要使用 libhackrf。操做系統與hackrf之間的通訊是經過USB2.0完成的,libhackrf使用了libusb進行USB通訊。android 並無自帶libusb,因此咱們須要自行編譯。linux
總體分爲兩個部分,首先是libusb的編譯,而後是libhackrf的編譯。android
libusbredis
libusb的編譯十分簡單,由於其沒有過多的依賴,而麻煩的是android對usb權限的控制,解決權限問題有兩種方法,第一種是改造 libusb,並在android應用層獲取usb的權限將usb的信息傳到libusb。第二種是經過修改系統權限列表,使得當前程序在系統usb用戶 組中。我這裏爲了更少的更改libusb和libhackrf文件,就選用了第二種方法。如下爲libusb的編譯步驟socket
1)確保NDK安裝成功,可經過在cmd中運行ndk-build --version查看信息。ui
- C:\Users\XXXX>ndk-build --version
- GNU Make 3.81
- Copyright (C) 2006 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions.
- There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
- PARTICULAR PURPOSE.
-
- This program built for i586-pc-mingw32
2)下載libusb,能夠到www.libusb.org下載。this
3)加壓libusb後在源碼根目錄建立android文件夾和相應文件。spa
|- libusb-master操作系統
|- android.net
|- jnidebug
|- Android.mk
|- Application.mk
|- examples.mk
|- libusb.mk
|- tests.mk
|- libs
|- obj
|- config.h
|- libusb
...
文件以下:
config.h
-
- #define ENABLE_LOGGING
-
- #define HAVE_DLFCN_H 1
-
- #define HAVE_GETTIMEOFDAY 1
-
- #define HAVE_INTTYPES_H 1
-
- #define OS_LINUX 1
-
- #define USE_SYSTEM_LOGGING_FACILITY 1
-
- #define POLL_NFDS_TYPE nfds_t
-
- #define THREADS_POSIX 1
-
- #define DEFAULT_VISIBILITY __attribute__((visibility("default")))
-
- #define HAVE_MEMORY_H 1
-
- #define HAVE_POLL_H 1
-
- #define HAVE_SIGNAL_H 1
-
- #define HAVE_SYS_STAT_H 1
-
- #define HAVE_SYS_TIME_H 1
-
- #define HAVE_SYS_TYPES_H 1
-
- #define HAVE_UNISTD_H 1
-
- #define HAVE_LINUX_FILTER_H 1
-
- #define HAVE_LINUX_NETLINK_H 1
-
- #define HAVE_ASM_TYPES_H 1
-
- #define HAVE_SYS_SOCKET_H 1
Android.mk
- LOCAL_PATH:= $(call my-dir)
-
- include $(LOCAL_PATH)/libusb.mk
- include $(LOCAL_PATH)/examples.mk
- include $(LOCAL_PATH)/tests.mk
Application.mk
- APP_ABI := all
-
- APP_LDFLAGS := -llog
examples.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
-
- 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 := libusb1.0
-
- include $(BUILD_SHARED_LIBRARY)
tests.mk
- # Android build config for libusb 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)
- LIBUSB_ROOT_REL:= ../..
- LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
-
- # testlib
-
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES := \
- $(LIBUSB_ROOT_REL)/tests/testlib.c
-
- LOCAL_C_INCLUDES += \
- $(LIBUSB_ROOT_ABS)/tests
-
- LOCAL_EXPORT_C_INCLUDES := \
- $(LIBUSB_ROOT_ABS)/tests
-
- LOCAL_MODULE := testlib
-
- include $(BUILD_STATIC_LIBRARY)
-
-
- # stress
-
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES := \
- $(LIBUSB_ROOT_REL)/tests/stress.c
-
- LOCAL_C_INCLUDES += \
- $(LIBUSB_ROOT_ABS)
-
- LOCAL_SHARED_LIBRARIES += libusb1.0
- LOCAL_STATIC_LIBRARIES += testlib
-
- LOCAL_MODULE:= stress
-
- include $(BUILD_EXECUTABLE)
4)切換到android目錄,使用ndk-build進行編譯
編譯完成以後,能夠在libs文件夾下看到不一樣平臺的lib和可執行文件,拷貝便可使用。
libhackrf
libhackrf的編譯,能夠參照libusb的編譯。在hackrf-master\host\libhackrf\src下建立以下結構
|- android/
|- jni/
|- Android.mk
|- Application.mk
|- libhackrf.mk
|- libs/
|- obj/
Android.mk
- LOCAL_PATH:= $(call my-dir)
-
- include $(LOCAL_PATH)/libhackrf.mk
- include $(LOCAL_PATH)/../../libusb/Android.mk
Application.mk
- APP_ABI := armeabi armeabi-v7a
-
- APP_LDFLAGS := -llog
libhackrf.mk
- # Android build config for libhackrf
- # Copyright © 2014 <pagekpang@gmail.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.
- #
-
- LOCAL_PATH:= $(call my-dir)
- LIBUSB_ROOT_REL:= ../..
- LIBUSB_ROOT_ABS:= $(LOCAL_PATH)/../..
-
- # libhackrf
- #D:\dev\android-ndk-r9d\ndk-build
-
-
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES := $(LIBUSB_ROOT_REL)/hackrf.c
-
- LOCAL_C_INCLUDES += $(LIBUSB_ROOT_ABS)/ \
- $(LIBUSB_ROOT_ABS)/libusb/
-
- LOCAL_LDLIBS := -llog
-
- LOCAL_MODULE := libhackrf
-
- LOCAL_SHARED_LIBRARIES += libusb1.0
-
- include $(BUILD_SHARED_LIBRARY)
4)切換到android目錄,使用ndk-build進行編譯
整體編譯完成後,便可獲得libhackrf.so和libusb.so以及對映的頭文件,能夠拷貝到android中使用。