在自己的AndroidStudio项目中引入Google开源的串口通信方案

本文主要介绍,如何在自己的AndroidStudio项目中引入Google开源的串口通信方案(android-serialport-api)
完整项目地址 https://github.com/Micheal-Yan/ImportDemo
Google开源的方案项目地址 https://github.com/cepr/android-serialport-api
演示如何与自动售货机建立串口通信,从而达到控制自动售货机的目的 https://github.com/Micheal-Yan/FuJiTestDemo

1,在使用AndroidStudio创建新项目时,勾选Include C++ support

创建新项目

2,将Google官方Demo代码中SerialPort.java和SerialPortFinder.java这两个Java文件,直接拷贝到我们的项目中来

导入Java文件

3,打开SerialPort.java文件,将光标分别移至open和close方法上,然后按住Alt+Enter快捷键,选择"Create function xxx"快捷方式,将会自动为我们在native-lib.cpp文件中,为我们创建对应的jni方法

在这里插入图片描述
自动生成JNI方法

4,将Google官方Demo代码中serial_port.h和serial_port.c这两个C源文件,分别拷贝的项目的cpp/include和cpp目录下,并修改jni方法签名为上一步编译器自动生成签名

拷贝C源文件

5,修改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.
             serial_port

             # Sets the library as a shared library.
             SHARED

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

# Specifies a path to native header files.
include_directories(src/main/cpp/include/)

# 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.
                       serial_port

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

CMakeLists文件存放位置

6,删除创建项目时,在MainActivity.java文件中为我们自动生成的JNI使用演示代码片段和native-lib.cpp文件(下图框选的部分都要删除)

删除多余的文件

7,在工具栏选择Build-Refresh Linked C++ Projects,刷新一下项目,至此导入完毕

刷新项目

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你的Flutter项目是在创建时未引入某个平台的,比如在创建项目时未引入Android平台,那么你可以按照以下步骤在Android Studio添加Android平台: 1. 打开你的Flutter项目,在顶部菜单栏选择`File` -> `New` -> `New Module`。 2. 在弹出的对话框选择`Android Library`,然后点击`Next`。 3. 在下一个界面输入你的Android库的名称,比如`flutter_android`,然后点击`Finish`。 4. 在Android Studio的左侧面板,展开`Project`视图,可以看到刚刚创建的`flutter_android`库。 5. 打开`flutter_android`库的`build.gradle`文件,在其添加以下代码: ``` apply plugin: 'com.android.library' android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { minSdkVersion 16 targetSdkVersion 30 versionCode flutterVersionCode.toInteger() versionName flutterVersionName ndk { abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' } } buildTypes { release { minifyEnabled false shrinkResources false } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) } ``` 注意修改`compileSdkVersion`和`targetSdkVersion`为你想要的版本。 6. 在Android Studio的左侧面板,展开你的Flutter项目,打开`android`目录下的`settings.gradle`文件,在最后添加以下代码: ``` include ':app' setBinding(new Binding([gradle: this])) evaluate(new File( settingsDir.parentFile, 'flutter_android/include_flutter.groovy' )) ``` 7. 在`android`目录下创建一个名为`include_flutter.groovy`的文件,并添加以下代码: ``` def flutterProjectRoot = rootProject.projectDir.parentFile.toPath() def plugins = new Properties() def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins') if (pluginsFile.exists()) { pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) } } plugins.each { name, path -> def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile() include ":$name" project(":$name").projectDir = pluginDirectory } ``` 8. 在Android Studio的顶部菜单栏选择`File` -> `Sync Project with Gradle Files`,等待同步完成。 9. 现在你就可以在你的Flutter项目使用Android平台了。可以在Flutter项目添加以下代码测试一下: ``` import android.os.Bundle; import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.android.FlutterFragmentActivity; public class MainActivity extends FlutterFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } ``` 这是一个简单的Android Activity类,可以在Flutter项目使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值