Android节点读写实现
在工作中,我们会对一些节点进行读写操作。比如控制闪光灯,指示灯;降噪芯片开关或其低功耗开关,因为项目中常用到,在此记录,备忘~
直接上代码,想用直接拿去
一 监听某个节点的写入值的反馈
有些节点写入是可以监听到写入结果的,比如降噪芯片低功耗节点写入后可以监听其写入结果,代码如下
/**
* 读取操作低功耗的开启/关闭结果
*/
private void readOpenLowPowerCOnsumptionResult() {
Log.e(TAG, "readOpenLowPowerCOnsumptionResult init");
new Thread(() -> {
try {
File deviceFile = new File("/dev/ttyS0");
if (!deviceFile.exists()) {
Log.e(TAG,"Device file not found: " + "/dev/ttyS0");
return;
}
FileInputStream fis = new FileInputStream(deviceFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
Log.d(TAG, "Reading from " + "/dev/ttyS0" + "...");
String line;
while ((line = reader.readLine()) != null) {
Log.d(TAG,"Log: " + line);
if ("OK: low_pw 1".equals(line)) {
Log.e(TAG, "auxstatus open low_pw success");
// isWaitingRead =false;
}else if ("OK: low_pw 0".equals(line)) {
Log.e(TAG, "auxstatus close low_pw success");
// isWaitingRead =false;
}else {
Log.e(TAG, "readOpenAudResult result :" + line);
}
}
Log.d(TAG, "Reading from " + "/dev/ttyS0" + "---");
reader.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Error reading from /dev/ttyS0");
}
}).start();
}
二 写入节点
2.1
/**
* 开启降噪低功耗模式
* @param isOpen true:开启低功耗;false 关闭低功耗
* @return [description]
*/
private synchronized boolean openAudLowPower(boolean isOpen) {
Log.d(TAG, "openAudLowPower init----1 isOpen:" + isOpen);
java.lang.Process process = null;
DataOutputStream os = null;
try {
String[] command = {"sh"};
process = Runtime.getRuntime().exec(command);
os = new DataOutputStream(process.getOutputStream());
os.writeBytes( "stty -F /dev/ttyS0 -echo\n");
os.writeBytes( "stty -F /dev/ttyS0 ispeed 115200 ospeed 115200 cs8\n");
if (isOpen) {
os.writeBytes("printf 'low_pw 1' > /dev/ttyS0\n");
}else {
os.writeBytes("printf 'low_pw 0' > /dev/ttyS0\n");
}
os.writeBytes("exit\n");
os.flush();
int result = process.waitFor();
Log.d(TAG, "openAudch result : " + result);
} catch (Exception e) {
Log.d(TAG, "openAudLowPower exception msg: " + e.getMessage());
Log.e(TAG, "openAudLowPower , send error");
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
Log.e(TAG, "send error");
return false;
}
Log.e(TAG, "openAudLowPower finally end");
}
return true;
}
2.2还有使用echo 实现写入节点,代码如下
public boolean openEncLvl() {
if (DEBUG_INPUT) {
Slog.d(TAG, "openEncLvl init---- ");
}
java.lang.Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("sh");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes( "stty -F /dev/ttyS0 -echo\n");
os.writeBytes( "stty -F /dev/ttyS0 ispeed 115200 cs8\n");
os.writeBytes( "echo enc_lvl 1 > /dev/ttyS0\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
if (DEBUG_INPUT) {
Slog.d(TAG, "openEncLvl exception msg: " + e.getMessage());
}
Log.e(TAG, "openEncLvl , 发送失败。");
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
Log.e(TAG, "发送失败");
return false;
}
if (DEBUG_INPUT) {
Slog.d(TAG, "openEncLvl finally end");
}
}
return true;
}
2.1和2.2 实现的区别就是写入值的时候
2.1如下实现:
os.writeBytes("printf 'low_pw 1' > /dev/ttyS0\n");
2.2如下实现:
os.writeBytes( "echo enc_lvl 1 > /dev/ttyS0\n");
----------------------------------------
若使用直接拿去,可运行!
大家新年快乐~