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
![](https://csdnimg.cn/release/blogv2/dist/components/img/blogType.png)
Androd color
03-09
107
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
![](https://csdnimg.cn/release/blogv2/dist/components/img/blogType.png)
Android权限明细
01-12
221
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-08
237
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
569
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
419
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
2778
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
892
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-06
172
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-16
295
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-19
586
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-10
1493
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-09
3251
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)