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.
If I install Terminal emulator, I can write the command and get its response with cat \dev\smd0
.
Try 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();
}
Additionally you need root access to your phone.
found the problem with thanks of 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
need in end of command and for some commands what needs su
we need to use DataOutPutStream
to send command.
EDIT :
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");
}
}