├── .idea
├── .name
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
├── jarRepositories.xml
└── codeStyles
│ └── Project.xml
├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── 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
│ │ │ ├── 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
│ │ │ └── com
│ │ │ └── luopeng
│ │ │ └── serialport
│ │ │ ├── MainActivity.java
│ │ │ └── SerialUtils.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── luopeng
│ │ │ └── serialport
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── luopeng
│ │ └── serialport
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── SerialPortUtils
├── .gitignore
├── consumer-rules.pro
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── luopeng
│ │ │ └── utils
│ │ │ └── serialport
│ │ │ ├── DataUtil.java
│ │ │ └── SerialPortManager.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── luopeng
│ │ │ └── utils
│ │ │ └── serialport
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── luopeng
│ │ └── utils
│ │ └── serialport
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/.idea/.name:
--------------------------------------------------------------------------------
1 | SerialPort
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/SerialPortUtils/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/SerialPortUtils/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':SerialPortUtils'
2 | include ':app'
3 | rootProject.name = "SerialPort"
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SerialPort
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/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/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luopeng11/SerialPortUtils/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/luopeng11/SerialPortUtils/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/luopeng11/SerialPortUtils/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/SerialPortUtils/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | /
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SerialPortUtils
2 | Android 串口通信(ttl&232$485&usb)DEMO 解决拼包问题
3 | 1、Android串口数据传输过大或读取速率较慢会导致写入时缓冲区覆盖重写,导致数据丢失,所以处理数据等耗时操作需开子线程进行;
4 | 2、此dmo可以解决分包问题,依据帧尾 帧头将分散数据拼接成一帧完整数据;
5 | 3、通信不频繁情况下,一般不会出现粘包问题;
6 | 4、要解决粘包问题,数据协议每一帧须包含帧头、帧尾、帧长度。
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Feb 06 14:34:03 CST 2021
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-6.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/luopeng/serialport/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.serialport;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/SerialPortUtils/src/test/java/com/luopeng/utils/serialport/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.utils.serialport;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/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
--------------------------------------------------------------------------------
/SerialPortUtils/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
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/luopeng/serialport/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.serialport;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 | assertEquals("com.luopeng.serialport", appContext.getPackageName());
25 | }
26 | }
--------------------------------------------------------------------------------
/SerialPortUtils/src/androidTest/java/com/luopeng/utils/serialport/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.utils.serialport;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 | assertEquals("com.luopeng.utils.serialport.test", appContext.getPackageName());
25 | }
26 | }
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/SerialPortUtils/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 30
5 | buildToolsVersion "30.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 19
9 | targetSdkVersion 30
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
14 | consumerProguardFiles "consumer-rules.pro"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: "libs", include: ["*.jar"])
27 | implementation 'androidx.appcompat:appcompat:1.2.0'
28 | testImplementation 'junit:junit:4.12'
29 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
30 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
31 |
32 |
33 | implementation 'com.licheedev:android-serialport:2.1.1'
34 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/luopeng/serialport/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.serialport;
2 |
3 | import androidx.appcompat.app.AppCompatActivity;
4 |
5 | import android.graphics.Bitmap;
6 | import android.os.Bundle;
7 |
8 | public class MainActivity extends AppCompatActivity implements SerialUtils.MessageListener {
9 |
10 | private SerialUtils mSerialUtils;
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_main);
16 | mSerialUtils = new SerialUtils();
17 | mSerialUtils.setMessageListener(this);
18 | mSerialUtils.init(this);
19 | }
20 |
21 | @Override
22 | protected void onDestroy() {
23 | super.onDestroy();
24 | mSerialUtils.relese();
25 | }
26 |
27 | @Override
28 | public void onTemperature(float temperature) {
29 |
30 | }
31 |
32 | @Override
33 | public void onMessage(byte[] frameData) {
34 |
35 | }
36 |
37 | @Override
38 | public void onTemperatureBimap(Bitmap bitmap) {
39 |
40 | }
41 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 30
5 | buildToolsVersion "30.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.luopeng.serialport"
9 | minSdkVersion 19
10 | targetSdkVersion 30
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: "libs", include: ["*.jar"])
27 | implementation 'androidx.appcompat:appcompat:1.2.0'
28 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
29 | implementation project(path: ':SerialPortUtils')
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
33 |
34 | }
--------------------------------------------------------------------------------
/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=-Xmx2048m
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
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/SerialPortUtils/src/main/java/com/luopeng/utils/serialport/DataUtil.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.utils.serialport;
2 |
3 | public class DataUtil {
4 | //-------------------------------------------------------
5 | static public int isOdd(int num) {
6 | return num & 0x1;
7 | }
8 |
9 | //-------------------------------------------------------
10 | static public int HexToInt(String inHex) {
11 | return Integer.parseInt(inHex, 16);
12 | }
13 |
14 | //-------------------------------------------------------
15 | static public byte HexToByte(String inHex) {
16 | return (byte) Integer.parseInt(inHex, 16);
17 | }
18 |
19 | //-------------------------------------------------------
20 | static public String Byte2Hex(Byte inByte) {
21 | return String.format("%02x", inByte).toUpperCase();
22 | }
23 |
24 | //-------------------------------------------------------
25 | static public String ByteArrToHex(byte[] inBytArr) {
26 | StringBuilder strBuilder = new StringBuilder();
27 | int j = inBytArr.length;
28 | for (int i = 0; i < j; i++) {
29 | strBuilder.append(Byte2Hex(inBytArr[i]));
30 | strBuilder.append(" ");
31 | }
32 | return strBuilder.toString();
33 | }
34 |
35 | //-------------------------------------------------------
36 | static public String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) {
37 | StringBuilder strBuilder = new StringBuilder();
38 | int j = byteCount;
39 | for (int i = offset; i < j; i++) {
40 | strBuilder.append(Byte2Hex(inBytArr[i]));
41 | }
42 | return strBuilder.toString();
43 | }
44 |
45 | //-------------------------------------------------------
46 | static public byte[] HexToByteArr(String inHex) {
47 | int hexlen = inHex.length();
48 | byte[] result;
49 | if (isOdd(hexlen) == 1) {
50 | hexlen++;
51 | result = new byte[(hexlen / 2)];
52 | inHex = "0" + inHex;
53 | } else {
54 | result = new byte[(hexlen / 2)];
55 | }
56 | int j = 0;
57 | for (int i = 0; i < hexlen; i += 2) {
58 | result[j] = HexToByte(inHex.substring(i, i + 2));
59 | j++;
60 | }
61 | return result;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | xmlns:android
32 |
33 | ^$
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | xmlns:.*
43 |
44 | ^$
45 |
46 |
47 | BY_NAME
48 |
49 |
50 |
51 |
52 |
53 |
54 | .*:id
55 |
56 | http://schemas.android.com/apk/res/android
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | .*:name
66 |
67 | http://schemas.android.com/apk/res/android
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | name
77 |
78 | ^$
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 | style
88 |
89 | ^$
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 | .*
99 |
100 | ^$
101 |
102 |
103 | BY_NAME
104 |
105 |
106 |
107 |
108 |
109 |
110 | .*
111 |
112 | http://schemas.android.com/apk/res/android
113 |
114 |
115 | ANDROID_ATTRIBUTE_ORDER
116 |
117 |
118 |
119 |
120 |
121 |
122 | .*
123 |
124 | .*
125 |
126 |
127 | BY_NAME
128 |
129 |
130 |
131 |
132 |
133 |
134 |
--------------------------------------------------------------------------------
/SerialPortUtils/src/main/java/com/luopeng/utils/serialport/SerialPortManager.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.utils.serialport;
2 |
3 | import android.serialport.SerialPort;
4 | import android.util.Log;
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.io.OutputStream;
8 |
9 | public class SerialPortManager {
10 | private static final String TAG = SerialPortManager.class.getSimpleName();
11 |
12 | /**
13 | * 串口,默认的8n1
14 | *
15 | * @param devicePath 串口设备文件
16 | * @param baudrate 波特率
17 | * @throws SecurityException
18 | * @throws IOException
19 | */
20 | public SerialPortManager(String devicePath, int baudrate) {
21 | this(devicePath, baudrate, 8, 0, 1, 0);
22 | }
23 |
24 | /**
25 | * 串口
26 | *
27 | * @param devicePath 串口设备文件
28 | * @param baudrate 波特率
29 | * @param dataBits 数据位;默认8,可选值为5~8
30 | * @param parity 奇偶校验;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
31 | * @param stopBits 停止位;默认1;1:1位停止位;2:2位停止位
32 | * @throws SecurityException
33 | * @throws IOException
34 | */
35 | public SerialPortManager(String devicePath, int baudrate, int dataBits, int parity, int stopBits) {
36 | this(devicePath, baudrate, dataBits, parity, stopBits, 0);
37 | }
38 | /**
39 | * 串口
40 | *
41 | * @param devicePath 串口设备文件
42 | * @param baudrate 波特率
43 | * @param dataBits 数据位;默认8,可选值为5~8
44 | * @param parity 奇偶校验;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
45 | * @param stopBits 停止位;默认1;1:1位停止位;2:2位停止位
46 | * @param flags 默认0
47 | * @throws SecurityException
48 | * @throws IOException
49 | */
50 | public SerialPortManager(String devicePath, int baudrate, int dataBits, int parity, int stopBits, int flags) {
51 | try {
52 | SerialPort serialPort = SerialPort //
53 | .newBuilder(devicePath, baudrate) // 串口地址地址,波特率
54 | .parity(parity) // 校验位;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN)
55 | .dataBits(dataBits) // 数据位,默认8;可选值为5~8
56 | .stopBits(stopBits) // 停止位,默认1;1:1位停止位;2:2位停止位
57 | .flags(flags) //标志位
58 | .build();
59 | mSerialPort = serialPort;
60 | mOutputStream = mSerialPort.getOutputStream();
61 | mInputStream = mSerialPort.getInputStream();
62 |
63 | /* Create a receiving thread */
64 | mReadThread = new ReadThread();
65 | mReadThread.start();
66 | } catch (Exception e) {
67 | Log.e(TAG, "SerialPortManager初始化出错:" + e.toString());
68 | e.printStackTrace();
69 | }
70 |
71 | }
72 |
73 | private SerialPort mSerialPort = null;
74 | protected OutputStream mOutputStream;
75 | private InputStream mInputStream;
76 | private ReadThread mReadThread;
77 |
78 |
79 |
80 | /**
81 | * 关闭串口
82 | */
83 | public void closeSerialPort() {
84 | if (mReadThread != null) mReadThread.interrupt();
85 | if (mSerialPort != null) {
86 | mSerialPort.close();
87 | mSerialPort = null;
88 | }
89 | }
90 |
91 | /**
92 | * 发送16进制字符串 中间无空格
93 | *
94 | * @param hex
95 | * @return
96 | */
97 | public boolean sendHex(String hex) {
98 | send(DataUtil.HexToByteArr(hex));
99 | return false;
100 | }
101 |
102 | /**
103 | * 发送二进制数组
104 | *
105 | * @param data
106 | * @return
107 | */
108 | public boolean send(byte[] data) {
109 | try {
110 | if (mOutputStream != null) {
111 | mOutputStream.write(data);
112 | return true;
113 | }
114 | } catch (IOException e) {
115 | e.printStackTrace();
116 | }
117 | return false;
118 | }
119 |
120 |
121 | OnSerialPortDataListener mOnSerialPortDataListener;
122 |
123 | public SerialPortManager setOnSerialPortDataListener(OnSerialPortDataListener onSerialPortDataListener) {
124 | this.mOnSerialPortDataListener = onSerialPortDataListener;
125 | return this;
126 | }
127 |
128 | /**
129 | * 数据监听
130 | */
131 | public interface OnSerialPortDataListener {
132 | void onDataReceived(final byte[] buffer, final int size);
133 | }
134 |
135 | /**
136 | * 数据读取线程
137 | */
138 | private class ReadThread extends Thread {
139 | @Override
140 | public void run() {
141 | super.run();
142 | while (!isInterrupted()) {
143 | int size;
144 | try {
145 | byte[] buffer = new byte[1024 * 2];
146 | if (mInputStream == null) return;
147 | size = mInputStream.read(buffer);
148 | if (size > 0 && mOnSerialPortDataListener != null) {
149 | mOnSerialPortDataListener.onDataReceived(buffer, size);
150 | }
151 | } catch (IOException e) {
152 | e.printStackTrace();
153 | return;
154 | }
155 | }
156 | }
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/luopeng/serialport/SerialUtils.java:
--------------------------------------------------------------------------------
1 | package com.luopeng.serialport;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 | import android.graphics.Bitmap;
6 | import android.util.Log;
7 | import com.luopeng.utils.serialport.DataUtil;
8 | import com.luopeng.utils.serialport.SerialPortManager;
9 |
10 | import java.io.ByteArrayOutputStream;
11 | import java.io.IOException;
12 | import java.util.concurrent.BlockingQueue;
13 | import java.util.concurrent.LinkedBlockingQueue;
14 |
15 | import static android.content.Context.MODE_PRIVATE;
16 |
17 | public class SerialUtils implements SerialPortManager.OnSerialPortDataListener {
18 |
19 |
20 | public interface MessageListener {
21 | void onTemperature(float temperature);
22 |
23 | void onMessage(byte[] frameData);
24 |
25 | void onTemperatureBimap(Bitmap bitmap);
26 | }
27 |
28 | private static final String TAG = SerialUtils.class.getSimpleName();
29 |
30 | private float mTemperature = 0;//温度补偿
31 | private SerialPortManager serialPortManager;
32 | private MessageListener mMessageListener;
33 | private ProgressDataThread mProgressDataThread;
34 |
35 | public void setMessageListener(MessageListener messageListener) {
36 | mMessageListener = messageListener;
37 | }
38 |
39 |
40 | public void init(Context context) {
41 | serialPortManager = new SerialPortManager("dev/ttyUSB0",115200).setOnSerialPortDataListener(this);
42 | mProgressDataThread = new ProgressDataThread();
43 | mProgressDataThread.start();
44 | SharedPreferences sp = context.getSharedPreferences(context.getPackageName() + "_preferences", MODE_PRIVATE);
45 | mTemperature = Float.parseFloat(sp.getString("TEMPERATURE", "0"));
46 | }
47 |
48 | public void relese() {
49 | // if (mQueryTemperatureThread != null) mQueryTemperatureThread.interrupt();
50 | if (serialPortManager != null) serialPortManager.closeSerialPort();
51 | if (mProgressDataThread != null) mProgressDataThread.interrupt();
52 | }
53 |
54 |
55 | //温度查询启动标志
56 | private boolean startDetection = false;
57 |
58 | public void switchDetection(boolean detecting) {
59 | this.startDetection = detecting;
60 | if (startDetection && !isQueryTemperature) queryTemperature();
61 | }
62 |
63 | //当前是否正在查询温度 记录状态 防止同时发送俩次查询命令导致数据叠加重写
64 | private boolean isQueryTemperature = false;
65 |
66 | private void queryTemperature() {
67 | Log.e(TAG, "发送获取温度命令");
68 | isQueryTemperature = true;
69 | sendHex("EEE10155FFFCFDFF");
70 | }
71 |
72 |
73 | /**
74 | * 发送十六进制字符串数据 无空格 如:AA5501B3FF
75 | */
76 | public boolean sendHex(String hex) {
77 | if (serialPortManager != null) return serialPortManager.sendHex(hex);
78 | return false;
79 | }
80 |
81 | public BlockingQueue dataQueue = new LinkedBlockingQueue<>();
82 |
83 |
84 | @Override
85 | public void onDataReceived(byte[] buffer, int size) {
86 | byte[] bytes = new byte[size];
87 | System.arraycopy(buffer, 0, bytes, 0, size);
88 | dataQueue.add(bytes);
89 | }
90 |
91 |
92 | private class ProgressDataThread extends Thread {
93 | public void run() {
94 | while (!isInterrupted()) {
95 | try {
96 | byte[] take = dataQueue.take();
97 | progressData(take);
98 | } catch (InterruptedException e) {
99 | e.printStackTrace();
100 | } catch (IOException e) {
101 | e.printStackTrace();
102 | }
103 | }
104 | }
105 | }
106 |
107 | private ByteArrayOutputStream baos = new ByteArrayOutputStream();
108 |
109 | private void progressData(byte[] buffer) throws IOException {
110 | baos.write(buffer);
111 | //其他数据帧 帧尾0xFF 0xFC 0xFD 0xFF
112 | boolean isFrameEnd = buffer.length > 4 && buffer[buffer.length - 1] == (byte) 0xFF && buffer[buffer.length - 2] == (byte) 0xFD && buffer[buffer.length - 3] == (byte) 0xFC && buffer[buffer.length - 4] == (byte) 0xFF;
113 |
114 | //温度帧 帧尾 01 00 C6 82(探测器编号)
115 | boolean isTemperatureFrameEnd = buffer.length > 4 && buffer[buffer.length - 1] == (byte) 0x82 && buffer[buffer.length - 2] == (byte) 0xC6 && buffer[buffer.length - 3] == (byte) 0x00 && buffer[buffer.length - 4] == (byte) 0x01;
116 |
117 | if (isTemperatureFrameEnd) {
118 | byte[] data = baos.toByteArray();
119 | Log.d(TAG, "=====获取温度帧:" + data.length);
120 | if (9927 == data.length) {//不丢失数据才解析
121 | float temperature = progressTemperature(data);
122 | if (mMessageListener != null )
123 | mMessageListener.onTemperatureBimap(createThermodynamicBitmap(data, Math.max(temperature, 37.3f)));
124 | if (mMessageListener != null)
125 | mMessageListener.onTemperature(temperature + mTemperature);
126 | } else {
127 | Log.e(TAG, data.length + "=====异常温度帧: " + DataUtil.ByteArrToHex(buffer));
128 | }
129 | baos.reset();
130 |
131 | //是否继续查询温度
132 | if (startDetection) {
133 | try {
134 | Thread.sleep(1000);
135 | } catch (InterruptedException e) {
136 | e.printStackTrace();
137 | }
138 | queryTemperature();
139 | } else {
140 | isQueryTemperature = false;
141 | }
142 | } else if (isFrameEnd) {
143 | byte[] data = baos.toByteArray();
144 | if (mMessageListener != null) mMessageListener.onMessage(data);
145 | baos.reset();
146 | }
147 | }
148 |
149 | private Bitmap createThermodynamicBitmap(byte[] data, float max) {
150 |
151 | int width = 80;
152 | int height = 62;
153 | int[] mArrayColor = new int[width * height];
154 | for (int i = 0; i < mArrayColor.length; i++) {
155 | int dataI = i * 2 + 1;
156 | float temp = (bytesToInt(data[dataI], data[dataI + 1]) * 1.0f - 2731) / 10;
157 | float value = temp < 25 ? 0 : (temp / max);
158 | mArrayColor[i] = getColor(value);
159 | }
160 | double scaleRate = 1;
161 | Bitmap img = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
162 | img.setPixels(mArrayColor, 0, (int) (width), 0, 0, (int) (width * scaleRate), (int) (height * scaleRate));
163 | return img;
164 | }
165 |
166 | /**
167 | * 能量色条值获取
168 | *
169 | * @param value 实际数据值
170 | * @return 对应能量色条颜色数组[r, g, b]
171 | */
172 | public static int getColor(float value) {
173 | int sr1 = 0x00, sg1 = 0x00, sb1 = 0x00;
174 | int sr2 = 0x46, sg2 = 0xb7, sb2 = 0xec;
175 | int sr3 = 0x22, sg3 = 0xfe, sb3 = 0x2c;
176 | int sr4 = 0xff, sg4 = 0xea, sb4 = 0x00;
177 | int sr5 = 0xff, sg5 = 0x4e, sb5 = 0x00;
178 | int r = 0, g = 0, b = 0;
179 |
180 | if (value < 0.15) {
181 | r = (int) (sr1 + (sr2 - sr1) * (value / 0.15));
182 | g = (int) (sg1 + (sg2 - sg1) * (value / 0.15));
183 | b = (int) (sb1 + (sb2 - sb1) * (value / 0.15));
184 | } else if (value < 0.5) {
185 | r = (int) (sr2 + (sr3 - sr2) * ((value - 0.15) / 0.35));
186 | g = (int) (sg2 + (sg3 - sg2) * ((value - 0.15) / 0.35));
187 | b = (int) (sb2 + (sb3 - sb2) * ((value - 0.15) / 0.35));
188 | } else if (value < 0.75) {
189 | r = (int) (sr3 + (sr4 - sr3) * ((value - 0.5) / 0.25));
190 | g = (int) (sg3 + (sg4 - sg3) * ((value - 0.5) / 0.25));
191 | b = (int) (sb3 + (sb4 - sb3) * ((value - 0.5) / 0.25));
192 | } else {
193 | r = (int) (sr4 + (sr5 - sr4) * ((value - 0.75) / 0.25));
194 | g = (int) (sg4 + (sg5 - sg4) * ((value - 0.75) / 0.25));
195 | b = (int) (sb4 + (sb5 - sb4) * ((value - 0.75) / 0.25));
196 | }
197 | return 128 << 24 | r << 16 | g << 8 | b;
198 | }
199 |
200 |
201 | private float progressTemperature(byte[] data) {
202 | float maxTemp = 0;
203 | for (int i = 0; i < 62 * 80; i++) {
204 | int dataI = i * 2 + 1;
205 | float temp = (bytesToInt(data[dataI], data[dataI + 1]) * 1.0f - 2731) / 10;
206 | if (temp > maxTemp) {
207 | maxTemp = temp;
208 | }
209 | }
210 | return maxTemp;
211 | }
212 |
213 | /**
214 | * 2byte转int
215 | */
216 | private int bytesToInt(byte b, byte b2) {
217 | int ch1 = b & 0xff;
218 | int ch2 = b2 & 0xff;
219 | return (ch1 << 8) | ch2;
220 | }
221 |
222 | }
223 |
--------------------------------------------------------------------------------