android 裝置唯一碼的擷取,Cpu號,Mac地址

來源:互聯網
上載者:User

開發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;
        }


作者:hpccn

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.