如何向android发送命令然后得到它的答案?

时间:2012-02-16 06:53:10

标签: android shell terminal runtime.exec

我想在shell中编写echo -e "AT\r" > /dev/smd0然后得到它的响应。 回复将在\dev\smd0

我搜索了Google,发现了这个:

 Runtime r = Runtime.getRuntime();
            process = r.exec("su");
            process = r.exec("echo -e \"AT\\r\" > /dev/smd0");

但它不起作用。

我不知道如何阅读回复。

如果我安装终端模拟器,我可以编写命令并使用cat \dev\smd0获取响应。

2 个答案:

答案 0 :(得分:2)

尝试like this

try {  
    Runtime r = Runtime.getRuntime();
    Process process = r.exec(" su -c 'echo -e \"AT\\r\" > /dev/smd0; cat /dev/smd0' ");

    BufferedReader in = new BufferedReader(  
                        new InputStreamReader(process.getInputStream()));  
    String line = null;  
    while ((line = in.readLine()) != null) {  
        System.out.println(line);  
    }  
} catch (IOException e) {  
    e.printStackTrace();  
}  

此外,您需要root权限才能访问手机。

答案 1 :(得分:1)

感谢this Link发现了问题:

Runtime r = Runtime.getRuntime();


            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(
                    process.getOutputStream());
            os.writeBytes("echo -e \"AT\\r\" > /dev/smd0\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();

\n需要在命令结束时和某些命令中需要su我们需要使用DataOutPutStream来发送命令。

编辑:

以下代码我可以阅读:

public class read implements Runnable {

    private Thread mBlinker;

    private ArrayList<String> output = new ArrayList<String>();

    public String getResponce() {
        if (output.size() != 0) {
            String ans = output.get(0);
            output.remove(0);
            return ans;
        }
        return null;
    }

    public void start() {
        mBlinker = new Thread(this);
        mBlinker.start();
    }

    public void stop() {
        mBlinker = null;
    }

    private DataInputStream dis;
    private DataOutputStream dos;

    @Override
    public void run() {
        System.out.println("START READ");
        try {
            Runtime r = Runtime.getRuntime();
            Process process = r.exec("su");
            dos = new DataOutputStream(process.getOutputStream());
            dos.writeBytes("cat /dev/smd0\n");
            dos.flush();

            dis = new DataInputStream(process.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (mBlinker != null) {
            try {
                int av = dis.available();
                if (av != 0) {
                    byte[] b = new byte[av];
                    dis.read(b);
                    output.add(new String(b));
                    System.out.println(new String(b) + "Recieved form modem");
                }
                else
                {
                    Thread.sleep(100);
                }
            } catch (IOException e) {
                if (e.getMessage() != null)
                    System.out.println(e.getMessage());
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            dos.writeBytes("exit\n");
            dos.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("STOP READ");
    }
}