├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── layout │ │ │ └── activity_main.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── me │ │ └── f1reking │ │ └── android_serialport │ │ └── MainActivity.java ├── proguard-rules.pro └── build.gradle ├── serialportlib ├── consumer-rules.pro ├── .gitignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── me │ │ │ └── f1reking │ │ │ └── serialportlib │ │ │ ├── listener │ │ │ ├── Status.java │ │ │ ├── ISerialPortDataListener.java │ │ │ └── IOpenSerialPortListener.java │ │ │ ├── entity │ │ │ ├── STOPB.java │ │ │ ├── PARITY.java │ │ │ ├── FLOWCON.java │ │ │ ├── DATAB.java │ │ │ ├── BAUDRATE.java │ │ │ ├── Driver.java │ │ │ └── Device.java │ │ │ ├── util │ │ │ └── ByteUtils.java │ │ │ ├── SerialPortReceivedThread.java │ │ │ ├── SerialPortFinder.java │ │ │ └── SerialPortHelper.java │ │ └── jni │ │ ├── SerialPort.h │ │ ├── termios.h │ │ └── SerialPort.c ├── build.gradle ├── proguard-rules.pro └── CMakeLists.txt ├── 3.webp ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── jitpack.gradle ├── gradle.properties ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /serialportlib/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /serialportlib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /3.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/3.webp -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':serialportlib' 2 | rootProject.name='Android-SerialPort' 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /serialportlib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SerialPortLib 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /serialportlib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeekBugs/Android-SerialPort/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Apr 19 16:13:52 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 7 | -------------------------------------------------------------------------------- /jitpack.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | group = 'com.github.f1reking' 4 | 5 | tasks.withType(JavaCompile) { 6 | options.encoding = "UTF-8" 7 | } 8 | 9 | task sourcesJar(type: Jar) { 10 | from android.sourceSets.main.java.srcDirs 11 | classifier = 'sources' 12 | } 13 | 14 | task javadoc(type: Javadoc) { 15 | failOnError false 16 | source = android.sourceSets.main.java.sourceFiles 17 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 18 | classpath += configurations.compile 19 | } 20 | 21 | 22 | task javadocJar(type: Jar, dependsOn: javadoc) { 23 | classifier = 'javadoc' 24 | from javadoc.destinationDir 25 | } 26 | 27 | artifacts { 28 | archives sourcesJar 29 | archives javadocJar 30 | } -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | Android-SerialPort 19 | 20 | -------------------------------------------------------------------------------- /serialportlib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.2" 6 | 7 | defaultConfig { 8 | minSdkVersion 16 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | consumerProguardFiles 'consumer-rules.pro' 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | externalNativeBuild { 24 | cmake { 25 | path "CMakeLists.txt" 26 | } 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | 33 | } 34 | apply from: '../jitpack.gradle' 35 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /serialportlib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | #008577 20 | #00574B 21 | #D81B60 22 | 23 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/listener/Status.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.listener; 17 | 18 | /** 19 | * @author HuangYH 20 | * @date 2019/12/2 18:17 21 | * @Description 22 | */ 23 | public enum Status { 24 | 25 | NO_READ_WRITE_PERMISSION, 26 | OPEN_FAIL 27 | } 28 | -------------------------------------------------------------------------------- /serialportlib/src/main/jni/SerialPort.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class android_serialport_api_SerialPort */ 4 | 5 | #ifndef _Included_com_android_serialport_SerialPort 6 | #define _Included_com_android_serialport_SerialPort 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: android_serialport_api_SerialPort 12 | * Method: open 13 | * Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor; 14 | */ 15 | JNIEXPORT jobject JNICALL Java_me_f1reking_serialportlib_SerialPortHelper_nativeOpen 16 | (JNIEnv *, jclass, jstring, jint, jint, jint, jint, jint, jint); 17 | 18 | /* 19 | * Class: android_serialport_api_SerialPort 20 | * Method: close 21 | * Signature: ()V 22 | */ 23 | JNIEXPORT void JNICALL Java_me_f1reking_serialportlib_SerialPortHelper_nativeClose 24 | (JNIEnv *, jobject); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/listener/ISerialPortDataListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.listener; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/11/1 10:58 21 | * @Description 串口消息监听 22 | */ 23 | public interface ISerialPortDataListener { 24 | 25 | void onDataReceived(byte[] bytes); 26 | 27 | void onDataSend(byte[] bytes); 28 | } 29 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/listener/IOpenSerialPortListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.listener; 17 | 18 | import java.io.File; 19 | 20 | /** 21 | * @author F1ReKing 22 | * @date 2019/11/1 10:49 23 | * @Description 串口打开状态监听 24 | */ 25 | public interface IOpenSerialPortListener { 26 | 27 | void onSuccess(File device); 28 | 29 | void onFail(File device, Status status); 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | 21 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/entity/STOPB.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.entity; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/10/31 18:04 21 | * @Description 串口停止位定义 22 | */ 23 | public enum STOPB { 24 | 25 | /** 26 | * 1位停止位 27 | */ 28 | B1(1), 29 | 30 | /** 31 | * 2位停止位 32 | */ 33 | B2(2); 34 | 35 | int stopBit; 36 | 37 | STOPB(int stopBit) { 38 | this.stopBit = stopBit; 39 | } 40 | 41 | public int getStopBit() { 42 | return this.stopBit; 43 | } 44 | 45 | public static int getStopBit(STOPB stopb) { 46 | return stopb.getStopBit(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/entity/PARITY.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.entity; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/10/31 18:05 21 | * @Description 串口校验位定义 22 | */ 23 | public enum PARITY { 24 | 25 | /** 26 | * 无奇偶校验 27 | */ 28 | NONE(0), 29 | /** 30 | * 奇校验 31 | */ 32 | ODD(1), 33 | /** 34 | * 偶校验 35 | */ 36 | EVEN(2); 37 | 38 | int parity; 39 | 40 | PARITY(int parity) { 41 | this.parity = parity; 42 | } 43 | 44 | public int getParity() { 45 | return this.parity; 46 | } 47 | 48 | public static int getParity(PARITY parity) { 49 | return parity.getParity(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/entity/FLOWCON.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.entity; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/10/31 18:06 21 | * @Description 串口流控定义 22 | */ 23 | public enum FLOWCON { 24 | 25 | /** 26 | * 不使用流控 27 | */ 28 | NONE(0), 29 | /** 30 | * 硬件流控 31 | */ 32 | HARD(1), 33 | /** 34 | * 软件流控 35 | */ 36 | SOFT(2); 37 | 38 | int flowCon; 39 | 40 | FLOWCON(int flowCon) { 41 | this.flowCon = flowCon; 42 | } 43 | 44 | public int getFlowCon() { 45 | return this.flowCon; 46 | } 47 | 48 | public static int getFlowCon(FLOWCON flowcon) { 49 | return flowcon.getFlowCon(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/entity/DATAB.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.entity; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/10/31 18:05 21 | * @Description 串口数据位定义 22 | */ 23 | public enum DATAB { 24 | 25 | /** 26 | * 5位数据位 27 | */ 28 | CS5(5), 29 | /** 30 | * 6位数据位 31 | */ 32 | CS6(6), 33 | /** 34 | * 7位数据位 35 | */ 36 | CS7(7), 37 | /** 38 | * 8位数据位 39 | */ 40 | CS8(8); 41 | 42 | int dataBit; 43 | 44 | DATAB(int dataBit) { 45 | this.dataBit = dataBit; 46 | } 47 | 48 | public int getDataBit() { 49 | return this.dataBit; 50 | } 51 | 52 | public static int getDataBit(DATAB datab) { 53 | return datab.getDataBit(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'com.android.application' 18 | 19 | android { 20 | compileSdkVersion 29 21 | buildToolsVersion "29.0.2" 22 | defaultConfig { 23 | applicationId "me.f1reking.android_serialport" 24 | minSdkVersion 16 25 | targetSdkVersion 29 26 | versionCode 1 27 | versionName "1.0" 28 | } 29 | buildTypes { 30 | release { 31 | minifyEnabled false 32 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 33 | } 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation fileTree(dir: 'libs', include: ['*.jar']) 39 | implementation 'androidx.appcompat:appcompat:1.1.0' 40 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 41 | // implementation 'com.github.F1ReKing:Android-SerialPort:1.3.3' 42 | implementation project(path: ':serialportlib') 43 | } 44 | -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.util; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/11/1 14:08 21 | * @Description 22 | */ 23 | public class ByteUtils { 24 | 25 | public static int isOdd(int num) { 26 | return num & 0x1; 27 | } 28 | 29 | public static int HexToInt(String inHex) { 30 | return Integer.parseInt(inHex, 16); 31 | } 32 | 33 | public static byte HexToByte(String inHex) { 34 | return (byte) Integer.parseInt(inHex, 16); 35 | } 36 | 37 | public static byte[] hexToByteArr(String hex) { 38 | int hexLen = hex.length(); 39 | byte[] result; 40 | if (isOdd(hexLen) == 1) { 41 | hexLen++; 42 | result = new byte[hexLen / 2]; 43 | hex = "0" + hex; 44 | } else { 45 | result = new byte[hexLen / 2]; 46 | } 47 | int j = 0; 48 | for (int i = 0; i < hexLen; i += 2) { 49 | result[j] = HexToByte(hex.substring(i, i + 2)); 50 | j++; 51 | } 52 | return result; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /serialportlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # For more information about using CMake with Android Studio, read the 2 | # documentation: https://d.android.com/studio/projects/add-native-code.html 3 | 4 | # Sets the minimum version of CMake required to build the native library. 5 | 6 | cmake_minimum_required(VERSION 3.4.1) 7 | 8 | # Creates and names a library, sets it as either STATIC 9 | # or SHARED, and provides the relative paths to its source code. 10 | # You can define multiple libraries, and CMake builds them for you. 11 | # Gradle automatically packages shared libraries with your APK. 12 | 13 | #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}) 14 | 15 | add_library( # Sets the name of the library. 16 | serialport 17 | 18 | # Sets the library as a shared library. 19 | SHARED 20 | 21 | # Provides a relative path to your source file(s). 22 | src/main/jni/SerialPort.c) 23 | 24 | # Searches for a specified prebuilt library and stores the path as a 25 | # variable. Because CMake includes system libraries in the search path by 26 | # default, you only need to specify the name of the public NDK library 27 | # you want to add. CMake verifies that the library exists before 28 | # completing its build. 29 | 30 | find_library( # Sets the name of the path variable. 31 | log-lib 32 | 33 | # Specifies the name of the NDK library that 34 | # you want CMake to locate. 35 | log) 36 | 37 | # Specifies libraries CMake should link to your target library. You 38 | # can link multiple libraries, such as libraries you define in this 39 | # build script, prebuilt third-party libraries, or system libraries. 40 | 41 | target_link_libraries( # Specifies the target library. 42 | serialport 43 | 44 | # Links the target library to the log library 45 | # included in the NDK. 46 | ${log-lib}) -------------------------------------------------------------------------------- /serialportlib/src/main/java/me/f1reking/serialportlib/entity/BAUDRATE.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 F1ReKing. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package me.f1reking.serialportlib.entity; 17 | 18 | /** 19 | * @author F1ReKing 20 | * @date 2019/10/31 18:03 21 | * @Description 串口波特率定义 22 | */ 23 | public enum BAUDRATE { 24 | 25 | B0(0), 26 | B50(50), 27 | B75(75), 28 | B110(110), 29 | B134(134), 30 | B150(150), 31 | B200(200), 32 | B300(300), 33 | B600(600), 34 | B1200(1200), 35 | B1800(1800), 36 | B2400(2400), 37 | B4800(4800), 38 | B9600(9600), 39 | B19200(19200), 40 | B38400(38400), 41 | B57600(57600), 42 | B115200(115200), 43 | B230400(230400), 44 | B460800(460800), 45 | B500000(500000), 46 | B576000(576000), 47 | B921600(921600), 48 | B1000000(1000000), 49 | B1152000(1152000), 50 | B1500000(1500000), 51 | B2000000(2000000), 52 | B2500000(2500000), 53 | B3000000(3000000), 54 | B3500000(3500000), 55 | B4000000(4000000); 56 | 57 | int baudrate; 58 | 59 | BAUDRATE(int baudrate) { 60 | this.baudrate = baudrate; 61 | } 62 | 63 | int getBaudrate() { 64 | return this.baudrate; 65 | } 66 | 67 | public static int getBaudrate(BAUDRATE baudrate) { 68 | return baudrate.getBaudrate(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 25 | 26 | 30 | 31 |