自己在开发安卓时获取mac地址时遇到一些问题,后来查询了一些资料和文章,总结出如下方法:

   
 /**
     * 根据wifi信息获取本地mac
     *
     * @param context
     * @return
     */
    public static String getLocalMacAddressFromWifiInfo(Context context) {
        String mac = StringUtil.getPreferences(context, "mac");
        if (null != mac) {
            Log.e("存储的mac地址", mac);
            return mac;
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mac = getMacDefault(context);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mac = getMacAddress();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mac = getMacFromHardware();
        }
        if (null != mac) {
            //mac 存储到本地,避免每次调用都要重新获取
            StringUtil.write2SharedPreferences(context, "mac", mac);
        }
        return mac;
    }

    /**
     * Android 6.0 之前(不包括6.0)获取mac地址
     * 必须的权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
     *
     * @param context * @return
     */
    public static String getMacDefault(Context context) {
        String mac;
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo winfo = wifi.getConnectionInfo();
        // 尝试打开WIFI,并获取mac地址,需要权限 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
        if (!wifi.isWifiEnabled()) {
            wifi.setWifiEnabled(true);
            return null;
        }
        mac = winfo.getMacAddress();
        if (null != mac) {
            StringUtil.write2SharedPreferences(context, "mac", mac);
        }
        Log.e("Android 6.0 之前获取的mac", mac);
        return mac;
    }

    /**
     * Android 7.0之后获取Mac地址
     * 遍历循环所有的网络接口,找到接口是 wlan0
     * 必须的权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
     *
     * @return
     */
    public static String getMacFromHardware() {
        try {
            ArrayList<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0"))
                    continue;
                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) return "";
                StringBuilder res1 = new StringBuilder();
                for (Byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }
                if (!TextUtils.isEmpty(res1)) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                Log.e("Android 7.0之后获取的mac", res1.toString());
                return res1.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Android 6.0-Android 7.0 获取mac地址
     */
    public static String getMacAddress() {
        String macSerial = null;
        String str = "";

        try {
            Process pp = Runtime.getRuntime().exec("cat/sys/class/net/wlan0/address");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            while (null != str) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();//去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        Log.e("Android 6.0-7.0 获取的mac", macSerial);
        return macSerial;
    }




    /**
     * @param tag
     * @param value
     * @description 写入sharedPreferences方法
     */
    public static void write2SharedPreferences(Context context, String tag, String value) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences("data-timetable", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(tag, value);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getPreferences(Context context, String tag) {
        SharedPreferences pref = context.getSharedPreferences("data-timetable", Context.MODE_PRIVATE);
        return pref.getString(tag, null);
    }

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐