Android应用层如何读写设备节点
1、什么是设备节点(设备文件)?
Linux中设备节点是通过“mknod”命令来创建的。一个设备节点其实就是一个文件,Linux中称为设备文件。有一点必要说明的是,在Linux中,所有的设备访问都是通过文件的方式,一般的数据文件程序普通文件,设备节点称为设备文件。所以读写设备节点即读取更改文件数据。
2、读取设备节点
例如节点路径为:/sys/wenjian/node
/**
* 读取设备节点
*/
public static String getNodeString(String path) {
String prop = "waiting";// 默认值
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
prop = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
调用方法:
private static final String path = "/sys/auxcheck/auxcheck";
getNodeString(path );
3、改写节点
例如节点路径为:/sys/wenjian/node
/**
* 改写节点
*/
public static boolean setNodeString(String path,String value){
try {
BufferedWriter bufWriter = null;
bufWriter = new BufferedWriter(new FileWriter(path));
bufWriter.write(voltage); // 写入数据
bufWriter.close();
Log.e("fht","改写节点成功!");
} catch (IOException e) {
e.printStackTrace();
Log.e("fht","改写节点失败!");
return false;
}
return true;
}
调用方法:
private static final String path = "/sys/auxcheck/auxcheck";
private string values = "需要写入的数据";
boolean flag = NaviDebug.setNodeString(path,values);
注意:因为写节点需要高权限,所以可能需要在AndroidMainfest.xml中添加android:sharedUserId=“android.uid.system” 属性,如下: