转自: https://blog.csdn.net/u010218230/article/details/80007819?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-1.fixedcolumn&spm=1001.2101.3001.4242.2

应用层中,一般都能够读取设备节点。对于写节点这个操作,需要更高的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;
    }

标签: aosp

添加新评论