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:
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:
Disadvantages of using IMEI as Device ID:
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.