Skip to content

Commit be8a99f

Browse files
committed
修复bug,添加可以修改su路径
1 parent 032b91d commit be8a99f

File tree

21 files changed

+411
-391
lines changed

21 files changed

+411
-391
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0'
9-
8+
classpath 'com.android.tools.build:gradle:2.3.3'
9+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
1212
}

gradle.properties

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
# Project-wide Gradle settings.
2-
3-
# IDE (e.g. Android Studio) users:
4-
# Gradle settings configured through the IDE *will override*
5-
# any settings specified in this file.
6-
1+
## Project-wide Gradle settings.
2+
#
73
# For more details on how to configure your build environment visit
84
# http://www.gradle.org/docs/current/userguide/build_environment.html
9-
5+
#
106
# Specifies the JVM arguments used for the daemon process.
117
# The setting is particularly useful for tweaking memory settings.
12-
org.gradle.jvmargs=-Xmx1536m
13-
8+
# Default value: -Xmx1024m -XX:MaxPermSize=256m
9+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10+
#
1411
# When configured, Gradle will run in incubating parallel mode.
1512
# This option should only be used with decoupled projects. More details, visit
1613
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1714
# org.gradle.parallel=true
15+
#Mon Sep 18 19:30:46 CST 2017
16+
systemProp.http.proxyHost=127.0.0.1
17+
org.gradle.jvmargs=-Xmx1536m
18+
systemProp.http.proxyPort=1080

jitpack.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.github.dcendents.android-maven'
2+
3+
group = 'com.github.licheedev'
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+
}

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 25
5-
buildToolsVersion "25.0.2"
5+
buildToolsVersion "25.0.3"
66

77
defaultConfig {
88
applicationId "android.serialport.sample"
@@ -26,7 +26,7 @@ dependencies {
2626
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2727
exclude group: 'com.android.support', module: 'support-annotations'
2828
})
29-
compile 'com.android.support:appcompat-v7:25.3.0'
29+
compile 'com.android.support:appcompat-v7:25.3.1'
3030
compile 'com.android.support.constraint:constraint-layout:1.0.2'
3131
testCompile 'junit:junit:4.12'
3232
compile project(':serialport')

serialport/build.gradle

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
apply plugin: 'com.android.library'
22

3+
34
android {
45
compileSdkVersion 25
5-
buildToolsVersion "25.0.2"
6+
buildToolsVersion "25.0.3"
67

78
defaultConfig {
89
minSdkVersion 15
@@ -26,10 +27,12 @@ android {
2627
}
2728

2829
dependencies {
29-
compile fileTree(dir: 'libs', include: ['*.jar'])
30-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
31-
exclude group: 'com.android.support', module: 'support-annotations'
32-
})
33-
compile 'com.android.support:appcompat-v7:25.3.0'
34-
testCompile 'junit:junit:4.12'
30+
// compile fileTree(dir: 'libs', include: ['*.jar'])
31+
// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
32+
// exclude group: 'com.android.support', module: 'support-annotations'
33+
// })
34+
// compile 'com.android.support:appcompat-v7:25.3.0'
35+
// testCompile 'junit:junit:4.12'
3536
}
37+
38+
apply from: '../jitpack.gradle'

serialport/src/androidTest/java/android/serialport/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

serialport/src/main/java/android/serialport/SerialPort.java

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.serialport;
1818

19+
import android.util.Log;
1920
import java.io.File;
2021
import java.io.FileDescriptor;
2122
import java.io.FileInputStream;
@@ -24,62 +25,89 @@
2425
import java.io.InputStream;
2526
import java.io.OutputStream;
2627

27-
import android.util.Log;
28-
2928
public class SerialPort {
3029

31-
private static final String TAG = "SerialPort";
30+
private static final String TAG = "SerialPort";
31+
32+
private static final String DEFAULT_SU_PATH = "/system/bin/su";
3233

33-
/*
34-
* Do not remove or rename the field mFd: it is used by native method close();
35-
*/
36-
private FileDescriptor mFd;
37-
private FileInputStream mFileInputStream;
38-
private FileOutputStream mFileOutputStream;
34+
private static String sSuPath = DEFAULT_SU_PATH;
3935

40-
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
36+
/**
37+
* Set the su binary path, the default su binary path is {@link #DEFAULT_SU_PATH}
38+
*
39+
* @param suPath su binary path
40+
*/
41+
public static void setSuPath(String suPath) {
42+
if (suPath == null) {
43+
return;
44+
}
45+
sSuPath = suPath;
46+
}
47+
48+
/*
49+
* Do not remove or rename the field mFd: it is used by native method close();
50+
*/
51+
private FileDescriptor mFd;
52+
private FileInputStream mFileInputStream;
53+
private FileOutputStream mFileOutputStream;
54+
55+
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
4156

4257
/* Check access permission */
43-
if (!device.canRead() || !device.canWrite()) {
44-
try {
45-
/* Missing read/write permission, trying to chmod the file */
46-
Process su;
47-
su = Runtime.getRuntime().exec("/system/bin/su");
48-
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
49-
+ "exit\n";
50-
su.getOutputStream().write(cmd.getBytes());
51-
if ((su.waitFor() != 0) || !device.canRead()
52-
|| !device.canWrite()) {
53-
throw new SecurityException();
54-
}
55-
} catch (Exception e) {
56-
e.printStackTrace();
57-
throw new SecurityException();
58-
}
59-
}
60-
61-
mFd = open(device.getAbsolutePath(), baudrate, flags);
62-
if (mFd == null) {
63-
Log.e(TAG, "native open returns null");
64-
throw new IOException();
65-
}
66-
mFileInputStream = new FileInputStream(mFd);
67-
mFileOutputStream = new FileOutputStream(mFd);
68-
}
69-
70-
// Getters and setters
71-
public InputStream getInputStream() {
72-
return mFileInputStream;
73-
}
74-
75-
public OutputStream getOutputStream() {
76-
return mFileOutputStream;
77-
}
78-
79-
// JNI
80-
private native static FileDescriptor open(String path, int baudrate, int flags);
81-
public native void close();
82-
static {
83-
System.loadLibrary("serial_port");
84-
}
58+
if (!device.canRead() || !device.canWrite()) {
59+
try {
60+
/* Missing read/write permission, trying to chmod the file */
61+
Process su;
62+
su = Runtime.getRuntime().exec(sSuPath);
63+
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n";
64+
su.getOutputStream().write(cmd.getBytes());
65+
if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
66+
throw new SecurityException();
67+
}
68+
} catch (Exception e) {
69+
e.printStackTrace();
70+
throw new SecurityException();
71+
}
72+
}
73+
74+
mFd = open(device.getAbsolutePath(), baudrate, flags);
75+
if (mFd == null) {
76+
Log.e(TAG, "native open returns null");
77+
throw new IOException();
78+
}
79+
mFileInputStream = new FileInputStream(mFd);
80+
mFileOutputStream = new FileOutputStream(mFd);
81+
}
82+
83+
public SerialPort(String devicePath, int baudrate, int flags)
84+
throws SecurityException, IOException {
85+
this(new File(devicePath), baudrate, flags);
86+
}
87+
88+
public SerialPort(File device, int baudrate) throws SecurityException, IOException {
89+
this(device, baudrate, 0);
90+
}
91+
92+
public SerialPort(String devicePath, int baudrate) throws SecurityException, IOException {
93+
this(new File(devicePath), baudrate, 0);
94+
}
95+
96+
// Getters and setters
97+
public InputStream getInputStream() {
98+
return mFileInputStream;
99+
}
100+
101+
public OutputStream getOutputStream() {
102+
return mFileOutputStream;
103+
}
104+
105+
// JNI
106+
private native static FileDescriptor open(String path, int baudrate, int flags);
107+
108+
public native void close();
109+
110+
static {
111+
System.loadLibrary("serial_port");
112+
}
85113
}

0 commit comments

Comments
 (0)