I'm using
Runtime.exec("sudo hcitool lescan --passive");
code but sudo hcitool lescan command does not terminate so Runtime.exec
cannot be finished.
I also used sudo timeout 10s hcitool lescan --passive
Command finishes well but cannot process with MAC Address on java code import java.io.*;
I used this code
public class scan{
public static void main(String args[]){
String s = null;
try {
Process p = Runtime.getRuntime().exec("sudo hcitool lescan --passive");
p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("result");
while((s=stdInput.readLine())!=null){
System.out.println(s);
}
while((s=stdError.readLine())!=null){
System.out.println(s);
}
System.exit(0);
}
catch (IOException e){
e.printStackTrace();
System.exit(-1);
}
}
}
I want to get Beacon's MAC Address to String.
How can I do it? help me, Thanks.
Multithreading works for me:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class LoopTest {
class Cmd extends Thread
{
public Process p = null;
public void run()
{
try {
p = Runtime.getRuntime().exec("cmd");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[]){
String s = null;
try {
Cmd cmd=new LoopTest().new Cmd();
ExecutorService es=Executors.newCachedThreadPool();
es.submit(cmd);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(cmd.p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(cmd.p.getErrorStream()));
System.out.println("result");
while((s=stdInput.readLine())!=null){
System.out.println(s);
}
while((s=stdError.readLine())!=null){
System.out.println(s);
}
System.exit(0);
}
catch (IOException e){
e.printStackTrace();
System.exit(-1);
}
}
}
cmd command for windows also blocks but another thread can read it.
result
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. T?m haklar? sakl?d?r.
instead of Thread.sleep(2000);
you can check if p
is initialized before using it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.