Android How to - Get Mac Address








Question

We would like to know how to get Mac Address.

Answer

/*from   w w  w.j ava  2s . c  o  m*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
class MacAddress {
  public static String getHex() {

    try {
      InetAddress address = InetAddress.getLocalHost();

      NetworkInterface ni = NetworkInterface.getByInetAddress(address);
      if (ni != null) {
        byte[] mac = ni.getHardwareAddress();
        if (mac != null) {

          String macaddress = "";
          for (int i = 0; i < mac.length; i++) {
            macaddress = macaddress
                + String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-"
                    : "");
          }
          return macaddress;
        } else {
          System.out
              .println("Address doesn't exist or is not " + "accessible.");
        }
      } else {
        System.out.println("Network Interface for the specified "
            + "address is not found.");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  public static String loadFileAsString(String filePath)
      throws java.io.IOException {
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
      String readData = String.valueOf(buf, 0, numRead);
      fileData.append(readData);
    }
    reader.close();
    return fileData.toString();
  } /* * Get the STB MacAddress */

  public static String getMacAddress() {
    try {
      return loadFileAsString("/sys/class/net/eth0/address").toUpperCase()
          .substring(0, 17);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
}