android 开发串口中遇到的权限问题

public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {

        //检查访问权限,如果没有读写权限,进行文件操作,修改文件访问权限
        if (!device.canRead() || !device.canWrite()) {
            try {
                //通过挂载到linux的方式,修改文件的操作权限
                Process su = Runtime.getRuntime().exec("/system/bin/su");
                String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";
                su.getOutputStream().write(cmd.getBytes());
                if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
                //device.canRead() device.canWrite() 返回都flase  估计就是串口权限问题了 我是找开发商 开权限的
                    throw new SecurityException();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new SecurityException();
            }
        }

        mFd = open(device.getAbsolutePath(), baudrate, flags);

        if (mFd == null) {
            Log.e(TAG, "native open returns null");
            throw new IOException();
        }

        mFileInputStream = new FileInputStream(mFd);
        mFileOutputStream = new FileOutputStream(mFd);
    }

执行 /system/xbin/su这个进程,是获取root权限。这个返回的process,是一个root过的shell,可以在process的输入流中,以root权限执行其他命令了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,465评论 0 5
  • 玩儿过Linux的应该都明白Root代表了什么,获取Root权限你就能控制系统的一切,甚至还可以执行rm -rf ...
    freeman521阅读 5,299评论 1 10
  • 原地址:http://blog.csdn.net/hsluoyc/article/details/50560782...
    柒黍阅读 2,440评论 2 12
  • 1、第八章 Samba服务器2、第八章 NFS服务器3、第十章 Linux下DNS服务器配站点,域名解析概念命令:...
    哈熝少主阅读 3,775评论 0 10
  • 第一章 1.Linux是一套免费使用和自由传播的类UNIX操作系统,它可以基于Intel x86系列处理器以及Cy...
    yansicing阅读 5,484评论 0 9