How to obtain the unique ID (mobile phone serial number) of an Android phone

There are many scenarios and requirements where you need to use the unique identifier of the mobile device.

In Android , there are several ways to get such an ID.

1. The IMEI: Only valid for Android phones:

1
2
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String szImei = TelephonyMgr.getDeviceId(); 

With this method, a permission needs to be added to AndroidManifest.xml: android.permission.READ_PHONE_STATE, and the user should allow this application to be installed. As a mobile phone, the IMEI is unique, it should be similar to 359881030314356 (unless you have a mobile phone that is not mass-produced (parallel imports) it may have an invalid IMEI, such as: 00000000000000).

2. Pseudo-Unique ID, this is valid in any Android phone.
There are some special cases, some settings such as tablet computers do not have call function, or you do not want to add READ_PHONE_STATE permission. And you still want to get something like a unique serial number. At this point you can do this by pulling out the ROM version, manufacturer, CPU model, and other hardware information. This calculated ID is not unique (because if two phones use the same hardware and ROM mirroring). But it should be understood that the possibility of a similar situation can basically be ignored. To achieve this, you can use the Build class:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
String m_szDevIDShort = "35" + //we make this look like a valid IMEI 

Build.BOARD.length()%10 + 
Build.BRAND.length()%10 + 
Build.CPU_ABI.length()%10 + 
Build.DEVICE.length()%10 + 
Build.DISPLAY.length()%10 + 
Build.HOST.length()%10 + 
Build.ID.length()%10 + 
Build.MANUFACTURER.length()%10 + 
Build.MODEL.length()%10 + 
Build.PRODUCT.length()%10 + 
Build.TAGS.length()%10 + 
Build.TYPE.length()%10 + 
Build.USER.length()%10 ; //13 digits

Most of the Build members are in the form of strings, we only take their length information. We take 13 numbers and add "35" in front. This way the ID looks the same as the 15-digit IMEI.

3. The Android ID
通常被认为不可信,因为它有时为null。开发文档中说明了:这个ID会改变如果进行了出厂设置。并且,如果某个Andorid手机被Root过的话,这个ID也可以被任意改变。

1
String m_szAndroidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

Returns: 9774d56d682e549c . 无需任何许可。

4. The WLAN MAC Address string
是另一个唯一ID。但是你需要为你的工程加入android.permission.ACCESS_WIFI_STATE 权限,否则这个地址会为null。

1
2
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();

Returns: 00:11:22:33:44:55 (这不是一个真实的地址。而且这个地址能轻易地被伪造。).WLan不必打开,就可读取些值。

5. The BT MAC Address string
只在有蓝牙的设备上运行。并且要加入android.permission.BLUETOOTH 权限.

1
2
3
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter      
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();      
String m_szBTMAC = m_BluetoothAdapter.getAddress();

Returns: 43:25:78:50:93:38 . 蓝牙没有必要打开,也能读取。

Combined Device ID
综上所述,我们一共有五种方式取得设备的唯一标识。它们中的一些可能会返回null,或者由于硬件缺失、权限问题等获取失败。
但你总能获得至少一个能用。所以,最好的方法就是通过拼接,或者拼接后的计算出的MD5值来产生一个结果。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String m_szLongID = m_szImei + m_szDevIDShort 
    + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;      
// compute md5     
 MessageDigest m = null;   
try {
 m = MessageDigest.getInstance("MD5");
 } catch (NoSuchAlgorithmException e) {
 e.printStackTrace();   
}    
m.update(m_szLongID.getBytes(),0,m_szLongID.length());   
// get md5 bytes   
byte p_md5Data[] = m.digest();   
// create a hex string   
String m_szUniqueID = new String();   
for (int i=0;i<p_md5Data.length;i++) {   
     int b =  (0xFF & p_md5Data[i]);    
// if it is a single digit, make sure it have 0 in front (proper padding)    
    if (b <= 0xF) 
        m_szUniqueID+="0";    
// add number to string    
    m_szUniqueID+=Integer.toHexString(b); 
   }   // hex string to uppercase   
m_szUniqueID = m_szUniqueID.toUpperCase();

Through the above algorithm , 32-bit hexadecimal data can be generated:
9DDDF85AFF0A87974CE4541BD94D5F55
Now you can perform your application on it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851677&siteId=291194637