Android arp table - developing questions

I have two problems with the Android application I am writing.

I read the local arp table from /proc/net/arp and save the ip and the corresponding MAC address in the hash map. See My function. It is working fine.

  /** * 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); } 
  • First problem:

    I also use a broadcast receiver. When my application receives the status of WifiManager.NETWORK_STATE_CHANGED_ACTION , check if the connection to the gateway is established. If true, I call my function to read the arp table. But at this point, the system has not yet built the arp table. Sometimes, when I get the connection status, the arp table is a threshold.

    Anyone have an idea to solve this?

  • The second problem:

    I want to keep the IP address and MAC address of the gateway constant. Right now I'm using General Settings for this. Perhaps it is better to write to the internal memory?

    Any tips?

+4
source share
1 answer

For the first problem, you can start a new thread that starts this method after sleep for a set period of time or until it has some entries (do Runnable with a mailbox to get a map) - if you do not need to use a map directly, then I think the only way is to wait for the recordings. For example (if you need to use the card 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); } 
+1
source

All Articles