由于android版本不一样获取网卡mac地址的方式也不一样,比较麻烦,但是java sdk里面的函数是可以获取里面的网卡内容。
代码如下:
/**
* 获取网络的mac地址
*
* @return xx:xx:xx:xx:xx
*/
public static String getMacAddress() {
try {
List all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
Log.d("NetworkInterface", nif.getName());
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return null;
}
StringBuilder res1 = new StringBuilder();
for (int i = 0; i < macBytes.length; i++) {
if (i == macBytes.length - 1) {
res1.append(String.format("%02x", macBytes[i]));
} else {
res1.append(String.format("%02x:", macBytes[i]));
}
}
Log.d("----", res1.toString());
return res1.toString();
}
} catch (Exception ex) {
Log.e(ex);
}
return null;
}
主要是获取出所有的网卡,然后根据名字获取需要的网卡mac地址