获取Mac地址

public class DeviceMacUtil {

    /**
     * Android  6.0 之前(不包括6.0)
     * 必须的权限  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     * @param context
     * @return
     */
    private static String getMacDefault(Context context) {
        String mac = null;
        if (context == null) {
            return mac;
        }

        WifiManager wifi = (WifiManager) context.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        if (wifi == null) {
            return mac;
        }
        WifiInfo info = null;
        try {
            info = wifi.getConnectionInfo();
        } catch (Exception e) {

        }
        if (info == null) {
            return null;
        }
        mac = info.getMacAddress();
        if (!TextUtils.isEmpty(mac)) {
            mac = mac.toUpperCase(Locale.ENGLISH);
        }
        return mac;
    }

    /**
     * Android 6.0(包括) - Android 7.0(不包括)
     * @return
     */
    private static String getMacAddress() {
        String WifiAddress =  null;
        try {
            WifiAddress = new BufferedReader(new FileReader(new File("/sys/class/net/wlan0/address"))).readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return WifiAddress;
    }

    /**
     * 遍历循环所有的网络接口,找到接口是 wlan0
     * 必须的权限 <uses-permission android:name="android.permission.INTERNET" />
     * @return
     */
    private static String getMacFromHardware() {
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            Log.d("Utils", "all:" + all.size());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0")){
                    continue;
                }

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return null;
                }
                Log.d("Utils", "macBytes:" + macBytes.length + "," + nif.getName());

                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMacFromHardware(Context context) {

        String macAddress;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            macAddress = getMacDefault(context);
            if (macAddress != null ) {
                Log.d("Utils", "android 5.0以前的方式获取mac"+macAddress);
                macAddress =  macAddress.replaceAll(":","");
                if(macAddress.equalsIgnoreCase("020000000000")== false){
                    return macAddress;
                }
            }
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            macAddress = getMacAddress();
            if (macAddress != null ) {
                Log.d("Utils", "android 6~7 的方式获取的mac"+macAddress);
                macAddress =  macAddress.replaceAll(":","");
                if(macAddress.equalsIgnoreCase("020000000000")== false){
                    return macAddress;
                }
            }
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            macAddress = getMacFromHardware();
            if (macAddress != null ) {
                Log.d("Utils", "android 7以后 的方式获取的mac"+macAddress);
                macAddress =  macAddress.replaceAll(":","");
                if(macAddress.equalsIgnoreCase("020000000000") == false){
                    return macAddress;
                }
            }
        }

        Log.d("Utils", "没有获取到MAC");
        return null;
    }

 

posted @ 2020-05-09 10:43  青竹玉简  阅读(606)  评论(0编辑  收藏  举报