Android 獲取MAC地址最靠譜的方法。

       衆所周知在安卓開發中,咱們常用WifiManager獲取MAC地址作設備映射,但會出現這樣那樣的問題,好比說開了WIFI獲取不到地址了、不開WIFI獲取不到地址了、剛開機獲取不到地址了、網卡未啓動獲取不到地址了。綜上所述最靠譜的方法仍是直接用busybox讀取系統文件中的MAC地址比較穩妥。java

   public String getMacAddress() {
        String result = "";
        String Mac = "";
        result = callCmd("busybox ifconfig", "HWaddr");

        if (result == null) {
            return "網絡出錯,請檢查網絡";
        }
        if (result.length() > 0 && result.contains("HWaddr")) {
            Mac = result.substring(result.indexOf("HWaddr") + 6, result.length() - 1);
            if (Mac.length() > 1) {
                result = Mac.toLowerCase();
            }
        }
        return result.trim();
    }
相關文章
相關標籤/搜索