There are several occasions when the unique identifier of a device is required. For instance to generate encryption keys or to track installations. On Android there are several ways to get such an ID. This tutorial examines various solution to associate unique identifier to a android device.
Using IMEI, MEID or ESN
To get the IMEI number of Android phone,
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); String szImei = TelephonyMgr.getDeviceId();
TelephonyManager.getDeviceId() is return (depending on the network technology) the IMEI, MEID, or ESN of the phone, which is unique to that piece of hardware. It requires READ_PHONE_STATE
permission, which is irritating if you don’t otherwise use or need telephony.
Wifi-only devices or music players which don’t have telephony hardware, above solution will not work for these kind of Android devices.
Using MAC Address
You can also try to get a MAC Address from a device having a Wi-Fi or Bluetooth hardware. This solution is will not work for devices which does not have Wi-Fi connection. Also, if the WiFi is not turned on, the hardware may not report the Mac address.
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); String mMAC = wm.getConnectionInfo().getMacAddress();
Using Secure Android ID
On a device first boot, a randomly value is generated and stored. It’s a 64-bit number that should remain constant for the lifetime of a device. It is available for smartphones and tablets. Below code shows how to retrieve the secure ID.
String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
Using UUID
Requirement for most of applications is to identify a particular installation and not a physical device, a good solution to get unique id is to use UUID. UUID.randomUUID() method generates an unique identifier for a specific installation.
public synchronized static String id(Context context) { static String uniqueID = null; static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID"; 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; }
Generate Pseudo Unique ID
You can generate the unique id using hardware and build details. Below example uses manufacturer name, CPU type, and other hardware details to compute unique ID.: it is possible to find two devices with the same ID (based on the same hardware and rom image) but the chances in real world applications are negligible. For this purpose you can use the Build class:
String mDevIDShort = "35" + Build.BOARD.length()%10+ Build.BRAND.length()%10 + Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + Build.DISPLAY.length()%10 + Build.HOST.length()%10 + Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + Build.TAGS.length()%10 + Build.TYPE.length()%10 + Build.USER.length()%10 ;