编程知识 cdmana.com

The IMEI obtained by Android has only 14 bit problems solved

This article has been included in Android A collection of miscellaneous diseases ( Continuous updating )

1、 Problems encountered

On the information in the phone settings IMEI Yes 15 Digit number , But the only way to get it through code is 14 position , Missing the last digit . The phone reboots , The code gets the right 15 Digit number . The final test phenomenon , The acquisition method is uncontrollable , The return value may be 14 It may also be 15 position , It is easy to cause data abnormality when using .

2、 obtain IMEI In the usual way

public static String getIMEI(Context context){
	String imei = "";
	try {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
		if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
			imei = tm.getDeviceId();
		}else {
			Method method = tm.getClass().getMethod("getImei");
			imei = (String) method.invoke(tm);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return imei;
}

3、 Solution

3.1 IMEI Structure

The first part TAC,Type Allocation Code, Type assignment code , from 8 Digit composition ( In the early days 6 position ), It's the code that distinguishes the brand and model of a mobile phone , The code is provided by GSMA And its authorized agencies . among TAC The first two digits of the code are the identification of the distribution organization (Reporting Body Identifier), It's authorization IMEI Code of the code Distribution Agency , Such as 01 For America CTIA,35 For the UK BABT,86 For China TAF.
The second part FAC,Final Assembly Code, Final assembly code , from 2 Digit numbers make up , Only in the early days TAC Code for 6 There is , therefore TAC and FAC The total is 8 Digit number .FAC Code is used to distinguish the production place code within the manufacturer .
The third part SNR,Serial Number, Serial number , From the first 9 Bit start 6 Digit composition , Distinguish the production serial number of each mobile phone .
The fourth part CD,Check Digit, Verification Code , From before 14 The digit number passes through Luhn The algorithm calculates that .

From that , The last one can be directly from the front 14 Bit by bit , Then we can judge the length directly and calculate it again .

3.2 Luhn Algorithm

  1. Multiply even digits by 2, Calculate the sum of the single digits and the ten digits respectively
  2. Add odd numbers to , Plus the value calculated in the previous step
  3. If the number of bits you get is 0 The check bit is 0, Otherwise 10 Minus the single digits

Such as :35 89 01 80 69 72 41

Even digits multiplied by 2 obtain

5*2=10 、 9*2=18 、 1*2=02 、 0*2=00 、 9*2=18 、 2*2=04 、 1*2=02

Calculation ( Odd digits )+( Ten bits of the product of even bits calculated in the previous step + bits ), obtain

3+(1+0)+8+(1+8)+0+(0+2)+8+(0+0)+6+(1+8)+7+(0+4)+4+(0+2)=63

The last check bit is 10-3 = 7

So the whole thing is 358901806972417

3.3 Java Realization

public static String getIMEI14To15(String imei) {
	int last = 0, i = 0, temp;
	while (i < imei.length()){
		//  Add odd digits 
		result += Integer.parseInt(imei.substring(i, ++i));
		//  Even digits multiplied by 2
		temp = Integer.parseInt(imei.substring(i, ++i)) * 2;
		//  Add ten digits to the even , Here's the optimization operation 
		//  Even digit multiplication 2 The product range of is [0, 18], Less than 10 Direct addition , Greater than 10 Then the sum of the bits and the tens is equal to the product minus 9
		result += temp < 10 ? temp : temp - 9;
	}
	//  Take the place of the final sum 
	result %= 10;
	//  if 0 Then for 0, Otherwise 10 Minus a bit 
	result = result == 0? 0 : 10 - result;
	return imei + result;
}

public static String getIMEI(Context context){
	String imei = "";
	try {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
		if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
			imei = tm.getDeviceId();
		}else {
			Method method = tm.getClass().getMethod("getImei");
			imei = (String) method.invoke(tm);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	if (imei != null && imei.length() == 14) {
		imei = getIMEI14To15(imei);
	}
	return imei;
}

版权声明
本文为[coder-ice]所创,转载请带上原文链接,感谢

Scroll to Top