I know that accessing the IMEI using the READ_PRIVILEGED_PHONE_STATE
permission in Android is not recommended. Starting from Android 10 (API level 29) and above, access to IMEI is restricted for privacy and security reasons. However, you can use the TelephonyManager
class to retrieve device-related information, such as the IMEI, on Android devices running on older versions before Android 10. By the way, it's not recommended to get IMEI, but you can use Android ID (Secure Setting Android ID - SSAID)
(it's recommended), also you can get Firebase (FID)
(it's recommended), or generate GUID
(fair practice), or my personal experience you can get WIFI or Bluetooth Mac Address (but remember it's not recommended too)... references:
Firebase, Android, Amazon
if it's necessary to have IMEI you should be sure the device's OS is below Android 10 else it won't work. First, make sure you grant READ_PHONE_STATE
permission, then here is the code:
val manager = activity!!.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity!!, arrayOf(Manifest.permission.READ_PHONE_STATE), 1000)
return
}
imei = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O) {
// print(manager.deviceId)
manager.deviceId
} else {
// print(manager.imei)
manager.imei
}