Android GPIO控制方法

方法1: Android Things 外设I/O-GPIO: 可参考文章: https://www.jianshu.com/p/d33c4b832bf0

方法2:

GPIO0对应系统节点如: /sys/class/backlight/rk28_bl/gpio0

//读GPIO

 private String getGpioString(String path) {
                String defString = "0";// 默认值
                try {
                        @SuppressWarnings("resource")
                        BufferedReader reader = new BufferedReader(new FileReader(path));
                        defString = reader.readLine();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return defString;
        }

getGpioString("/sys/class/backlight/rk28_bl/gpio0")   返回字符串“0” 或者“1”

//将GPIO口设置为输出的时候,默认是输出,调用下面的方法即可变成默认输入

public boolean gpio_input0() {
	return  RootCommand("echo  2 > /sys/class/backlight/rk28_bl/gpio0");
}

//当GPIO口为输出的时候,通过以下的办法来控制高低电平

private boolean set_gpio0_high() {   //拉高
		boolean FLAG = RootCommand("echo  1 > /sys/class/backlight/rk28_bl/gpio0");
		return FLAG;
	}
public boolean set_gpio0_low() {    //拉低
		boolean FLAG =  RootCommand("echo 0 >/sys/class/backlight/rk28_bl/gpio0");
		return FLAG;
	}

//下面的是执行的方法

private boolean RootCommand(String command) {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(command + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
			}
		}
		return true;
	}

你可能感兴趣的:(Android)