Android中獲取手機(jī)IMEI,IMSI, MAC(Android 6.0)工具類(標(biāo)識用戶唯一)

<font color=FF0000> LZ-Says:給大家推薦一個網(wǎng)站,有興趣可以查閱,想為大家貢獻(xiàn)一點自己的力量也可以投稿,老大審核通過會發(fā)表,更好的幫助有需要的人歡迎大家踴躍投稿地址如下:
http://www.123si.org/android

最近項目中用到獲取手機(jī)IMEI,IMSI以及MAC,在此記錄一下,方便你我他。。。

那么什么時候會用到這些東西呢?

LZ 個人認(rèn)為,如果項目當(dāng)中需要對當(dāng)前使用用戶設(shè)備做唯一標(biāo)識時(證明這個是你用的)可以使用這種方式。當(dāng)然了這也是LZ個人的一點理解,歡迎大家打臉~

那么首先簡單普及一下這三個分別都是什么鬼。。。

1. IMEI

IMEI(International Mobile Equipment Identity)是國際移動設(shè)備身份碼的縮寫,國際移動裝備辨識碼,是由15位數(shù)字組成的"電子串號",它與每臺移動電話機(jī)一一對應(yīng),而且該碼是全世界唯一的。每一只移動電話機(jī)在組裝完成后都將被賦予一個全球唯一的一組號碼,這個號碼從生產(chǎn)到交付使用都將被制造生產(chǎn)的廠商所記錄。
PS:通俗來講就是標(biāo)識你當(dāng)前設(shè)備(手機(jī))全世界唯一,類似于個人身份證,這個肯定唯一啦~

2. IMSI

國際移動用戶識別碼(IMSI:International Mobile Subscriber Identification
Number)是區(qū)別移動用戶的標(biāo)志,儲存在SIM卡中,可用于區(qū)別移動用戶的有效信息。其總長度不超過15位,同樣使用0~9的數(shù)字。其中MCC是移動用戶所屬國家代號,占3位數(shù)字,中國的MCC規(guī)定為460;MNC是移動網(wǎng)號碼,由兩位或者三位數(shù)字組成,中國移動的移動網(wǎng)絡(luò)編碼(MNC)為00;用于識別移動用戶所歸屬的移動通信網(wǎng);MSIN是移動用戶識別碼,用以識別某一移動通信網(wǎng)中的移動用戶
PS:通俗來講就是標(biāo)識你當(dāng)前SIM卡(手機(jī)卡)唯一,同樣類似于個人身份證,肯定唯一啦~

3. MAC

MAC(Media Access Control或者M(jìn)edium Access Control)地址,意譯為媒體訪問控制,或稱為物理地址、硬件地址,用來定義網(wǎng)絡(luò)設(shè)備的位置。在OSI模型中,第三層網(wǎng)絡(luò)層負(fù)責(zé) IP地址,第二層數(shù)據(jù)鏈路層則負(fù)責(zé) MAC地址。因此一個主機(jī)會有一個MAC地址,而每個網(wǎng)絡(luò)位置會有一個專屬于它的IP地址
PS:通俗來講就是標(biāo)識你當(dāng)前使用我這個軟件(功能)時的地址,方便在你干壞事的時候警察叔叔抓你~
最主要的是:在平板設(shè)備上,無法通過imei標(biāo)示設(shè)備,我們會將mac地址作為用戶的唯一標(biāo)識

好啦,下面貼出獲取這三項的代碼。。。


import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

/**
 * 獲取手機(jī)信息工具類
 * 
 * @author HLQ
 * @createtime 2016-12-7下午2:06:03
 * @remarks
 */
public class MobileInfoUtil {

    /**
     * 獲取手機(jī)IMEI
     * 
     * @param context
     * @return
     */
    public static final String getIMEI(Context context) {
        try {
            //實例化TelephonyManager對象
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            //獲取IMEI號
            String imei = telephonyManager.getDeviceId();
            //在次做個驗證,也不是什么時候都能獲取到的啊
            if (imei == null) {
                imei = "";
            }
            return imei;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 獲取手機(jī)IMSI
     */
    public static String getIMSI(Context context){
        try {
            TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            //獲取IMSI號
            String imsi=telephonyManager.getSubscriberId();
            if(null==imsi){
                imsi="";
            }
            return imsi;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    
}

2017年6月10日00:26:48 刪除之前獲取MAC地址方法,重新更新一下:

哎,有時候這活干的真是心累,沒轍按著自己的方向繼續(xù)前行吧

今天,突然給我發(fā)個鏈接,說什么Android 6.0獲取MAC地址,返回的都是02:00:00:00:00:00

突然一蒙圈,然后回過頭看看以前提交的數(shù)據(jù),日了狗,還真是!!!

經(jīng)過排查,發(fā)現(xiàn)了下面一些結(jié)論,了解了解:

從Android 6.0之后,android 移除了通過 WiFi 和藍(lán)牙 API 來在應(yīng)用程序中可編程的訪問本地硬件標(biāo)示符。現(xiàn)在 WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法都將返回 02:00:00:00:00:00 。

So,如今,我們也更新下獲取MAC地址方式,如下:

    /**
     * 兼容Android 6.0以上設(shè)備獲取MAC地址
     * 物理地址、硬件地址,用來定義網(wǎng)絡(luò)設(shè)備的位置
     * 兼容原因:從android 6.0之后,android 移除了通過 WiFi 和藍(lán)牙 API 來在應(yīng)用程序中可編程的訪問本地硬件標(biāo)示符。現(xiàn)在 WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法都將返回 02:00:00:00:00:00
     * add by heliquan at 2017年6月12日
     *
     * @return
     */
    public static String getMobileMAC(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 如果當(dāng)前設(shè)備系統(tǒng)大于等于6.0 使用下面的方法
            return getHeightMac();
        } else {
            return getLowerMac(context);
        }
    }

    /**
     * 兼容Android 6.0以下設(shè)備獲取MAC地址 Android API < 23
     *
     * @return
     */
    private static String getLowerMac(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            // 獲取MAC地址
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String mac = wifiInfo.getMacAddress();
            if (null == mac) {
                // 未獲取到
                mac = "";
            }
            return mac;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 獲取高版本手機(jī)的MAC地址 Android API >= 23
     *
     * @return
     */
    private static String getHeightMac() {
        String str = "";
        String macSerial = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (macSerial == null || "".equals(macSerial)) {
            try {
                return loadFileAsString("/sys/class/net/eth0/address")
                        .toUpperCase().substring(0, 17);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return macSerial;
    }

    private static String loadFileAsString(String fileName) throws Exception {
        FileReader reader = new FileReader(fileName);
        String text = loadReaderAsString(reader);
        reader.close();
        return text;
    }

    private static String loadReaderAsString(Reader reader) throws Exception {
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[4096];
        int readLength = reader.read(buffer);
        while (readLength >= 0) {
            builder.append(buffer, 0, readLength);
            readLength = reader.read(buffer);
        }
        return builder.toString();
    }

結(jié)果如下,測試設(shè)備比較少,見諒~

使用Android 6.0獲取:"mac":"ac:c1:ee:8d:78:03"

使用Android 5.0獲取:"mac":"38:bc:1a:d6:e6:1a"

期待Android越來越好~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容