Android Studio开发串口时进行编译C接口注意点、跟解析协议、还有Android外部定义的java类库如何在activity能被引用到。

1、Android Studio开发串口时进行编译C接口的主要设置:

  • 首先SerialPort.c、SerialPort.h文件可以随意放到一个文件夹下,一般推荐在app下建立文件夹【jni】。

要修改的位置为主要是函数名,其实如果没修改这两个函数名颜色是红色的,当我们手动输入Java后就会出现自动提示,按确认就马上改好了。

  • 然后再网上GitHub上找到别人的编译串口的CMakeLists.txt文件放到工程里,理论上放的位置随意,这里是放到app目录下,跟【jni】文件同级。
  • 修改CMakeLists.txt文件主要位置有,下面是完整Cm文件的内容,
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             #1、这个是指生成的库文件名称,到时候生成的so会把打包到apk文件里
             serial_portt
             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             #2、这个是主要,其指定源C文件的文件路径。
             jni/SerialPort.c )
             
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.
                        #3、这个名称要跟上面的1的名称一样
                       serial_portt

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
  •  接着再SerialPort.jave文件里修改加载的C文件打标的库名称,即是我们上面步骤修改的库名称,其要去掉,其不用添加lib、so等前缀、后缀。
  • 接着在build.grade里指定CM文件的路径,并且调用ndk进行指定其编译的编译的库类型(不过测试时此部分代码可以注释都OK)
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.serialportweight"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
       #此部分代码好像非必须
        externalNativeBuild {
            ndk {
                moduleName "serial_port"         //生成的so名字,此名称无效,在cmake里指定的才有效
                abiFilters  "armeabi-v7a", "x86"  //输出指定三种abi体系结构下的so库。目前可有可无。
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    #指定CM文件的位置
    externalNativeBuild{
        cmake{
            path 'CMakeLists.txt'
        }

    }

}
  • 其中谷歌官方对串口又进行了一次封装到类Application.jave里,这个是app启动就会调用的东西。其实实际使用可以不继承【android.app.Application】然后进行单独定义一个类进行把接口在那里进行封装。
  • 到此基本配置完成,直接可以buil,然后通过【build-》Analyze apk】进行查看编译进apk里的库文件

2、跟解析协议:

其中谷歌的这个串口样例,其读取的

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值