[转] Android 上层应用读写底层设备节点(Android M)
应用层中,一般都能够读取设备节点。对于写节点这个操作,需要更高的root权限。
示例: 设备节点:
private static final String LED_RED_TRIGGER = "sys/class/leds/red/on_off";
写节点:
private void setLedOnOff(String path, String value) {
try {
BufferedWriter bufWriter = null;
bufWriter = new BufferedWriter(new FileWriter(path));
bufWriter.write(value + "");
bufWriter.close();
Log.d(TAG, "write setLedOnOff: " + value);
} catch (IOException e) {
Log.e(TAG,"erro= "+ Log.getStackTraceString(e));
}
}
读节点:
private boolean isLedOn(String path){
try {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String readString = null;
String valueString = null;
while ((readString = br.readLine())!=null){
if(readString == null)break;
valueString = readString;
}
br.close();
return valueString != null && valueString.equals("1");//此处1表示灯亮,0表示灯灭
} catch (IOException e) {
Log.e(TAG,"erro= "+ Log.getStackTraceString(e));
}
return false;
}