获取Android手机的Mac地址的工具类

时间:2022-09-11 19:01:57

首先添加权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

接下来是具体的方法:

public static String getMacAddress(Context context) {
// 获取mac地址:
String macAddress = "000000000000";
try {
WifiManager wifiMgr = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = (null == wifiMgr ? null : wifiMgr
.getConnectionInfo());
if (null != info) {
if (!TextUtils.isEmpty(info.getMacAddress()))
macAddress = info.getMacAddress().replace(":", "");
else
return macAddress;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return macAddress;
}
return macAddress;
}