Skip to content

Instantly share code, notes, and snippets.

@5ZSQ
Created November 10, 2017 08:21
Show Gist options
  • Save 5ZSQ/c69e23a01abe097ea16926a94bfa0d4e to your computer and use it in GitHub Desktop.
Save 5ZSQ/c69e23a01abe097ea16926a94bfa0d4e to your computer and use it in GitHub Desktop.
Android - 获取mac地址
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
/**
* 获取Mac地址
*/
public class MacUtils {
public static String getMacAddr() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "02:00:00:00:00:00";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment