如何向android发送命令,然后获得其答案?

如何向android发送命令,然后获得其答案?,android,shell,terminal,runtime.exec,Android,Shell,Terminal,Runtime.exec,我想在shell中编写echo-e“AT\r”>/dev/smd0,然后得到它的响应。 响应将出现在\dev\smd0中 我在谷歌上搜索发现: Runtime r = Runtime.getRuntime(); process = r.exec("su"); process = r.exec("echo -e \"AT\\r\" > /dev/smd0"); 但它不起作用 我也不知道该如何解读回复 如果我安装了终端仿真器,我可以编写命令并

我想在shell中编写
echo-e“AT\r”>/dev/smd0
,然后得到它的响应。 响应将出现在
\dev\smd0

我在谷歌上搜索发现:

 Runtime r = Runtime.getRuntime();
            process = r.exec("su");
            process = r.exec("echo -e \"AT\\r\" > /dev/smd0");
但它不起作用

我也不知道该如何解读回复

如果我安装了终端仿真器,我可以编写命令并使用
cat\dev\smd0

获取其响应,请尝试:


此外,您还需要根用户访问您的手机。

发现问题,感谢:

\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");
    }
}
公共类读取实现可运行{
专用线程链接器;
私有ArrayList输出=新建ArrayList();
公共字符串getResponse(){
if(output.size()!=0){
字符串ans=output.get(0);
输出。删除(0);
返回ans;
}
返回null;
}
公开作废开始(){
mBlinker=新线程(此线程);
mBlinker.start();
}
公共停车场(){
mBlinker=null;
}
私有数据输入流dis;
私有数据输出流dos;
@凌驾
公开募捐{
System.out.println(“开始读取”);
试一试{
Runtime r=Runtime.getRuntime();
过程=r.exec(“su”);
dos=新的DataOutputStream(process.getOutputStream());
dos.writeBytes(“cat/dev/smd0\n”);
dos.flush();
dis=新的DataInputStream(process.getInputStream());
}捕获(IOE异常){
e、 printStackTrace();
}
while(mBlinker!=null){
试一试{
int av=dis.available();
如果(av!=0){
字节[]b=新字节[av];
dis.read(b);
add(新字符串(b));
System.out.println(新字符串(b)+“接收到的调制解调器”);
}
其他的
{
睡眠(100);
}
}捕获(IOE异常){
如果(如getMessage()!=null)
System.out.println(e.getMessage());
e、 printStackTrace();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
试一试{
dos.writeBytes(“退出”);
dos.flush();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
系统输出打印项次(“停止读取”);
}
}

谢谢,但我给出了这个错误:02-16 08:58:30.869:WARN/su(12559):请求被拒绝(0->0'echo)尝试将
cat
中的“\”替换为“/”,我只是从您的问题中复制了它。我在\\r\”>/dev/smd0;cat/dev/smd0”测试“su-c'echo-e\”,但在您上面提供的相同链接上存在问题,只需查找以下行:String currUid=osRes.readLine();
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();
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");
    }
}