get Ethernet Network MAC Address - Android Network

Android examples for Network:Mac Address

Description

get Ethernet Network MAC Address

Demo Code

import java.net.NetworkInterface;
import java.net.SocketException;


public class Main {

  public static String getEthernetNetworkMACAddress() {
    NetworkInterface NIC;/*from ww  w .  j  a va 2  s  .c om*/
    StringBuffer mac = new StringBuffer();
    try {
      NIC = NetworkInterface.getByName("eth0");
      byte[] buf = NIC.getHardwareAddress();
      for (int i = 0; i < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
          mac.append("0" + hex);
        } else {
          mac.append(hex);
        }
      }
    } catch (SocketException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return mac.toString();
  }
}

Related Tutorials