/**
* 通过网络接口获取MAC地址
*type = eth0,获取有线mac
* type=wlan0,获取无线mac
* @return
*/
public static String getMac(String type) {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
Log.d(TAG,"nif==" + nif.getName());
if (!nif.getName().equalsIgnoreCase(type)) {
continue;
}
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return null;
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
Log.i(TAG, "MAC==" + res1.toString());
return res1.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Android 获取mac地址方法
最新推荐文章于 2024-07-10 03:15:11 发布