public class WlanMacAddressGetter {
private static final String TAG = WlanMacAddressGetter.class.getName();
public static String getWlanMacAddress() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
if (networkInterface.getName().equals("wlan0")) {
StringBuilder mac = new StringBuilder();
byte[] hardwareAddress = networkInterface.getHardwareAddress();
String hex = Integer.toHexString(hardwareAddress[0] & 0xff);
if (hex.length() == 1) {
mac.append('0');
}
mac.append(hex);
for (int i = 1; i < hardwareAddress.length; ++i) {
mac.append(':');
hex = Integer.toHexString(hardwareAddress & 0xff);
if (hex.length() == 1) {
mac.append('0');
}
mac.append(hex);
}
return mac.toString();
}
}
} catch (SocketException ex) {
Log.e(TAG, null, ex);
}
return null;
}
}
注意:须声明权限android.permission.INTERNET,否则在获取MAC时会引发SocketException
博客
Androd color
03-09
101
博客
Android权限明细
01-12
214
12-08
225
11-04
552
11-04
392
11-04
2747
11-04
882
09-06
166
08-16
284
07-19
577
07-10
1479
07-09
3223
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交