0

We are using the serial number for unique device identification. In android Q we can't access that device information. so we went for Unique id, But it changes while uninstalling and installing the app. Need an idea for that. Thanks in advance

2 Answers2

3

I faced the same problem of uniquely identifying a device after uninstalling the app. The problem with android.provider.Settings.Secure.ANDROID_ID is that it keeps changing after app uninstall and device reset.

The only solution that worked for me is MediaDrm. It gives back the device unique id that which remains the same on app uninstall and even on device reset. But there is one thing it gives different id for different app package names. I came to know about MediaDrm from this question.

After using MediaDrm I came to know about a few things. There are few devices that return the same id for the same app. For example, in my case, There are few Vivo devices that give the same id when using MediaDrm. So you have to deal with it.

This is the code to get that unique id using MediaDrm.

@Nullable
public static String getUUId() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return null;
    }
    
    UUID wideVineUuid = new UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L);
    MediaDrm wvDrm = null;
    try {
        wvDrm = new MediaDrm(wideVineUuid);
        byte[] wideVineId = wvDrm.getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID);
        //optional encoding to convert the array in string.
        return Base64.encodeToString(wideVineId, Base64.NO_WRAP);

    } catch (Exception e) {
        e.printStackTrace();
        return null;

    } finally {
        if (wvDrm != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                wvDrm.close();
            } else {
                wvDrm.release();
            }
        }
    }
}
Ali Asjad
  • 1,489
  • 1
  • 10
  • 11
1

The official documentation says accessing Device Based informacion (Serial, Imei, DeviceId, Meid, SimSerialNumber SubscriberId, etc) it's restricted by default.

In order to access that kind of information, the official docs says you should set your app as a Device or Profile Owner App, have Special Carrier Permissions, or use the READ_PRIVILEGED_PHONE_STATE, which requires a special kind of signature. You can read more Here.

As a workaround, i recommend trying to generate your unique id based on other information you may be able to get from device (bluetooth address, wifi mac address, etc). These are not 100% stable as you can change them with some programming, but for most users it will work fine.

Ivan Verges
  • 595
  • 3
  • 10
  • 25