I have connected three cameras to my phone and i want to know their ip addresses. In windows and linux its with the arp command but this command does not run on the android terminal . How can i run arp command on android or it there any other way to achieve the same?
use this code work for me.
try {
Process process = Runtime.getRuntime().exec("cat /proc/net/arp");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
String output = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Log.d("*************", ""+output);
}
catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
return "";
Related
I have the following code:
try {
String command = "ps";
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
// Do some stuff
}
bufferedReader.close();
} catch (IOException e) {
// Do some stuff
}
Android Studio is now issuing the following error:
error: incompatible types: java.lang.Process cannot be converted to android.os.Process
I could have sworn this code was working the last time I looked at it. I tried invalidating caches and restarting to no avail. Can someone point out what's going on?
I am trying to receive port based SMS with the below piece of code.
serverSocket = new ServerSocket(SERVERPORT);
Socket client = serverSocket.accept();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
System.out.println("Reading Line is>>>>>>>>>>>>>"+line);
break;
}
} catch (Exception e) {
System.out.println("Exception While Reading SMS>>>>>>>>>>"+e);
}
Will it wait in the line of serverSocket.accept(); until it gets the port based SMS,Is this correct behaviour or I am making any issue which hangs at that place.I am not able to move beyond it.
I am not able to test fully,I am not having option of testing it here,sending the port message.
Did anyone came across this issue.Any Info regarding this will be useful.
I think you could try adding the while statement
serverSocket = new ServerSocket(SERVERPORT);
while(true){
Socket client = serverSocket.accept();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
System.out.println("Reading Line is>>>>>>>>>>>>>"+line);
break;
}
} catch (Exception e) {
System.out.println("Exception While Reading SMS>>>>>>>>>>"+e);
}
}
and for as long as it's true, it will wait for the client to send a message. It's been a while since i last did one of these Working with Datagrams
i want to get logcat data programmatically i used the below code
StringBuilder debuglog=new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debuglog.append(line);
debuglog.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Above code is working fine
This line "logcat -d" shows -d,-w,-i messages
But,my problem is i want to show -e messages
To get only ERROR use this:
Process process = Runtime.getRuntime().exec("logcat -d *:E");
All I want to achieve is to get the response from the execution.
For example if I execute command like this "ls"
I want to get string with all the files and directories
For example like this
Runtime.getRuntime().exec("ls");
But I do not know how to get the response.
I run something like this, I thought that I will redirect the output but still nothing
Runtime.getRuntime().exec("ls",null,new File("/sdcard/myFile") );
I tried also something like this but still nothing
Runtime.getRuntime().exec("echo | ls > /sdcard/myfile");
Any ideas ?
Have you tried this?
String cmd = "commands";
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()).length() > -1)
{
log.append(line);
}
try this code
try {
String[] commands = {"dumpstate > /sdcard/log1.txt"};
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
I am trying to get the output of android shell command 'getprop' with java since getprop() always returns null no matter what.
I tried this from developer.android.com:
Process process = null;
try {
process = new ProcessBuilder()
.command("/system/bin/getprop", "build.version")
.redirectErrorStream(true)
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = process.getInputStream();
//String prop = in.toString();
System.out.println(in);
process.destroy();
However what is printed is not the output but a bunch of characters and numbers (dont have the exact output right now).
How can i get the output of the process?
Thanks!
Is there any particular reason why you want to run the command as an external process?
There is a simpler way:
String android_rel_version = android.os.Build.VERSION.RELEASE;
However, if you really want to do it via a shell command, here is the way I got it to work:
try {
// Run the command
Process process = Runtime.getRuntime().exec("getprop");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Grab the results
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line + "\n");
}
// Update the view
TextView tv = (TextView)findViewById(R.id.my_text_view);
tv.setText(log.toString());
} catch (IOException e) {
}