Android UDID like IPhone?

28,718

Solution 1

From the docs:

getDeviceId()

Returns the unique device ID, for example, the IMEI for GSM and the MEID for CDMA phones. Return null if device ID is not available.

Solution 2

It's very easy to get the Android UDID - check out the following code:

public class DemoActivityActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    Log.d(">>>>", "Android ID: " + Secure.getString(getContentResolver(), Secure.ANDROID_ID));
    Log.d(">>>>", "Device ID : " + tm.getDeviceId());

}

For getting the Device ID you have to set following permission in AndroidManifest.xml:

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

For getting the Android ID you don't need to set any permission.

Solution 3

The Device ID used to only be available if you had signed up for Market by associating your phone with your Google account when you start, i.e. not available on the emulator. This seems to have changed with Android 2.2, where one is generated for the emulator as well. I don't believe it is associated with IMEI, ICC or any other phone-related token, but is rather a pseudo-unique token generated by Google web services to identify your phone.

Solution 4

I implemented a class to get IMEI / Wifi MAC address / deviceID, hope it useful for you ^^

public class DeviceInfo {

protected static String imeiNumber;
protected static String wifiMacAddress;
protected static String deviceID;

// This method must be called before other method
public static void init(Context context) throws Exception {
    imeiNumber = getImei(context);
    wifiMacAddress = getWifiMacAddress(context);
    deviceID = getDeviceId(context);
}

public static String getDeviceInfo() {
    return deviceID;
}

public static String getImei() {
    return imeiNumber;
}

public static String getWifiMacAddress() {
    return wifiMacAddress;
}

public static String getModel() {
    return Build.MODEL;
}

public static String getOsVersion() {
    return Build.VERSION.RELEASE;
}

protected static String getDeviceId(Context context) throws Exception {
    String imei = getImei(context);
    if (imei != null) return imei;
    String tid = getWifiMacAddress(context);
    return tid;
}

protected static String getWifiMacAddress(Context context) throws Exception {
    WifiManager manager = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = manager.getConnectionInfo();
    if (wifiInfo == null || wifiInfo.getMacAddress() == null)
        return md5(UUID.randomUUID().toString());
    else return wifiInfo.getMacAddress().replace(":", "").replace(".", "");
}

protected static String getImei(Context context) {
    TelephonyManager m = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    String imei = m != null ? m.getDeviceId() : null;
    return imei;
}

protected static String md5(String s) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");

    md.update(s.getBytes());

    byte digest[] = md.digest();
    StringBuffer result = new StringBuffer();

    for (int i = 0; i < digest.length; i++) {
        result.append(Integer.toHexString(0xFF & digest[i]));
    }
    return (result.toString());
}
}
Share:
28,718
Chris
Author by

Chris

Updated on August 06, 2022

Comments

  • Chris
    Chris over 1 year

    Does Android have a UDID like IPhone? If yes, is there a way I can get it programatically?

    Thanks Chris

  • user1154390
    user1154390 about 10 years
    add following permissions with above code. <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>