0

So, I'm developing an app for enterprise-owned mobile phones, which are managed by Knox and Intune.

How do I get the device's IMEI programmatically?

I tried integrating the Intune and Graph SDKs but they are of no use, there's no explicit way to grant the READ_PRIVILEGED_PHONE_STATE permission to the app and no method or something to get that information.

And no, using the generic Android ID or other IDs won't do the job, it's mandatory that I get the IMEI programmatically for this use case.

Thanks in advance :)

Sujith Kumar
  • 872
  • 6
  • 19
jcss
  • 1
  • 1

2 Answers2

0

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
        }
A.R.B.N
  • 1,035
  • 2
  • 10
  • 20
  • Well like i said, for the use case i need the imei. and the devices are running android versions past 10. accordingly to the documentation, business owned and managed devices should have access to the TelephonyManager.getImei() function, but it does nothing (just returns null) – jcss May 20 '23 at 18:52
  • Most likely, your application lacks the necessary permissions/rights. Have you made sure that all [necessary rights](https://developer.android.com/reference/android/telephony/TelephonyManager#getImei(int)) have been granted for your application? – Risto May 22 '23 at 06:47
  • @joaomcspais I edited the answer for you. check it. – A.R.B.N May 22 '23 at 12:33
0

Since your requirements explicitly call for the use of Android 10 and higher and you absolutely need the IMEI, the decision to use KNOX is wrong. Samsung's KNOX documentation clearly states that the IMEI cannot be determined with Android 10 and higher.

But they also offer a solution to this problem by suggesting to migrate to Android Enterprise.

Risto
  • 983
  • 7
  • 14