Can't get IMEI as device owner on Android 10

Viewed 1531

For a special kiosk setup I need the IMEI of the device. Since Android 10 it is not possible for regular apps to access device identifiers like the IMEI. However the documentation suggests:

This API requires one of the following:

  • If the caller is the device or profile owner, the caller holds the Manifest.permission#READ_PHONE_STATE permission.
  • ...

I've set the app as device owner with the following command:

adb shell dpm set-device-owner com.xxxx.xxxx/.AdminReceiver

My manifest contains:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

The IMEI is accessed with the following lines of code:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

try
{
    return telephonyManager.getImei();
}
catch (SecurityException e)
{
    return null;
}

I'm still getting SecurityExceptions (with message: The user does not meet the requirements to access device identifiers.). What am I doing wrong?

2 Answers

READ_PHONE_STATE is a dangerous permission. For Android 6.0+, you need to request it at runtime in addition to having it the manifest. Alternatively — particularly for a one-off app installation — you could go into the system Settings app and manually grant the permission.

As far as I know, Android 10 restricts IMEI access to system apps only (but you can try with device owner rights). To get the IMEI, the app needs to declare the following permission:

<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />

See for details: https://developer.android.com/about/versions/10/privacy/changes

A workaround would be to target your app to Android 9 (API level 28) or lower.

You can get the example of the device owner app here (this is my open source MDM project). In that example, the app retrieves IMEI on Android 10 using this way and declaring android.permission.READ_PHONE_state only.

UPDATE - July 2020: On some builds of Android 10 (for example Samsung Galaxy A01), the IMEI is now not returned even if the app targets to API level 28.

Related