9

How do I get an unique ID from an Android phone?

Whenever I try to get the unique ID from the phone as a string it always shows android id and no other unique hex values.

How do I get that one?

This is the code I use to get the ID until now:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);

the output which I get looks like this:

Android id is: android id

I am using a Nexus One for testing.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
jaimin
  • 111
  • 1
  • 1
  • 4
  • possible duplicate of [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id) – Joachim Sauer Oct 11 '11 at 12:01
  • See http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id – MatejC Aug 16 '10 at 09:10
  • Best way to get the Android phones id is stated [here][1] [1]: http://stackoverflow.com/questions/4468248/unique-id-of-android-device/23196094#23196094 – Zar E Ahmer Apr 21 '14 at 11:15
  • Use [this library](https://github.com/delight-im/Android-BaseLib) for a unique ID per device with [Identity.getDeviceId(context)](https://github.com/delight-im/Android-BaseLib/blob/master/Source/src/im/delight/android/baselib/Identity.java) that always works. – caw Mar 08 '15 at 22:35

6 Answers6

17

For detailed instructions on how to get a Unique Identifier for each Android device your application is installed from, see this official Android Developers Blog posting:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

It seems the best way is for you to generate one your self upon installation and subsequently read it when the application is re-launched.

I personally find this acceptable but not ideal. No one identifier provided by Android works in all instances as most are dependent on the phone's radio states (wifi on/off, cellular on/off, bluetooth on/off). The others like Settings.Secure.ANDROID_ID must be implemented by the manufacturer and are not guaranteed to be unique.

The following is an example of writing data to an INSTALLATION file that would be stored along with any other data the application saves locally.

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
4
((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();

with manifest

<uses-permission android:name='android.permission.READ_PHONE_STATE' />

Edit:

Here is some interesting reading about the android id:

How to set the Android ID

Android ID Requires Market Login

Try setting it to something other than 'android id' and see if you read the new value.

drawnonward
  • 53,459
  • 16
  • 107
  • 112
  • 1
    Thanks for your reply but i want 32 bit unique id in getDeviceID() i only get 15 character so how can i get 32 bit HMAC MD5 id of android device ? – jaimin Jun 25 '10 at 06:21
  • 2
    Be aware there are huge limitations with this solution: http://android-developers.blogspot.com/2011/03/identifying-app-installations.html – emmby Apr 07 '11 at 20:08
  • 1
    What if this is run on a tablet without a phone? – Chloe Apr 07 '12 at 19:57
  • It returns null value on Nexus 7 tablet – Aamirkhan Feb 20 '14 at 13:51
2

Here is code segment how to get androidId, unique DeviceId and Serial Number for your android phone may be it helps you.

TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
           final String DeviceId, SerialNum, androidId;
            DeviceId = tm.getDeviceId();
            SerialNum = tm.getSimSerialNumber();
            androidId = Secure.getString(getContentResolver(),Secure.ANDROID_ID);

            UUID deviceUuid = new UUID(androidId.hashCode(), ((long)DeviceId.hashCode() << 32) | SerialNum.hashCode());
            String mydeviceId = deviceUuid.toString();
            Log.v("My Id", "Android DeviceId is: " +DeviceId); 
            Log.v("My Id", "Android SerialNum is: " +SerialNum); 
            Log.v("My Id", "Android androidId is: " +androidId);  
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
naeemgik
  • 2,232
  • 4
  • 25
  • 47
1
Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);

this is not a good method it will return null in some cases.

THis piece of code will help you to generate unique pseudodevice ID .......``

    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;

}
Sreedev
  • 6,563
  • 5
  • 43
  • 66
  • 1
    This can be unique but it is not safe to use because if the user did a factory reset, the value of Secure.ANDREID_ID (could be) changed so the method will return a different unique ID compared with the one returned before factory reset. – Ungureanu Liviu Oct 02 '12 at 10:49
1

Wireless MAC address is more unique than IMEI, because the later gets spoofed on stolen devices. Drawback is that it only works on WiFi enabled devices. WifiInfo

ognian
  • 11,451
  • 4
  • 35
  • 33
  • Be aware there are huge limitations with this solution: http://android-developers.blogspot.com/2011/03/identifying-app-installations.html – emmby Apr 07 '11 at 20:06
  • Many phones I've tested (30%) return `null` for the Mac address in any situation. *Some* phones can only report it when Wifi is on. – JoJo Mar 09 '12 at 18:17
0

Unique Id

Info adInfo = null;

 

try {

     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);

} catch (IOException e) {

     ...

} catch (GooglePlayServicesAvailabilityException e) {

     ...

} catch (GooglePlayServicesNotAvailableException e) {

     ...

}

 

String AdId = adInfo.getId();

 

Fahim
  • 12,198
  • 5
  • 39
  • 57