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
170


Android权限明细
01-12
297

12-08
331

11-04
655

11-04
545

11-04
2895

11-04
978

09-06
226

08-16
371

07-19
693

07-10
1553

07-09
3419
