Android获取Mac地址,支持android Q

1、声明权限

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

2、调用代码
1) android 6.0以下可以通过获取wifi服务来获取Mac地址

	/**
	 * 获取wifi的mac地址,适配到android Q
	 * @param paramContext
	 * @return
	 */
	public static String getMac(Context paramContext) {
		try {
			if (Build.VERSION.SDK_INT >= 23) {
				String str = getMacMoreThanM(paramContext);
				if (!TextUtils.isEmpty(str))
					return str;
			}
			// 6.0以下手机直接获取wifi的mac地址即可
			WifiManager wifiManager = (WifiManager)paramContext.getSystemService("wifi");
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			if (wifiInfo != null)
				return wifiInfo.getMacAddress();
		} catch (Throwable throwable) {}
		return null;
	}

2)android 6.0+需要遍历网络接口来获取硬件地址,即mac地址

/**
	 * android 6.0+获取wifi的mac地址
	 * @param paramContext
	 * @return
	 */
	private static String getMacMoreThanM(Context paramContext) {
		try {
			//获取本机器所有的网络接口
			Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
			while (enumeration.hasMoreElements()) {
				NetworkInterface networkInterface = (NetworkInterface)enumeration.nextElement();
				//获取硬件地址,一般是MAC
				byte[] arrayOfByte = networkInterface.getHardwareAddress();
				if (arrayOfByte == null || arrayOfByte.length == 0) {
					continue;
				}

				StringBuilder stringBuilder = new StringBuilder();
				for (byte b : arrayOfByte) {
					//格式化为:两位十六进制加冒号的格式,若是不足两位,补0
					stringBuilder.append(String.format("%02X:", new Object[] { Byte.valueOf(b) }));
				}
				if (stringBuilder.length() > 0) {
					//删除后面多余的冒号
					stringBuilder.deleteCharAt(stringBuilder.length() - 1);
				}
				String str = stringBuilder.toString();
				// wlan0:无线网卡 eth0:以太网卡
				if (networkInterface.getName().equals("wlan0")) {
					return str;
				}
			}
		} catch (SocketException socketException) {
			return null;
		}
		return null;
	}

你可能感兴趣的:(android)