java调用Linux命令控制GPIO

static Process process = null;

static DataOutputStream dos = null;

public static int sss(int gpio, String direction, int level) {
    cdGPIO = "cd" + " /sys/class/gpio";

    exportPath = "echo " + gpio + " > export";

    directionPath = "echo " + direction + " > direction";

    valuePath = "echo " + level + " > value";

    String cd = "cd gpio" + gpio;

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

        dos = new DataOutputStream(process.getOutputStream());

        dos.writeBytes(cdGPIO + "\n");

        dos.flush();

        dos.writeBytes(exportPath + "\n");

        dos.flush();

        dos.writeBytes(cd + "\n");

        dos.flush();

        dos.writeBytes(directionPath + "\n");

        dos.flush();
 
  
      dos.writeBytes("cd " + "/sys/class/gpio/gpio" + gpio + "\n");
    dos.flush();

    dos.writeBytes(valuePath + "\n");

    dos.flush();
     dos.close(); } catch (IOException e) { e.printStackTrace(); } finally { if ( process != null) { process.destroy(); process = null; } } return 1;}

你可能感兴趣的:(个人笔记)