**
Android获取当前WiFi的MAC地址
**
package com.dogness.platform.utils;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* Android获取当前WiFi的MAC地址-适配所有版本
* Created by dcg on 2019/8/26.
*/
public class WifiMacUtils {
public static String getMac(Context mContext) {
String strMac = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
AppLog.Loge( "6.0以下");
strMac = getLocalMacAddressFromWifiInfo(mContext.getApplicationContext());
return strMac;
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AppLog.Loge("6.0以上7.0以下");
strMac = getMacAddress(mContext.getApplicationContext());
return strMac;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
AppLog.Loge("7.0以上");
if (!TextUtils.isEmpty(getMacAddress())) {
Log.e("=====", "7.0以上1");
strMac = getMacAddress();
return strMac;
} else if (!TextUtils.isEmpty(getMachineHardwareAddress())) {
AppLog.Loge( "7.0以上2");
strMac = getMachineHardwareAddress();
return strMac;
} else {
AppLog.Loge( "7.0以上3");
strMac = getLocalMacAddressFromBusybox();
return strMac;
}
}
return "02:00:00:00:00:00";
}
/**
* 根据wifi信息获取本地mac
* @param context
* @return
*/
public static String getLocalMacAddressFromWifiInfo(Context context){
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo winfo = wifi.getConnectionInfo();