Unique ID of Android device
Sorry to bump an old thread but this problem gives me headache, I found a good article for someone to read, and this really helps me a lot.
Sometimes it is required during Android application development to get the unique id of the Android based smartphone device. This is necessary in cases when the user wants to track the unique device installations of the application.
This is also useful in cases where the Android developer wants to send Push messages to only few specific devices. So over here it becomes necessary to have a UDID for every device.
In Android there are many alternatives to UDID of the device. Some of the methods to get the UDID in android application are listed below with its advantages and disadvantages and any necessary permissions for getting the device ID.
- The IMEI: (International Mobile Equipment Identity)
- The Android ID
- The WLAN MAC Address string
- The Bluetooth Address string
1) IMEI: (International Mobile Equipment Identity)
The IMEI Number is a very good and primary source to get the device ID. It is unique for each and every device and is dependent on the device Hardware. So it is also unique for each and every device and it is permanent till the lifetime of the device.
The code snippet to get the Device IMEI is as below,
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String m_deviceId = TelephonyMgr.getDeviceId();
For this your application will require the permission “android.permission.READ_PHONE_STATE” given in the manifest file.
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.
2) The Android ID
The Android_ID is a unique 64 bit number that is generated and stored when the device is first booted. The Android_ID is wiped out when the device is Factory reset and new one gets generated.
The code to get the Android_ID is shown below,
String m_androidId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
Advantages of using Android_ID as Device ID:
It is unique identifier for all type of devices (smart phones and tablets). No need of any permission.
It will remain unique in all the devices and it works on phones without Simcard slot.
Disadvantages of using Android_ID as Device ID:
If Android OS version is upgraded by the user then this may get changed. The ID gets changed if device is rooted or factory reset is done on the device. Also there is a known problem with a Chinese manufacturer of android device that some devices have same Android_ID.
3) The WLAN MAC Address string
We can get the Unique ID for android phones using the WLAN MAC address also. The MAC address is unique for all devices and it works for all kinds of devices.
The code snippet to get the WLAN MAC address for a device is as shown below,
WifiManager m_wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String m_wlanMacAdd = m_wm.getConnectionInfo().getMacAddress();
Your application will require the permission “android.permission.ACCESS_WIFI_STATE” given in the manifest file.
Advantages of using WLAN MAC address as Device ID:
It is unique identifier for all type of devices (smart phones and tablets). It remains unique if the application is reinstalled.
Disadvantages of using WLAN MAC address as Device ID:
If device doesn’t have wifi hardware then you get null MAC address, but generally it is seen that most of the Android devices have wifi hardware and there are hardly few devices in the market with no wifi hardware.
4) The Bluetooth Address string
We can get the Unique ID for android phones using the Bluetooth device also. The Bluetooth device address is unique for each device having Bluetooth hardware.
The code snippet to get the Bluetooth device address is as given below,
BluetoothAdapter m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_bluetoothAdd = m_BluetoothAdapter.getAddress();
To get the above code, your application needs the permission “android.permission.BLUETOOTH” given in the manifest file.
Advantages of using Bluetooth device address as Device ID: It is unique identifier for all type of devices (smart phones and tablets). There is generally a single Bluetooth hardware in all devices and it doesn’t gets changed.
Disadvantages of using Bluetooth device address as Device ID: If device hasn’t bluetooth hardware then you get null.
As per me these are few of the best possible ways to get the Unique Device ID for Android smartphone device and their pros and cons of using it. Now it is upto you to decide which method to use based on the Android application development requirements.
If there are any other methods to get UDID and that covers up the disadvantages of above methods, then I would love to explore those in my Android application. Pl. share those in comment box and also if any suggestions or queries.
Here's the article
There are three types of identifier on android phone.
- IMEI
IMSI
String ts = Context.TELEPHONY_SERVICE; TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(ts); String imsi = mTelephonyMgr.getSubscriberId(); String imei = mTelephonyMgr.getDeviceId();
Android ID It is a 64-bit hex string which is generated on the device's first boot. Generally it won't be changed unless is factory reset.
Secure.getString(getContentResolver(), Secure.ANDROID_ID);
Secure.getString(getContentResolver(), Secure.ANDROID_ID);
This will not work for all the devices.
Some of the android devices has a problem Some devices returns null when we try to get the Device ID.The only way to solve this issue is to make a pseudodeviceID which should be generated by ourself.This function will generation a unique device ID for you.You can make changes to this function as you needed.Me too struggled a lot for solving this issue.
public String getDeviceID() {
/*String Return_DeviceID = USERNAME_and_PASSWORD.getString(DeviceID_key,"Guest");
return Return_DeviceID;*/
TelephonyManager TelephonyMgr = (TelephonyManager) getApplicationContext().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String m_szImei = TelephonyMgr.getDeviceId(); // Requires
// READ_PHONE_STATE
// 2 compute DEVICE ID
String m_szDevIDShort = "35"
+ // we make this look like a valid IMEI
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; // 13 digits
// 3 android ID - unreliable
String m_szAndroidID = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
// 4 wifi manager, read MAC address - requires
// android.permission.ACCESS_WIFI_STATE or comes as null
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
// 5 Bluetooth MAC address android.permission.BLUETOOTH required
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_szBTMAC = m_BluetoothAdapter.getAddress();
System.out.println("m_szBTMAC "+m_szBTMAC);
// 6 SUM THE IDs
String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;
System.out.println("m_szLongID "+m_szLongID);
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
byte p_md5Data[] = m.digest();
String m_szUniqueID = new String();
for (int i = 0; i < p_md5Data.length; i++) {
int b = (0xFF & p_md5Data[i]);
// if it is a single digit, make sure it have 0 in front (proper
// padding)
if (b <= 0xF)
m_szUniqueID += "0";
// add number to string
m_szUniqueID += Integer.toHexString(b);
}
m_szUniqueID = m_szUniqueID.toUpperCase();
Log.i("-------------DeviceID------------", m_szUniqueID);
Log.d("DeviceIdCheck", "DeviceId that generated MPreferenceActivity:"+m_szUniqueID);
return m_szUniqueID;
}