67,512
社区成员
发帖
与我相关
我的任务
分享
/**
* 获取本地MAC地址
*/
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;
}