[desc]:添加串口功能

[author]:wangyimiao
master
yimiao 3 years ago
parent 9241427370
commit 0038b64fe4

@ -14,6 +14,7 @@ import com.common.commonlib.net.callback.RequestNoResultCallBack;
import com.common.commonlib.net.callback.RequestResultCallBack; import com.common.commonlib.net.callback.RequestResultCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor; import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor; import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
import com.common.commonlib.serialport.SerialPortUtil;
import com.common.commonlib.utils.BaseUtils; import com.common.commonlib.utils.BaseUtils;
import com.common.commonlib.utils.FTPUtils; import com.common.commonlib.utils.FTPUtils;
import com.common.commonlib.utils.MMKVUtils; import com.common.commonlib.utils.MMKVUtils;
@ -135,5 +136,10 @@ public class DemoActivity extends AppCompatActivity {
int value = MMKVUtils.INSTANCE.getInt("key", "bb"); 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();
});
} }
} }

@ -106,4 +106,16 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.9" /> app:layout_constraintVertical_bias="0.9" />
<Button
android:id="@+id/port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="port test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.97" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -2,6 +2,17 @@ apply from: "${rootProject.rootDir}/buildCommon/commonLibConfig.gradle"
project.ext.setLibDefaultConfig project project.ext.setLibDefaultConfig project
android { android {
defaultConfig {
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'x86'
}
}
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs = ['libs']
}
}
buildTypes { buildTypes {
debug { debug {
minifyEnabled false minifyEnabled false
@ -25,6 +36,8 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5' implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-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) implementation(rootProject.ext.dependencies.mmkv)
// kotlin // kotlin

@ -3,10 +3,8 @@ package com.common.commonlib
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import com.common.commonlib.log.Logger.d
import com.common.commonlib.utils.BaseUtils import com.common.commonlib.utils.BaseUtils
import com.tencent.mmkv.MMKV import com.tencent.mmkv.MMKV
import okhttp3.logging.HttpLoggingInterceptor
/** /**
* 基础Application * 基础Application

@ -0,0 +1,9 @@
package com.common.commonlib.serialport;
/**
* @author by AllenJ on 2018/5/3.
*/
public interface Cmd {
String OPEN_DOOR = "010100000000FFFF";
}

@ -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<String> getDivLines(String inputString, int length) {
List<String> 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<String> 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();
}
}

@ -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();
}
}
}
}
}
Loading…
Cancel
Save