how to retrieve unique identifiers from android 11 r (API 30) device?

568 Views Asked by At

I want to get unique identifiers like Android Device ID, IMEI, or MAC address from an Android phone with Android version 11 using Java.
Can someone help? I need just either one unique identifier.
This is the code that I use to get IMEI:

public String getIMEI() {
        Context context = null;
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Log.i("Testing: ", telephonyManager.getImei());
        return telephonyManager.getImei();
}

And this is the error message that I got:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
1

There are 1 best solutions below

0
Kozmotronik On

As of the API 29, Google introduced new security policies to the TelephonyManager.getImei API. So it won't be available for 3rd party apps for security reasons.
Look at what is mentioned in the API's description:

Starting with API level 29, persistent device identifiers are guarded behind additional restrictions, and apps are recommended to use resettable identifiers (see Best practices for unique identifiers). This method can be invoked if one of the following requirements is met: