How to get Unique ID to identify Android devices Android 13.06.2017

How to get Unique ID to identify Android devices

Sometimes it is required to get the unique ID of android device (phone, tablet, tv, wear) while developing the android application. There are several solutions and we going to examine them all.

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.

Be aware that ANDROID_ID could be changed after user switch (multiuser in 4.2).

But, accordingly to googleblog.com ANDROID_ID is the preferred device identifier. ANDROID_ID is perfectly reliable on versions of Android <=2.1 or >=2.3. Only 2.2 has the problems.

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.

String uniqueID = UUID.randomUUID().toString(); 

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.

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:

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();

Advantages of using IMEI as Device ID:

  • The IMEI is unique for each and every device.
  • It remains unique for the device even if the application is re-installed or if the device is rooted or factory reset.

Disadvantages of using IMEI as Device ID:

  • IMEI is dependent on the simcard slot of the device, so it is not possible to get the IMEI for the devices that do not use Simcard.
  • In Dual sim devices, we get 2 different IMEIs for the same device as it has 2 slots for simcard.
  • 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.

MAC and Bluetooth 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.

But it's better to avoid MAC address because these are no more accessible to application in Android v6.0 MarshMallow: To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.