How To Send A Command To Android And Then Get Its Answer?
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.get
Solution 1:
Try like this:
try {
Runtimer= Runtime.getRuntime();
Processprocess= r.exec(" su -c 'echo -e \"AT\\r\" > /dev/smd0; cat /dev/smd0' ");
BufferedReaderin=newBufferedReader(
newInputStreamReader(process.getInputStream()));
Stringline=null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Additionally you need root access to your phone.
Solution 2:
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 :
publicclassreadimplementsRunnable {
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;
}
returnnull;
}
publicvoidstart() {
mBlinker = new Thread(this);
mBlinker.start();
}
publicvoidstop() {
mBlinker = null;
}
private DataInputStream dis;
private DataOutputStream dos;
@Override
publicvoidrun() {
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 = newbyte[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");
}
}
Post a Comment for "How To Send A Command To Android And Then Get Its Answer?"