3

I'm a beginner in stackoverflow so I cant add a comment.

I saw this page:
Read command output inside su process

and I tried this answer and it is ok:

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

but when I tried at command in the shell the response was the same command.

I put this command:

stdin.writeBytes("echo AT+CQI?\n");

the answer was:

AT+CQI?

I wrote:

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

the answer was:

ATinkd

That is mean "bla..bla..bla..". that is mean the android system does not recognize this commands as at commands.

I wonder if any body have an advice or solution.

Community
  • 1
  • 1
Mohammad Alshaar
  • 523
  • 4
  • 21
  • Notice that [V.250](http://www.itu.int/rec/T-REC-V.250-200307-I/en) requires AT command lines to be terminated with `\r` and not `\n`. – hlovdal May 01 '14 at 22:18

1 Answers1

3

First I think you are just sending the AT command to stdout in the shell which will not do anything other than giving you an echo which you read back. For this approach to work you have to redirect the echo command to the serial port device file. Android phones use various devices for this, /dev/ttyGS0 and /dev/smd0 seems to be common names.

However I would suggest using the program atinput to send AT commands and capture modem responses. It is specifically written to be used from the command line like that. That will relieve you from communicating directly with the modem and the only thing left is the pipe handling reading the response.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • thank u for your answer. I don't know how to redirect at-commands to /dev/smd0. I download atinput and I found c file, but I need to execute the commands on my android mobile. – Mohammad Alshaar May 03 '14 at 14:41
  • You need to compile the program for the target android phone. To compile normally you just run `make`, but to make an android binary you have to use a cross-compiler, so edit the Makefile and replace gcc with the cross compiler. – hlovdal May 03 '14 at 18:00
  • I didn't get it. Do u mean by make is "makefile". If yes I couldn't it because it's not exe file or something. what is cross-compiler? – Mohammad Alshaar May 03 '14 at 18:09
  • You need to invest a bit in understanding the build process for compiling C programs. Start with http://en.wikipedia.org/wiki/Make_(software) and http://en.wikipedia.org/wiki/Cross_compiler. – hlovdal May 03 '14 at 18:24