How to get enrollment-specific identifier of an android device?

660 Views Asked by At

How to get the enrollment-specific identifier of a device, which is guaranteed to be the same value for the same device, enrolled into the same organization by the same managing app.

How to use the getEnrollmentSpecificId () method (added in API level 31) in Kotlin?

public String getEnrollmentSpecificId ()
2

There are 2 best solutions below

0
Sweta Jain On
public String getEnrollmentSpecificId ()

It returns an enrollment-specific identifier of this device, which is guaranteed to be the same value for the same device, enrolled into the same organization by the same managing app. This identifier uniquely identifies individual devices within the same organization. The identifier would be consistent even if the work profile is removed and enrolled again (to the same organization), or the device is factory reset and re-enrolled.

Usage:

    fun getEnrollmentSpecificId(): String {
        val policy = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            try {
                policy.enrollmentSpecificId
            } catch (securityException: SecurityException) {
                print(securityException.message)
                ""
            }
        } else {
            ""
        }
    }
0
Oriol Villaret On

Here is the documentation about it

Only availabla form Android 12. And this can only be called by the Profile Owner or Device Owner, if the setOrganizationId(java.lang.String) was previously called

fun getEnrollmentSpecificId(): String {
    val policy = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        try {
            policy.setOrganizationId(ORGANIZATION_ID)
            return policy.enrollmentSpecificId
        } catch (securityException: SecurityException) {
        }
    }
    return ""
}