android 上层读写 sys/bus/驱动节点 有有没有达人知道方法

网上查了好多都不成功啊

运行到到 os.writeBytes(command + "\n");时老是出错啊 java.io.IOException: write failed: EPIPE (Broken pipe)

下面是程序
Process process = null;
DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("su",null,null);

            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
              e.printStackTrace();  
            return null;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                 //process.destroy();
            } catch (Exception e) {
                  e.printStackTrace();  
                return null;
            }
        }

2个回答

写sys节点也可以的,apk要在packages/apps/目录下,用mm编译,同时要在AndroidManifest.xml配置权限

     String exe_path = "/sys/class/rfkill/rfkill0/state";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mRefresh = (Button)findViewById(R.id.refresh);
        mRefresh.setOnClickListener(mClickListener);

        mMountText = (TextView)findViewById(R.id.mountText);
        mMount = (Button)findViewById(R.id.mountBtn1);
        mMount.setOnClickListener(mClickListener);

        try
        {
            BufferedWriter bufWriter = new BufferedWriter(new FileWriter(exe_path));
            bufWriter.write("1");  // 这儿进行的写操作
            bufWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            /*
            exe_file = new File(exe_path);  
            exe_file.setExecutable(true, true); 
            */
            Runtime runtime = Runtime.getRuntime();            
            Process process = runtime.exec("cat /sys/class/rfkill/rfkill0/state"); // 这儿进行的度操作
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while (null != (line = br.readLine())) {
                Log.e("########", line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

读节点的方法:
filename = "sys/data/filename";
BufferedReader reader = new BufferedReader(new FileReader(filename));
reader.readLine();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐