如何将命令发送到Android,然后得到它的答案吗?它的、发送到、如何将、命令

2023-09-04 10:35:48 作者:笨不拉几萌萌哒╯3╰

我想写AT \ R呼应-e>为/ dev / SMD0 的壳,然后获取它的响应。 响应将在 \ dev的\ SMD0

I want to write echo -e "AT\r" > /dev/smd0 in the shell and then get its response. The response will be in \dev\smd0.

我搜索谷歌和发现这一点:

I searched Google and found this :

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

,但它不工作

but it does not work.

和我不知道如何读的响应。

And I do not know how to read the response.

如果我安装终端仿真器,我可以写命令,并得到了与响应猫\ dev的\ SMD0

If I install Terminal emulator, I can write the command and get its response with cat \dev\smd0.

推荐答案

中发现的问题与的这个链接:

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();

需要在命令的结束和一些命令有什么需要我们需要使用 DataOutputStream类发送命令。

\n need in end of command and for some commands what needs su we need to use DataOutPutStream to send command.

编辑:

低于code,我可以读它:

with below code i can read it :

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");
    }
}