在已root的安卓上实现AT指令并获取结果。

3

我是一个stackoverflow的新手,所以我不能添加评论。

我看到了这个页面:
在su进程内读取命令输出

我尝试了这个答案,它可以:

Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("ls /data\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
    read = stdout.read(buffer);
    out += new String(buffer, 0, read);
    if(read<BUFF_LEN){
        //we have read everything
        break;
       }
 }
 //do something with the output

但是当我在shell中尝试运行该命令时,响应仍然是相同的命令。
我输入了以下命令:
stdin.writeBytes("echo AT+CQI?\n");

答案是:
AT+CQI?

I wrote:

stdin.writeBytes("echo ATinkd\n");

答案是:
ATinkd

这意味着"bla..bla..bla.."。这意味着安卓系统无法将这些命令识别为AT命令。

我想知道是否有人有建议或解决方案。


请注意,V.250 要求 AT 命令行以 \r 而不是 \n 结尾。 - hlovdal
1个回答

3
首先,我认为你只是将AT命令发送到标准输出中,这不会产生任何效果,只是让你读回一个回显。要使该方法有效,您必须将echo命令重定向到串行端口设备文件。Android手机使用各种设备进行此操作,/dev/ttyGS0/dev/smd0似乎是常见的名称。
然而,我建议使用程序atinput来发送AT命令并捕获调制解调器响应。它专门编写成可以像那样从命令行使用。这将使您免于直接与调制解调器通信,剩下的唯一事情就是管道处理读取响应。

谢谢您的回答。我不知道如何将AT指令重定向到/dev/smd0。我下载了atinput并找到了c文件,但我需要在我的安卓手机上执行这些命令。 - Mohammad Alshaar
你需要为目标安卓手机编译程序。通常情况下,你只需运行“make”即可进行编译,但是要制作一个安卓二进制文件,你必须使用交叉编译器,因此请编辑Makefile并将gcc替换为交叉编译器。 - hlovdal
我不明白。你的意思是通过“make”来创建“makefile”吗?如果是,我无法做到,因为它不是可执行文件或其他什么东西。什么是交叉编译器? - Mohammad Alshaar
你需要花一些时间了解编译C程序的构建过程。可以从http://en.wikipedia.org/wiki/Make_(software)和http://en.wikipedia.org/wiki/Cross_compiler开始学习。 - hlovdal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接