Get IMEI Android Kotlin

Get IMEI Android kotlin

1. Overview

In this article, we will discuss getting IMEI of Android device SIM in Kotlin.

Over time, Android provided functions such as getDeviceId(), getImei() (IMEI for GSM) or getMeid() (MEID for CDMA) to get IMEI information programmatically.

Android deprecated the getDeviceId in Android 26 and above devices. It recommends using the getImei and getMeid functions.

2. Get IMEI Android Kotlin

Starting in Android 10, the third-party apps cannot access the above functions to get the device’s IMEI and serial number. This is to guard the device identifiers

The app must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to get the details. This permission is available only to system apps. So, the third-party apps installed from the Google Play Store cannot declare privileged permissions.

  • If your app targets API level 28 or lower, and the app has the READ_PHONE_STATE permission then the method returns null.
  • If your app targets API level 28 or lower, and the app does not have the READ_PHONE_STATE permission, or your app targets API level 29 or higher, then throws a SecurityException.

3. Get IMEI Android Kotlin in System app

If your app is a system app, then declare the READ_PRIVILEGED_PHONE_STATE permission in your manifest file and access IMEI:

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (VERSION.SDK_INT >= 26) {
   imei = telephonyManager.getImei()
} else {
   imei = telephonyManager.getDeviceId()
}
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>

4. Get unique identifier in third-party apps

Since third-party apps cannot access IMEI information, Android recommends using resettable identifiers. However, if the third-party apps meet any of the following criteria, then they can collect IMEI information from the device.

  • If your app is the device owner of a fully managed device, a profile owner of an organization owned device, or their delegates. Refer the official documentation DevicePolicyManager.getEnrollmentSpecificId() for more information.
  • App with carrier privileges and TelephonyManager.hasCarrierPrivileges() returns true on any active subscription.
  • App is the default SMS role holder and RoleManager.isRoleHeld(RoleManager.ROLE_SMS) returns true.

5. Conclusion

To sum up, we have learned about getting the IMEI number of Android devices programmatically in Kotlin. Refer to Android 12 articles for the latest updates on Android.

Leave a Comment