android gpio操作
public class GPIOUtil {
? ? //讀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;
? ? }
? ? //將GPIO口設置為輸出的時候,默認是輸出,調用下面的方法即可變成默認輸入
? ? public boolean gpio_input0(View v) {
? ? ? ? return ?RootCommand("echo ?2 > /sys/class/backlight/rk28_bl/gpio0");
? ? }
? ? public boolean gpio_input1(View v) {
? ? ? ? return ?RootCommand("echo ?2 > /sys/class/backlight/rk28_bl/gpio1");
? ? }
? ? public boolean gpio_input2(View v) {
? ? ? ? return RootCommand("echo ?2 > /sys/class/backlight/rk28_bl/gpio2");
? ? }
? ? //當GPIO口為輸出的時候,通過以下的辦法來控制高低電平
? ? public boolean set_gpio0_high(View v) { ? //拉高
? ? ? ? boolean FLAG = RootCommand("echo ?1 > /sys/class/backlight/rk28_bl/gpio1");
? ? ? ? Log.e("123high", String.valueOf(FLAG));
? ? ? ? return FLAG;
? ? }
? ? public boolean set_gpio0_low(View v) { ? ?//拉低
? ? ? ? boolean FLAG = ?RootCommand("echo 0 > /sys/class/backlight/rk28_bl/gpio1");
? ? ? ? Log.e("123low", String.valueOf(FLAG));
// ? ? ? ?read_gpio0();
? ? ? ? return FLAG;
? ? }
? ? // ?其他的GPIO口都是一樣的方法(gpio0、gpio1、gpio2、gpio3)
? ? //下面的是執行的方法
? ? 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;
? ? }
---------------------?
作者:馬偵銘?
來源:CSDN?
原文:https://blog.csdn.net/zhenming_ma/article/details/80264002?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
總結
以上是生活随笔為你收集整理的android gpio操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高通平台gpio简单调试
- 下一篇: NDK JNI方式读写Android系