1

I am using the Java exec command to issue a "hcitool scan" command in order to perform a Bluetooth Scan.

The output is in the exact same format as it would be if i were to type in the command to the terminal

scanning...
mac address      bluetoothName
Done

I want to be able to split the returned string down, so that i can store the found MAC Addresses as a String.

My code so far is as follows:

import java.io.*;

public class altBluetooth
{
  public static void main(String args[])
  {
    try
    {
      Process p=Runtime.getRuntime().exec("hcitool scan");
      p.waitFor();
      BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line=reader.readLine();

      while(line!=null)
      {
        System.out.println(line);
        line=reader.readLine();
      }
    }
    catch(IOException e1) {
      System.out.print("This didnt work - exception 1");
    }
    catch(InterruptedException e2) {
      System.out.print("This didnt work - exception 2");
    }

    System.out.println("Done");
  }
} 
5
  • 7
    Doesn't look like you've even tried. You do know that String has a split method, right? Stop being lazy and DIY. Commented May 11, 2011 at 17:50
  • 2
    do you have more details about the exact output of the lines? are they tab-delimited? just space? can there be space inside any of the fields? Commented May 11, 2011 at 17:52
  • 1
    String.split() should do the trick. You may want to read about regular expressions. I also recommend to get familiar with the Java documentation, or a great book "Thinking in Java". The basics (and much more) are covered there. Commented May 11, 2011 at 17:53
  • Note that p.waitFor() might never finish if you don't start reading the output right away. If the output of hcitool scan gets too big, the internal output buffer by the OS might block! Commented May 11, 2011 at 17:55
  • The output is tab-delimited - I want to discard the bluetooth name and store only the MAC address. I will give the answer given a try. Commented May 11, 2011 at 18:01

2 Answers 2

2
String[] parts = line.split("\\s+");

probably

Sign up to request clarification or add additional context in comments.

Comments

0

If you need advanced splitting you can use com.google.common.base.Splitter from Google Guava

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.