1

I'm currently creating a tool that can extract and search for data stored on a smartwatch for a University project.

I have been able to extract a file in particular called "Node.db" from my smartwatch which contains the Bluetooth MAC Address of the mobile phone the smartwatch is connected to. I am now trying to create a scanner than will scan this "node.db" file and print out the MAC Address.

This is the code I currently have:

// Identify the location of the node.txt file    
File file = new File("C:\\WatchData\\node.txt");
// Notify the user that Bluetooth extraction has initalized
Txt_Results.append("Pulling bluetooth data...");
        Scanner in = null;
        try {
            in = new Scanner(file);
            while(in.hasNext())
            {   // Scan till the end of the file
                String line=in.nextLine();
                // Scan the file for this string
                if(line.contains("settings.bluetooth"))
                // Print the MAC Address string out for the user
                    System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }      

A previous function converted the file to .txt. The code searches each line and looks for "settings.bluetooth" and should print out this line which contains the MAC Address if it is found. However, I believe the format of the node.db file is stopping the scanner from finding this string. I believe that some of the data in the file is encoded. An example of how the data is presented is shown below. I believe it is the black characters it doesn't recognize:

Picture of file lines

When I run the code on the file, the program will simply hang and provide no error message. I have left the program to run for over 20 minutes and still no success.

The exact line I am trying to print out from the file is shown below:

Line showing MAC Address of paired device

I have tested this code on a text file without these encoded characters and can conclude that the code does work. So my question is the following:

Is there a way that I can get the scanner to skip the characters it doesn't recognize in the file so it can continue scanning the file?

Thanks in advance.

JPM
  • 189
  • 1
  • 3
  • 14
  • Do you get any errors? – dryairship Apr 08 '16 at 11:50
  • @Hackerdarshi Hey there. The program simply hangs, no error messages shown. I have updated the question to include this information. – JPM Apr 08 '16 at 11:57
  • Can you include some lines (including the one which you want it to print; not all lines) of the file in your question.... – dryairship Apr 08 '16 at 12:00
  • I have added the line in the file which displays the MAC Address. – JPM Apr 08 '16 at 12:12
  • Use `hasNextLine()` instead of `hasNext()` and adding the encoding to the `new Scanner(..., encoding)` as answered, is less likely to cause troubles. Try "ISO-8859-1" to, as that should accept all values. Skipping chars is doable, but no problem. Maybe `System.out.println(line.replaceAll("\\P{Ascii}", ""));` - removing non-ASCII. – Joop Eggen Apr 08 '16 at 14:17

1 Answers1

1

Since you didn't provide the files here, so I can't write code to test on your files. It looks like your files have an different encoding than that Java uses to decode it.

So, you need to try different encoding settings for your input stream.

Usually, you specify the encoding by:

String encoding = "UTF-8"; // try "UTF-8" first and also change to other encodings to see the results
Reader reader = new InputStreamReader(new FileInputStream("your_file_name"), encoding);

Refer to this post for more information. This post also talks about how to write code to detect the encoding of your file.

BTW, the decoded characters shown in your file with a dark background are some control characters in ASCII.

I would also suggest you try changing the decoding method of your text viewer application to see if you can actually make the text display correctly in a particular encoding method.

UPDATE

It looks like Scanner doesn't work while using other IO class actually works fine.

StringBuilder sb = new StringBuilder();

try (BufferedReader reader = new BufferedReader(new FileReader("node.txt"))) {

    String line;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

} catch (Exception e) {
    // TODO: handle exception
}


int index = sb.indexOf("settings.bluetooth");
if (index != -1)
    System.out.println(sb.substring(index, index + 18));

UPDATE

It looks like only when you create a Scanner from a File will cause an exception in one of the Scanner's inner method when reading from the file. But using an input stream as below will always work, even wrapping it inside a Scanner.

try (Scanner s = new Scanner(new FileInputStream("node.txt"))) {
    while(s.hasNext()) {
        System.out.println(s.next());
    }
} catch (Exception e) {
    e.printStackTrace();
}

UPDATE

This solution just eliminates all the illegal characters from your file.

public static void main(String args[]) {
    String encoding = "UTF-8"; // try "UTF-8" first and also change to other encodings to see the results

    StringBuilder sb = new StringBuilder();
    try(Reader reader = new InputStreamReader(new FileInputStream("node.txt"), encoding)) {
        int c = -1;
        while ((c = reader.read()) != -1) {
            if (eligible(c)) {
                sb.append((char)c);
            }
        }
    } catch (Exception e){
        e.printStackTrace();
    }

    int index = sb.indexOf("settings.bluetooth");
    if (index >= 0) {
        System.out.println(sb.substring(index));
    }
}

public static boolean eligible(int c) {
    return (c >= 'a' && c <= 'z' || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.');
}
Community
  • 1
  • 1
lkq
  • 2,326
  • 1
  • 12
  • 22
  • I can only provide a link to a sample of the file as the whole file contains about 38000 lines and also contains some sensitive data. But a link to a sample of the file can be found here: http://www.mediafire.com/download/523h456eub8j7c0/node.txt – JPM Apr 08 '16 at 14:53
  • @JPM `Scanner` doesn't work for this file. I'm still looking for the reason. However, using a `FileReader` works fine as in my updated answer. – lkq Apr 08 '16 at 15:31
  • Hi there and thank you for your help. I've just used your code and had to change the "+18" to "+65" to ensure it showed the whole line. However it showing some encoded data as seen here: http://i.imgur.com/92R94rA.png. Is there anyway to get rid of the squares? Thanks in advance. – JPM Apr 08 '16 at 16:32
  • @JPM I would suggest you to eliminate these characters if you're only interested in the MAC address. To do this, just read from the file one character a time and if it's legal, say 'a-z(A-Z)', '.' or '1-9', append it to a `StringBuilder` and then look up `settings.bluetooth` in that string. – lkq Apr 08 '16 at 18:08
  • Hi there. I understand what you're saying but I'm not entirely sure how I would be able to add that to the code. Would you be able to update the answer with this? Thanks. – JPM Apr 09 '16 at 13:54
  • @JPM Updated as you requested. – lkq Apr 09 '16 at 18:05