android RK3328 gpio处理

文章描述了一个Android应用程序中的方法,用于执行系统命令并管理GPIO(GeneralPurposeInput/Output)操作,包括读写权限设置和文件I/O。方法`execCommand`负责执行多条命令,展示了如何使用Java处理命令流和异常处理。
摘要由CSDN通过智能技术生成

public int result = -1;

public String errorMsg;

public String successMsg;

}

/**

  • 执行命令—单条

  • @param command

  • @param isRoot

  • @return

*/

public static CommandResult execCommand(String command, boolean isRoot) {

String[] commands = {command};

return execCommand(commands, isRoot);

}

/**

  • 执行命令-多条

  • @param commands

  • @param isRoot

  • @return

*/

public static CommandResult execCommand(String[] commands, boolean isRoot) {

CommandResult commandResult = new CommandResult();

if (commands == null || commands.length == 0) return commandResult;

Process process = null;

DataOutputStream os = null;

BufferedReader successResult = null;

BufferedReader errorResult = null;

StringBuilder successMsg = null;

StringBuilder errorMsg = null;

try {

process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);

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

for (String command : commands) {

if (command != null) {

os.write(command.getBytes());

os.writeBytes(COMMAND_LINE_END);

os.flush();

}

}

os.writeBytes(COMMAND_EXIT);

os.flush();

commandResult.result = process.waitFor();

//获取错误信息

successMsg = new StringBuilder();

errorMsg = new StringBuilder();

successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));

errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));

String s;

while ((s = successResult.readLine()) != null) successMsg.append(s);

while ((s = errorResult.readLine()) != null) errorMsg.append(s);

commandResult.successMsg = successMsg.toString();

commandResult.errorMsg = errorMsg.toString();

Log.i(TAG, commandResult.result + " | " + commandResult.successMsg

  • " | " + commandResult.errorMsg);

} catch (IOException e) {

String errmsg = e.getMessage();

if (errmsg != null) {

Log.e(TAG, errmsg);

} else {

e.printStackTrace();

}

} catch (Exception e) {

String errmsg = e.getMessage();

if (errmsg != null) {

Log.e(TAG, errmsg);

} else {

e.printStackTrace();

}

} finally {

try {

if (os != null) os.close();

if (successResult != null) successResult.close();

if (errorResult != null) errorResult.close();

} catch (IOException e) {

String errmsg = e.getMessage();

if (errmsg != null) {

Log.e(TAG, errmsg);

} else {

e.printStackTrace();

}

}

if (process != null) process.destroy();

}

return commandResult;

}

}

  • gpio读写操作

package cn.hxg.androidgpiotest;

import android.os.Bundle;

import android.os.SystemClock;

import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private String[] gpioShell = {“cd /sys/class/gpio”, “echo 3 > export”,

“cd gpio3”, “echo in >direction”, “chmod 777 value”};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

new Thread(new Runnable() {

@Override

public void run() {

if (FileUtils.fileIsExists(“/sys/class/gpio/gpio3”)) {

CommandExecution.CommandResult commandResult = CommandExecution.execCommand(gpioShell, true);

Log.e(“huang”, commandResult.result + " | " + commandResult.successMsg

  • " | " + commandResult.errorMsg);

while (true) {

SystemClock.sleep(1000);

String s = FileUtils.ReadTxtFile(“/sys/class/gpio/gpio3/value”);

Log.i(“huang”, “s===>” + s);

}

}

}

}).start();

}

}

  • 文件操作类

package cn.hxg.androidgpiotest;

import java.io.BufferedReader;

import java.io.File;

尾声

开发是需要一定的基础的,我是08年开始进入Android这行的,在这期间经历了Android的鼎盛时期,和所谓的Android”凉了“。中间当然也有着,不可说的心酸,看着身边朋友,同事一个个转前端,换行业,其实当时我的心也有过犹豫,但是我还是坚持下来了,这次的疫情就是一个好的机会,大浪淘沙,优胜劣汰。再等等,说不定下一个黄金浪潮就被你等到了。

  • 330页 PDF Android核心笔记

  • 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题

  • PDF和思维脑图,包含知识脉络 + 诸多细节

  • Android进阶系统学习视频

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • Android进阶系统学习视频

[外链图片转存中…(img-zVO03iPs-1714288833403)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 27
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值