Android 獲取mac地址

由于項目開發(fā)需要獲取設(shè)備的mac地址,因此查閱了Android獲取mac地址相關(guān)信息,本篇博客轉(zhuǎn)載自《Android獲取Mac地址-適配所有版本》,并在此基礎(chǔ)上進行進一步整理。
根據(jù)Android版本,可將Android獲取mac的方法分為三類: Android 6.0以下,Android 6.0以上、7.0以下, Android 7.0以上。

1. Android 6.0以下

在Android 6.0以下,首先需要獲取如下權(quán)限:

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

獲取mac地址的方法如下:

/**
 * Android 6.0 之前(不包括6.0)獲取mac地址
 * 必須的權(quán)限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
 * @param context * @return
 */
public static String getMacDefault(Context context) {
    String mac = "";
    if (context == null) {
        return mac;
    }
    WifiManager wifi = (WifiManager)context.applicationContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = null;
    try {
        info = wifi.getConnectionInfo();
    } catch (Exception e) {
        e.printStackTrace();
    }

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

2.Android 6.0以上、7.0以下

android 6.0以后 將不再能通過 wifimanager 獲取mac,獲取到的mac將是固定的:02:00:00:00:00:00 。android sdk后來做了6.0適配,通過cat /sys/class/net/wlan0/address,可以在6.0上獲取mac地址。
獲取mac地址的方法如下:

/**
 * 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) {
        // 賦予默認(rèn)值
        ex.printStackTrace();
    }

    return macSerial;
}

3.Android 7.0以上

android 7.0 后,通過上述適配的方法,將獲取不到mac地址。
經(jīng)過調(diào)研和測試,7.0上仍有辦法獲取mac地址:
總共分為三種方式:
(1)通過ip地址來獲取綁定的mac地址
(2)掃描各個網(wǎng)絡(luò)接口獲取mac地址
(3)通過busybox獲取本地存儲的mac地址
以下為通過(2)方法獲取mac地址,其他方法請參考原博客:

/**
 * Android 7.0之后獲取Mac地址
 * 遍歷循環(huán)所有的網(wǎng)絡(luò)接口,找到接口是 wlan0
 * 必須的權(quán)限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 * @return
 */
public static String getMacFromHardware() {
    try {
        Enumeration<NetworkInterface> all = 
Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.name.equals("wlan0", ignoreCase = true)) 
                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)
            }
            return res1.toString()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return ""
}

最后,將三個方法整合,就能適配所有Android版本了:

/**
 * 獲取mac地址(適配所有Android版本)
 * @return
 */
fun getMac(context: Context): String? {
    String mac: String? = ""
    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()
    }
    return mac
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容