【问题标题】:raspberry pi java {Runtime.exec(sudo hcitool lescan);}树莓派 java {Runtime.exec(sudo hcitool lescan);}
【发布时间】:2017-04-17 11:56:35
【问题描述】:

我正在使用

Runtime.exec("sudo hcitool lescan --passive");

代码但 sudo hcitool lescan 命令未终止,因此无法完成 Runtime.exec

我也使用了sudo timeout 10s hcitool lescan --passive 命令完成良好,但无法处理java code import java.io.*; 上的 MAC 地址

我用过这段代码

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);
        }
    }
}

我想将 Beacon 的 MAC 地址转换为字符串。

我该怎么做?帮帮我,谢谢。

【问题讨论】:

    标签: java bluetooth-lowenergy raspberry-pi3


    【解决方案1】:

    多线程对我有用:

    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);
                }
            }
    }
    

    windows 的 cmd 命令也会阻塞,但另一个线程可以读取它。

    result
    Microsoft Windows [Version 10.0.14393]
    (c) 2016 Microsoft Corporation. T?m haklar? sakl?d?r.
    

    您可以在使用p 之前检查p 是否已初始化,而不是Thread.sleep(2000);

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2016-06-24
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2015-04-26
      • 2015-09-01
      相关资源
      最近更新 更多