Skip to content

Commit c94e058

Browse files
committed
优化su地址读取
1 parent bf4ebf5 commit c94e058

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

app/src/main/java/com/ichtj/basetools/application/InitializeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private void performInit() {
4545
// CrashHandler.getInstance().init(getApplication());
4646
//需要在 Application 的 onCreate() 中调用一次 BaseIotTools.instance()....
4747
BaseIotUtils.instance().create(mContext);
48-
ScreenAdaptUtils.init(mContext, ScreenAdaptUtils.AdaptBase.HEIGHT, 1920f);
48+
ScreenAdaptUtils.init(mContext, ScreenAdaptUtils.AdaptBase.WIDTH, 1080f);
4949
FBaseTools.instance()
5050
.create(getApplication());
5151
}

app/src/main/java/com/ichtj/basetools/util/CustomButtonGridView.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.widget.ScrollView;
1212

1313
import androidx.core.content.ContextCompat;
14+
import androidx.core.view.ViewCompat;
1415

1516
import com.ichtj.basetools.R;
1617

@@ -117,6 +118,7 @@ public void onClick(View v) {
117118
}
118119
}
119120
});
121+
ViewCompat.setBackgroundTintList(button, null);
120122
return button;
121123
}
122124

base_iotutils/src/main/java/com/face_chtj/base_iotutils/serialport/SerialPort.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
26+
import java.io.OutputStreamWriter;
2627

2728
public class SerialPort {
2829
private static final String TAG = "SerialPort";
@@ -34,30 +35,51 @@ public class SerialPort {
3435
private FileOutputStream mFileOutputStream;
3536

3637
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
37-
/** Check access permission */
38+
// 检查权限
3839
if (!device.canRead() || !device.canWrite()) {
39-
try {
40-
/** Missing read/write permission, trying to chmod the file */
41-
Process su= Runtime.getRuntime().exec("/system/bin/su");
42-
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n"
43-
+ "exit\n";
44-
su.getOutputStream().write(cmd.getBytes());
45-
if ((su.waitFor() != 0) || !device.canRead()
46-
|| !device.canWrite()) {
47-
throw new SecurityException();
40+
// 常见 su 路径
41+
String[] suPaths = {
42+
"/system/bin/su",
43+
"/system/xbin/su",
44+
"/sbin/su",
45+
"/vendor/bin/su"
46+
};
47+
48+
boolean chmodSuccess = false;
49+
for (String suPath : suPaths) {
50+
File suFile = new File(suPath);
51+
if (suFile.exists() && suFile.canExecute()) {
52+
try {
53+
Process su = Runtime.getRuntime().exec(suPath);
54+
OutputStream os = su.getOutputStream();
55+
OutputStreamWriter writer = new OutputStreamWriter(os);
56+
String cmd = "chmod 777 " + device.getAbsolutePath() + "\nexit\n";
57+
writer.write(cmd);
58+
writer.flush();
59+
writer.close();
60+
os.close();
61+
62+
if (su.waitFor() == 0 && device.canRead() && device.canWrite()) {
63+
chmodSuccess = true;
64+
break;
65+
}
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
}
4869
}
49-
} catch (Exception e) {
50-
e.printStackTrace();
51-
throw new SecurityException("execute command /system/bin/su chmod 777 xx Abnormal,Please check whether the serial port grants permission");
70+
}
71+
72+
if (!chmodSuccess) {
73+
throw new SecurityException("无法修改串口权限,请确保设备已 root 且 su 权限可用");
5274
}
5375
}
5476

77+
// 打开串口
5578
mFd = open(device.getAbsolutePath(), baudrate, flags);
56-
KLog.d(TAG, "SerialPort:open success ");
5779
if (mFd == null) {
58-
KLog.e(TAG, "native open returns null");
59-
throw new IOException();
80+
throw new IOException("native open returns null");
6081
}
82+
6183
mFileInputStream = new FileInputStream(mFd);
6284
mFileOutputStream = new FileOutputStream(mFd);
6385
}

0 commit comments

Comments
 (0)