Android串口使用3之使用CMake工具完成android-serialport-api庫的移植

君問歸期未有期,巴山夜雨漲秋池。
對於Android串口的使用,基本已經被寫爛了,網上一搜一大堆教程,還有很多大佬也已經封裝成庫了,可以在項目中直接添加依賴進行使用。用別人造的輪子不好嗎?非要自己動手再造輪子?這是在弄啥嘞?
emm。。。。。這麼一說好像這篇文章沒必要往下寫了。。。。。。

別人造的輪子https://github.com/xmaihh/Android-Serialport

這個輪子造的不錯,但是有個小瑕疵,如果使用默認的粘包處理,默認的是不處理粘包,直接讀取返回,但是在返回數據的時候,將數據位擴展成64位了,對於接收數據量比較少的數據,會產生很多 多餘的“0”

問題是出現在BaseStickPackageHelper裏面

//原來定義的數據
byte[] buffer = new byte['?'];

//可以改成這樣
byte[] buffer = new byte[available];

好了,是時候打臉了,我要往下寫了,誰讓咱們是勤寫標兵呢
這篇文章其實是接着我上一篇文章的,Android串口使用2之使用Google官方庫android-serialport-api 它是用Android.mk方式實現的,而這篇文章用CMake工具也可以做到,反正最後都能抓到老鼠,至於它是黑貓、白貓還是紅貓,那就要看你怎麼看咯。

由於Android Studio 3.5版本在新建項目之時,無法看到Include C++ Support這個選項,本來可以一步到位的操作,現在要多走幾步了。

  1. 檢查NDK和CMake是否安裝
    找到Settings,或者使用快捷鍵Ctrl + Alt + S。搜索Android SDK,找到SDK Tools,選中CMake和NDK,點擊Apply,最後點OK。
    在這裏插入圖片描述
  2. 無論是新建項目還是在舊的項目上,切換項目視圖到Project模式,默認是Android模式。找到src/main,右鍵main文件夾,選擇New,找到Folder,選擇JNI Folder
    在這裏插入圖片描述將上篇文章中的SerialPort.h和SerialPort.c這兩個文件複製過去,複製android-serialport-api庫中的也行。在這裏插入圖片描述
  3. 右鍵app,New一個File文件,命名爲CMakeLists.txt
    在這裏插入圖片描述CMakeLists.txt內容如下:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        SerialPort

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/jni/SerialPort.c)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        SerialPort

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

CMakeLists.txt這個文件主要定義了哪些文件需要編譯,以及和其他庫的關係等。

  1. 右鍵app,選擇Link C++ Project with Gradle
    在這裏插入圖片描述
  2. 配置app/build.gradle文件
    如果上面gradle成功之後,會自動生成下面內容的
externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }

但是還需要手動添加一點內容

defaultConfig {
        ......
        externalNativeBuild {
            cmake {
                cppFlags ""
                //.so文件所支持的CPU架構
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86_64', 'x86'
            }
        }
    }

目前Android系統支持以下七種不用的CPU架構,每一種對應着各自的應用程序二進制接口ABI:(Application Binary Interface)定義了二進制文件(尤其是.so文件)如何運行在相應的系統平臺上,從使用的指令集,內存對齊到可用的系統函數庫。對應關係如下:
ARMv5——armeabi
ARMv7 ——armeabi-v7a
ARMv8——arm64-v8a
x86——x86
MIPS ——mips
MIPS64——mips64
x86_64——x86_64

  1. 最後一步,在src/main/java根目錄下,新建一個文件夾android_serialport_api,名字千萬不要改哦,因爲這個名字鏈接着這個api庫,改變之後,java代碼無法調用它,會報錯的。
    複製之前的SerialPort.java到這個文件夾裏,但是需要修改一個地方,因爲CMakeLists文件定義了這個庫的名字是SerialPort,也可以自定義這個庫名,跟下面對應上就可以了:
static {
		System.loadLibrary("SerialPort");
	}

寫到這裏,基本就完成了android-serialport-api庫的移植,感覺所做的事情也不少,就當學習下新技能,CMake工具的簡單使用。

如果需要源代碼的,可以點這裏。
源代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章