android 設備唯一碼的獲取,Cpu號,Mac地址

開發Android應用中,我們常常需要設備的唯一碼來確定客戶端。


Android 中的幾中方法,使用中常常不可靠


1. DEVICE_ID
假設我們確實需要用到真實設備的標識,可能就需要用到DEVICE_ID。通過 TelephonyManager.getDeviceId()獲取,它根據不同的手機設備返回IMEI,MEID或者ESN碼.

缺點:在少數的一些設備上,該實現有漏洞,會返回垃圾數據

        2. MAC ADDRESS
我們也可以通過Wifi獲取MAC ADDRESS作爲DEVICE ID
缺點:如果Wifi關閉的時候,硬件設備可能無法返回MAC ADDRESS.。

        3. Serial Number
android.os.Build.SERIAL直接讀取
缺點:在少數的一些設備上,會返回垃圾數據
       4. ANDROID_ID
ANDROID_ID是設備第一次啓動時產生和存儲的64bit的一個數,
缺點:當設備被wipe後該數改變, 不適用。


android 底層是 Linux,我們還是用Linux的方法來獲取:


1 cpu號:

文件在: /proc/cpuinfo

通過Adb shell 查看:

adb shell cat /proc/cpuinfo

2 mac 地址

文件路徑 /sys/class/net/wlan0/address

adb shell  cat /sys/class/net/wlan0/address                               
xx:xx:xx:xx:xx:aa


這樣可以獲取兩者的序列號, 


方法確定,剩下的就是寫代碼了

以Mac地址爲例:

        String getMac() {
                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);


                        for (; null != str;) {
                                str = input.readLine();
                                if (str != null) {
                                        macSerial = str.trim();// 去空格
                                        break;
                                }
                        }
                } catch (IOException ex) {
                        // 賦予默認值
                        ex.printStackTrace();
                }
                return macSerial;
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章