International Mobile Equipment Identity (IMEI) is the abbreviation of the International Mobile Device Identity (IMEI). It consists of 15 to 17 digits and is one-to-one with a Mobile phone. The device identifier will not change whether the device is swiped or restored to factory Settings, so it is especially useful for advertising and traffic statistics, which is favored by developers.
However, since Android29, TelephonyManager provides getImei method through android API has been unable to obtain the iMEI of the phone, so how should we obtain the unique device identifier of the phone?
The Android developer documentation provides the Android Id, which is valid most of the time for regular AD statistics, but will be reset if the phone is swiped, upgraded, or restored to factory. So in some business scenarios, such as using the device id as a unique user ID, it is not enough.
There are some online solutions, most of which use MAC addresses, UUid, or unique identifiers for specific phone manufacturers. Is it true that we can’t get the iMEI of the phone in Android 10? The answer is no. For example, under root permission, we can bypass Android’S API and directly get the iMEI of the phone.
The author assumes that the user’s mobile phone is already root and has been connected to the computer with android debugging bridge. Then we can print the IMEI of the mobile phone by entering adb command on the computer terminal:
adb shell service call iphonesubinfo 1
Copy the code
We might as well match the obtained IMEI through regular expression:
adb shell service call iphonesubinfo 3 i32 1 | awk -F "'" '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=
Copy the code
Since we can obtain iMEI in the terminal, we just need to use this command as a shell and run it in the code, but we need su permission, otherwise we cannot obtain iMEI:
public static String getImeiByShell(a){
try {
String shell = "service call iphonesubinfo 3 i32 1 | awk -F \"'\" '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=";
Process p=Runtime.getRuntime().exec(new String[]{"su"."-c",shell});
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
String readLine=br.readLine();
while(readLine! =null){
Log.d("execShell",readLine);
return readLine.trim();
}
if(br! =null){
br.close();
}
p.destroy();
p=null;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Copy the code
In this way, we can obtain the IMEI of the phone.