Skip to content

Commit

Permalink
1、在不同android版本的模拟器上进行串口测试,并修复代码
Browse files Browse the repository at this point in the history
  • Loading branch information
LyounJAP committed Jan 20, 2022
1 parent aa75623 commit 7839222
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions SerialPort/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ android {
defaultConfig {
minSdk 16
targetSdk 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
4 changes: 4 additions & 0 deletions SerialPort/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="top.lyoun.serialport">

<!--serial need read and write privilege-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package top.lyoun.serialport;

import android.Manifest;
import android.app.Activity;
import android.app.Application;

import androidx.core.app.ActivityCompat;

/**
* @author LyounJAP
* @title: RequestStoragePrivileges
* @projectName AndroidSerialHelper
* @description: dynamic acquire storage read/write privileges
* @date 2022-01-17 16:32
*/
public class RequestStoragePrivileges {

public static void requestStoragePrivileges(Activity activity){
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
1);
}
}
17 changes: 11 additions & 6 deletions SerialPort/src/main/java/top/lyoun/serialport/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public final class SerialPort {

private static final String TAG = "SerialPort";

public static final String DEFAULT_SU_PATH = "/system/bin/su";
//android6及其以上开发版(且android6以下的也能用):/system/bin/sh
public static final String DEFAULT_SU_PATH = "/system/bin/sh"; ///system/bin/su

private static String sSuPath = DEFAULT_SU_PATH;
private File device; //串口设备文件
Expand Down Expand Up @@ -235,9 +236,13 @@ public void handleMessage(Message msg) {
private void stopSendThread() {
mSendingHandler = null;
if (null != mSendingHandlerThread) {
mSendingHandlerThread.interrupt();
mSendingHandlerThread.quit();
mSendingHandlerThread = null;
try {
mSendingHandlerThread.interrupt();
mSendingHandlerThread.quit();
mSendingHandlerThread = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}

Expand Down Expand Up @@ -271,7 +276,7 @@ private void stopReceivedThread() {
* @param device 文件
* @return 权限修改是否成功
*/
private boolean chmod777(File device) {
public static boolean chmod777(File device) {
if (null == device || !device.exists()) {
return false;
}
Expand All @@ -296,7 +301,7 @@ private boolean openSafe(File device, int baudrate, int dataBits, int parity,
Log.e(TAG, "openSafe: 文件不存在或文件不能为空!");
return false;
}
Log.i(TAG, String.format("SerialPort: %s: %d,%d,%d,%d,%d,%d", device.getPath(), baudrate, dataBits, parity, stopBits, flags));
Log.i(TAG, String.format("SerialPort: %s: %d,%d,%d,%d,%d", device.getPath(), baudrate, dataBits, parity, stopBits, flags));
if (!device.canRead() || !device.canWrite()) {
boolean chmod777 = chmod777(device);
if (!chmod777) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package top.lyoun.serialport;

import android.app.Activity;
import android.util.Log;
import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -48,6 +49,8 @@ public Vector<File> getDevices() {

File[] files = dev.listFiles();

Log.i(TAG, "driver: " + files);

if (files != null) {
int i;
for (i = 0; i < files.length; i++) {
Expand All @@ -70,7 +73,10 @@ public String getName() {

private Vector<Driver> mDrivers = null;

Activity activity;

Vector<Driver> getDrivers() throws IOException {

if (mDrivers == null) {
mDrivers = new Vector<Driver>();
LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
Expand All @@ -90,7 +96,9 @@ Vector<Driver> getDrivers() throws IOException {
return mDrivers;
}

public String[] getAllDevices() {
public String[] getAllDevices(Activity activity) {
this.activity = activity;

Vector<String> devices = new Vector<String>();
// Parse each driver
Iterator<Driver> itdriv;
Expand All @@ -99,6 +107,7 @@ public String[] getAllDevices() {
while (itdriv.hasNext()) {
Driver driver = itdriv.next();
Iterator<File> itdev = driver.getDevices().iterator();

while (itdev.hasNext()) {
String device = itdev.next().getName();
String value = String.format("%s (%s)", device, driver.getName());
Expand Down Expand Up @@ -130,4 +139,5 @@ public String[] getAllDevicesPath() {
}
return devices.toArray(new String[devices.size()]);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package top.lyoun.serialport;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

Expand All @@ -14,6 +16,7 @@ public abstract class SerialReadThread extends Thread{

private InputStream mInputStream;
private byte[] mReceivedBuffer;
private boolean reading;

public SerialReadThread(InputStream inputStream) {
mInputStream = inputStream;
Expand All @@ -23,7 +26,8 @@ public SerialReadThread(InputStream inputStream) {
@Override
public void run() {
super.run();
while (!isInterrupted()) {
reading = true;
while (reading) {
try {
if (null == mInputStream) {
return;
Expand All @@ -34,7 +38,9 @@ public void run() {
}
byte[] receivedBytes = new byte[size];
System.arraycopy(mReceivedBuffer, 0, receivedBytes, 0, size);
onDataReceived(receivedBytes);
if (reading) {
onDataReceived(receivedBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -47,15 +53,20 @@ public void run() {
* 释放
*/
public void release() {
interrupt();
try {
reading = false;
interrupt();

if (null != mInputStream) {
try {
mInputStream.close();
} catch (IOException e) {
e.printStackTrace();
if (null != mInputStream) {
try {
mInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
mInputStream = null;
}
mInputStream = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="top.lyoun.androidserialhelper">

<!--serial need read and write privilege-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:requestLegacyExternalStorage="true"
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class Application extends android.app.Application {
public SerialPort getSerial1(){
if(mSerial1 == null){
mSerial1 = SerialPort.newBuilder(
"/dev/ttymxc1",
115200)
"/dev/ttyS1",
9600)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import top.lyoun.androidserialhelper.databinding.ActivityMainBinding;
import top.lyoun.serialport.Hello;
import top.lyoun.serialport.RequestStoragePrivileges;
import top.lyoun.serialport.SerialPort;
import top.lyoun.serialport.Welcome;
import top.lyoun.serialport.listener.OnSerialDataListener;
Expand Down Expand Up @@ -54,8 +55,10 @@ protected void onCreate(Bundle savedInstanceState) {
mSerial1 = mApplication.getSerial1();

//读取设备列表
Log.i(TAG, "allDevices: " + Arrays.toString(mApplication.mSerialPortFinder.getAllDevices()));
Log.i(TAG, "allDevices: " + Arrays.toString(mApplication.mSerialPortFinder.getAllDevices(this)));
Log.i(TAG, "\n=============================\n");
Log.i(TAG, "allDevicesPath: " + Arrays.toString(mApplication.mSerialPortFinder.getAllDevicesPath()));

}

@Override
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath 'com.android.tools.build:gradle:7.0.4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 7839222

Please sign in to comment.