5

I've got two problems with the android app I'm writing.

I'm reading out the local arp table from /proc/net/arp and save ip and corresponding mac address in a hash map. See my function. It's working properly.

    /**
     * Extract and save ip and corresponding MAC address from arp table in HashMap
     */
    public Map<String, String> createArpMap() throws IOException {      
        checkMapARP.clear();
        BufferedReader localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
        String line = "";       

        while ((line = localBufferdReader.readLine()) != null) {            
            String[] ipmac = line.split("[ ]+");
            if (!ipmac[0].matches("IP")) {
                String ip = ipmac[0];
                String mac = ipmac[3];
                if (!checkMapARP.containsKey(ip)) {
                    checkMapARP.put(ip, mac);               
                }                   
            }
        }
        return Collections.unmodifiableMap(checkMapARP);
    }
  1. First problem:

    I'm also using a broadcast receiver. When my app receives the State WifiManager.NETWORK_STATE_CHANGED_ACTION i check if the connection to the gateway is established. If true i call my function to read the arp table. But in this stage the system has not yet builded up the arp table. Sometimes when i receive the connection state the arp table is sill empty.

    Anyone got an idea to solve this?

  2. Second problem:

    I want to save the ip and mac address of the gateway in a persistent way. Right now i'm using Shared Preferences for this. Maybe it's better to write to an internal storage?

    Any tips?

Jomoos
  • 12,823
  • 10
  • 55
  • 92
IcedEarth
  • 51
  • 1
  • 2
  • Second problem. Use sqlite database. SharedPreferences may get cleared when application update occurs. So better put the values in sqlite – kumar Nov 30 '12 at 14:00
  • @kumar SharedPreferences should never get cleared unless it's a bug like http://b.android.com/2066 - http://stackoverflow.com/questions/3860823/are-shared-preferences-in-android-apps-deleted-when-a-user-updates-the-app – zapl Nov 30 '12 at 14:31
  • So it's okay to use SharedPreferences it think. – IcedEarth Nov 30 '12 at 16:17

1 Answers1

2

For the first problem you could start a new thread that runs that method after sleeping for a set amount of time or until it has some entries (Make a Runnable with a mailbox to get the Map) - unless you need to use the map directly, then I think the only way is to wait for the entries. For example (if you need to use the map directly):

public Map<String, String> createArpMap() throws IOException {      
    checkMapARP.clear();
    BufferedReader localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
    String line = "";       
    while ((line = localBufferdReader.readLine()) == null) {
        localBufferdReader.close();
        Thread.sleep(1000);
        localBufferdReader = new BufferedReader(new FileReader(new File("/proc/net/arp")));
    }
    do {            
        String[] ipmac = line.split("[ ]+");
        if (!ipmac[0].matches("IP")) {
            String ip = ipmac[0];
            String mac = ipmac[3];
            if (!checkMapARP.containsKey(ip)) {
                checkMapARP.put(ip, mac);               
            }                   
        }
    } while ((line = localBufferdReader.readLine()) != null);
    return Collections.unmodifiableMap(checkMapARP);
}
ddmps
  • 4,350
  • 1
  • 19
  • 34