Skip to content

Instantly share code, notes, and snippets.

@ulinkwo
Created June 10, 2010 08:28
Show Gist options
  • Save ulinkwo/432709 to your computer and use it in GitHub Desktop.
Save ulinkwo/432709 to your computer and use it in GitHub Desktop.
public static String getLocalMAC() {
InetAddress address;
String sMAC = null;
try {
address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
Formatter formatter = new Formatter();
for (int i = 0; i < mac.length; i++) {
sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i],
(i < mac.length - 1) ? "-" : "").toString();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
return sMAC;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment