get Mac From Arp Cache - Android java.net

Android examples for java.net:Mac Address

Description

get Mac From Arp Cache

Demo Code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

  public static String getMacFromArpCache(String ip) {
    if (ip == null)
      return null;
    BufferedReader br = null;/*from  w w  w  .ja v a 2  s.co m*/
    try {
      br = new BufferedReader(new FileReader("/proc/net/arp"));
      String line;
      while ((line = br.readLine()) != null) {
        String[] splitted = line.split(" +");
        if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) {
          // Basic sanity check
          String mac = splitted[3];
          if (mac.matches("..:..:..:..:..:..")) {
            return mac;
          } else {
            return null;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }

}

Related Tutorials