diff --git a/app/src/main/java/com/common/commonlibtest/DemoActivity.java b/app/src/main/java/com/common/commonlibtest/DemoActivity.java
index 27d5150..1afb0c6 100644
--- a/app/src/main/java/com/common/commonlibtest/DemoActivity.java
+++ b/app/src/main/java/com/common/commonlibtest/DemoActivity.java
@@ -14,6 +14,7 @@ import com.common.commonlib.net.callback.RequestNoResultCallBack;
import com.common.commonlib.net.callback.RequestResultCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
+import com.common.commonlib.serialport.SerialPortUtil;
import com.common.commonlib.utils.BaseUtils;
import com.common.commonlib.utils.FTPUtils;
import com.common.commonlib.utils.MMKVUtils;
@@ -133,7 +134,12 @@ public class DemoActivity extends AppCompatActivity {
binding.mmkv.setOnClickListener(l -> {
MMKVUtils.INSTANCE.put("key", 100, "aa");
int value = MMKVUtils.INSTANCE.getInt("key", "bb");
- Toast.makeText(DemoActivity.this, ""+value, Toast.LENGTH_SHORT).show();
+ Toast.makeText(DemoActivity.this, "" + value, Toast.LENGTH_SHORT).show();
+ });
+
+ binding.port.setOnClickListener(l -> {
+ SerialPortUtil serialPortUtil = new SerialPortUtil();
+ serialPortUtil.openSerialPort();
});
}
}
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 5914f02..e813bf9 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -106,4 +106,16 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.9" />
+
+
\ No newline at end of file
diff --git a/commonLib/build.gradle b/commonLib/build.gradle
index d1d49f2..a3011c1 100644
--- a/commonLib/build.gradle
+++ b/commonLib/build.gradle
@@ -2,6 +2,17 @@ apply from: "${rootProject.rootDir}/buildCommon/commonLibConfig.gradle"
project.ext.setLibDefaultConfig project
android {
+ defaultConfig {
+ ndk {
+ abiFilters 'armeabi', 'armeabi-v7a', 'x86'
+ }
+ }
+ sourceSets {
+ main {
+ jni.srcDirs = []
+ jniLibs.srcDirs = ['libs']
+ }
+ }
buildTypes {
debug {
minifyEnabled false
@@ -25,6 +36,8 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
+ implementation 'com.aill:AndroidSerialPort:1.0.8'
+
implementation(rootProject.ext.dependencies.mmkv)
// 添加kotlin依赖
diff --git a/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt b/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt
index f556638..37f7224 100644
--- a/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt
+++ b/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt
@@ -3,10 +3,8 @@ package com.common.commonlib
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
-import com.common.commonlib.log.Logger.d
import com.common.commonlib.utils.BaseUtils
import com.tencent.mmkv.MMKV
-import okhttp3.logging.HttpLoggingInterceptor
/**
* 基础Application
diff --git a/commonLib/src/main/java/com/common/commonlib/serialport/Cmd.java b/commonLib/src/main/java/com/common/commonlib/serialport/Cmd.java
new file mode 100644
index 0000000..f7f74b0
--- /dev/null
+++ b/commonLib/src/main/java/com/common/commonlib/serialport/Cmd.java
@@ -0,0 +1,9 @@
+package com.common.commonlib.serialport;
+
+/**
+ * @author by AllenJ on 2018/5/3.
+ */
+
+public interface Cmd {
+ String OPEN_DOOR = "010100000000FFFF";
+}
diff --git a/commonLib/src/main/java/com/common/commonlib/serialport/DataUtils.java b/commonLib/src/main/java/com/common/commonlib/serialport/DataUtils.java
new file mode 100644
index 0000000..458be46
--- /dev/null
+++ b/commonLib/src/main/java/com/common/commonlib/serialport/DataUtils.java
@@ -0,0 +1,139 @@
+package com.common.commonlib.serialport;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 串口数据转换工具类
+ * Created by Administrator on 2016/6/2.
+ */
+public class DataUtils {
+ //-------------------------------------------------------
+ // 判断奇数或偶数,位运算,最后一位是1则为奇数,为0是偶数
+ public static int isOdd(int num) {
+ return num & 1;
+ }
+
+ //-------------------------------------------------------
+ //Hex字符串转int
+ public static int HexToInt(String inHex) {
+ return Integer.parseInt(inHex, 16);
+ }
+
+ public static String IntToHex(int intHex){
+ return Integer.toHexString(intHex);
+ }
+
+ //-------------------------------------------------------
+ //Hex字符串转byte
+ public static byte HexToByte(String inHex) {
+ return (byte) Integer.parseInt(inHex, 16);
+ }
+
+ //-------------------------------------------------------
+ //1字节转2个Hex字符
+ public static String Byte2Hex(Byte inByte) {
+ return String.format("%02x", new Object[]{inByte}).toUpperCase();
+ }
+
+ //-------------------------------------------------------
+ //字节数组转转hex字符串
+ public static String ByteArrToHex(byte[] inBytArr) {
+ StringBuilder strBuilder = new StringBuilder();
+ for (byte valueOf : inBytArr) {
+ strBuilder.append(Byte2Hex(Byte.valueOf(valueOf)));
+ strBuilder.append(" ");
+ }
+ return strBuilder.toString();
+ }
+
+ //-------------------------------------------------------
+ //字节数组转转hex字符串,可选长度
+ public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) {
+ StringBuilder strBuilder = new StringBuilder();
+ int j = byteCount;
+ for (int i = offset; i < j; i++) {
+ strBuilder.append(Byte2Hex(Byte.valueOf(inBytArr[i])));
+ }
+ return strBuilder.toString();
+ }
+
+ //-------------------------------------------------------
+ //转hex字符串转字节数组
+ public static byte[] HexToByteArr(String inHex) {
+ byte[] result;
+ int hexlen = inHex.length();
+ if (isOdd(hexlen) == 1) {
+ hexlen++;
+ result = new byte[(hexlen / 2)];
+ inHex = "0" + inHex;
+ } else {
+ result = new byte[(hexlen / 2)];
+ }
+ int j = 0;
+ for (int i = 0; i < hexlen; i += 2) {
+ result[j] = HexToByte(inHex.substring(i, i + 2));
+ j++;
+ }
+ return result;
+ }
+
+ /**
+ * 按照指定长度切割字符串
+ *
+ * @param inputString 需要切割的源字符串
+ * @param length 指定的长度
+ * @return
+ */
+ public static List getDivLines(String inputString, int length) {
+ List divList = new ArrayList<>();
+ int remainder = (inputString.length()) % length;
+ // 一共要分割成几段
+ int number = (int) Math.floor((inputString.length()) / length);
+ for (int index = 0; index < number; index++) {
+ String childStr = inputString.substring(index * length, (index + 1) * length);
+ divList.add(childStr);
+ }
+ if (remainder > 0) {
+ String cStr = inputString.substring(number * length, inputString.length());
+ divList.add(cStr);
+ }
+ return divList;
+ }
+
+ /**
+ * 计算长度,两个字节长度
+ *
+ * @param val value
+ * @return 结果
+ */
+ public static String twoByte(String val) {
+ if (val.length() > 4) {
+ val = val.substring(0, 4);
+ } else {
+ int l = 4 - val.length();
+ for (int i = 0; i < l; i++) {
+ val = "0" + val;
+ }
+ }
+ return val;
+ }
+
+ /**
+ * 校验和
+ *
+ * @param cmd 指令
+ * @return 结果
+ */
+ public static String sum(String cmd) {
+ List cmdList = DataUtils.getDivLines(cmd, 2);
+ int sumInt = 0;
+ for (String c : cmdList) {
+ sumInt += DataUtils.HexToInt(c);
+ }
+ String sum = DataUtils.IntToHex(sumInt);
+ sum = DataUtils.twoByte(sum);
+ cmd += sum;
+ return cmd.toUpperCase();
+ }
+}
diff --git a/commonLib/src/main/java/com/common/commonlib/serialport/SerialPortUtil.java b/commonLib/src/main/java/com/common/commonlib/serialport/SerialPortUtil.java
new file mode 100644
index 0000000..b862817
--- /dev/null
+++ b/commonLib/src/main/java/com/common/commonlib/serialport/SerialPortUtil.java
@@ -0,0 +1,113 @@
+package com.common.commonlib.serialport;
+
+import android.util.Log;
+
+import com.aill.androidserialport.SerialPort;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @author by AllenJ on 2018/4/20.
+ *
+ * 通过串口用于接收或发送数据
+ */
+public class SerialPortUtil {
+ private SerialPort serialPort = null;
+ private InputStream inputStream = null;
+ private OutputStream outputStream = null;
+ private ReceiveThread mReceiveThread = null;
+ private boolean isStart = false;
+
+ /**
+ * 打开串口,接收数据
+ * 通过串口,接收单片机发送来的数据
+ */
+ public void openSerialPort() {
+ try {
+ // 设置当前设备SU命令的位置
+ SerialPort.setSuPath("/system/xbin/su");
+ serialPort = new SerialPort(new File("/dev/ttyS0"), 9600, 0);
+ //调用对象SerialPort方法,获取串口中"读和写"的数据流
+ inputStream = serialPort.getInputStream();
+ outputStream = serialPort.getOutputStream();
+ isStart = true;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ getSerialPort();
+ }
+
+ /**
+ * 关闭串口
+ * 关闭串口中的输入输出流
+ */
+ public void closeSerialPort() {
+ Log.i("test", "关闭串口");
+ try {
+ if (inputStream != null) {
+ inputStream.close();
+ }
+ if (outputStream != null) {
+ outputStream.close();
+ }
+ isStart = false;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ /**
+ * 发送数据
+ * 通过串口,发送数据到单片机
+ *
+ * @param data 要发送的数据
+ */
+ public void sendSerialPort(String data) {
+ try {
+ byte[] sendData = DataUtils.HexToByteArr(data);
+ outputStream.write(sendData);
+ outputStream.flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void getSerialPort() {
+ if (mReceiveThread == null) {
+ mReceiveThread = new ReceiveThread();
+ }
+ mReceiveThread.start();
+ }
+
+ /**
+ * 接收串口数据的线程
+ */
+ private class ReceiveThread extends Thread {
+ @Override
+ public void run() {
+ super.run();
+ //条件判断,只要条件为true,则一直执行这个线程
+ while (isStart) {
+ if (inputStream == null) {
+ return;
+ }
+ byte[] readData = new byte[1024];
+ try {
+ int size = inputStream.read(readData);
+ if (size > 0) {
+ String readString = DataUtils.ByteArrToHex(readData, 0, size);
+// EventBus.getDefault().post(readString);
+ }
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}