How to retrieve an Unique ID to identify Android devices ?

Sylvain Saurel

Some developers can need an Unique ID to identify Android devices. Use cases where developers need this Unique ID are multiples.

For example, when you want to :

  • identify an Android user to store games scores on a server
  • track apps installation
  • generate DRM for copy protection.

Several solutions exist but none is perfect. In this tutorial, we’re going to examine five solutions and presenting their disadvantages :

1. Unique Telephony Number (IMEI, MEID, ESN, IMSI)

If you only target smartphones, you can take profit of the fact that the device have telephony services.So, you can easily retrieve an unique ID identifying the device.

This unique ID can be IMEI, MEID, ESN or IMSI. They can be defined as follows :

  • IMEI for International Mobile Equipment Identity : the Unique Number to identify GSM, WCDMA mobile phones as well as some satellite phones
  • MEID for Mobile Equipment IDentifier : the globally unique number identifying a physical piece of CDMA mobile station equipment, the MEID was created to replace ESNs (Electronic Serial Number)
  • ESN for Electronic Serial Number : the unique number to identify CDMA mobile phones
  • IMSI (International Mobile Subscriber Identity) : the unique identification associated with all GSM and UMTS network mobile phone users

To retrieve the unique ID associated to your device, you can use the following code :

import android.telephony.TelephonyManager;
import android.content.Context;
// ...TelephonyManager telephonyManager;telephonyManager = (TelephonyManager) getSystemService(Context.
TELEPHONY_SERVICE);
/*
* getDeviceId() returns the unique device ID.
* For example,the IMEI for GSM and the MEID or ESN for CDMA phones.
*/
String deviceId = telephonyManager.getDeviceId();
/*
* getSubscriberId() returns the unique subscriber ID,
* For example, the IMSI for a GSM phone.
*/
String subscriberId = telephonyManager.getSubscriberId();

This solution needs to request for android.permission.READ_PHONE_STATE to your user which can be hard to justify following the type of application you have made.

Furthermore, this solution is limited to smartphones because tablets don’t have telephony services. One advantage is that the value survives to factory resets on devices.

2. MAC Address

You can also try to get a MAC Address from a device having a Wi-Fi or Bluetooth hardware. But, this solution is not recommended because not all of the device have Wi-Fi connection. Even if the user have a Wi-Fi connection, it must be turned on to retrieve the data. Otherwise, the call doesn’t report the MAC Address.

3. Serial Number

Devices without telephony services like tablets must report a unique device ID that is available via android.os.Build.SERIAL since Android 2.3 Gingerbread. Some phones having telephony services can also define a serial number. Like not all Android devices have a Serial Number, this solution is not reliable.

4. Secure Android ID

On a device first boot, a randomly value is generated and stored. This value is available via Settings.Secure.ANDROID_ID . It’s a 64-bit number that should remain constant for the lifetime of a device. ANDROID_ID seems a good choice for a unique device identifier because it’s available for smartphones and tablets. To retrieve the value, you can use the following code :

String androidId = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);

However, the value may change if a factory reset is performed on the device. There is also a known bug with a popular handset from a manufacturer where every instance have the same ANDROID_ID. Clearly, the solution is not 100% reliable.

5. Use UUID

As the requirement for most of applications is to identify a particular installation and not a physical device, a good solution to get unique id for an user if to use UUID class. The following solution has been presented by Reto Meier from Google in a Google I/O presentation :

private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
public synchronized static String id(Context context) {
if (uniqueID == null) {
SharedPreferences sharedPrefs = context.getSharedPreferences(
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
if (uniqueID == null) {
uniqueID = UUID.randomUUID().toString();
Editor editor = sharedPrefs.edit();
editor.putString(PREF_UNIQUE_ID, uniqueID);
editor.commit();
}
}
return uniqueID;
}

UUID.randomUUID() method generates an unique identifier for a specific installation. You have just to store that value and your user will be identified at the next launch of your application. You can also try to associate this solution with Android Backup service to keep the information available for the user even if he installs your application on an other device.

Conclusion

Identify a particular device on Android is not an easy thing. There are many good reasons to avoid that. Best solution is probably to identify a particular installation by using UUID solution. However, if you want absolutely identify a particular device physically, you can try to use the ANDROID_ID solution. Not 100% reliable but better than other solutions.


Sylvain Saurel

Written by

Entrepreneur / Developer / Blogger / Author. Tout sur le Bitcoin : https://www.toutsurlebitcoin.fr. Je Deviens Meilleur : https://www.jedeviensmeilleur.fr

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade