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

開發Android應用中,咱們經常須要設備的惟一碼來肯定客戶端。android

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

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的方法來獲取:ui

1 cpu號:spa

文件在: /proc/cpuinfoip

經過Adb shell 查看:開發

adb shell cat /proc/cpuinfoget

2 mac 地址input

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

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

這樣能夠獲取二者的序列號, 

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

以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;
        }
相關文章
相關標籤/搜索