-1

Does TelephonyManager. getDeviceId () returns different ID If user changes his mobile network in settings from GSM to CDMA , WCDMA (or vice versa from CDMA to GSM ) ?

Or does TelephonyManager. getDeviceId () returns an IMEI id when network settings in mobile is set to GSM and MEID or ESN id when settings changes to CDMA and so returned values from getDeviceId() may changes base on device settings and can not be used to uniquely identify devices !

ygngy
  • 3,630
  • 2
  • 18
  • 29

2 Answers2

0

Use this to get a unique ID on all devices. TelephonyManager fails on tablets and returns null, so you have to combine that result with something else, like I did.

public static String getDeviceID(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String tmDevice, tmSerial, androidId;

    tmDevice = "" + tm.getDeviceId();
    tmSerial = "" + tm.getSimSerialNumber();
    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return deviceUuid.toString();
}

Note you need to add this permission to AndroidManifest

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • As you see title is about phone devices thus tm.getDeviceId() should not returns null. I need to identify a device but if user changes his/her SIM card tm.getSimSerialNumber() changes. I think ANDROID_ID may changes (I am not sure) if user upgrades his android OS. – ygngy May 05 '15 at 07:52
0

I don't know why (specifically) you're using an unique id, but in my case I use it to identify a unique "app instalation". So it's not dependent of simCards, telephonymanager and etc.

What I did was create a unique id (when it has not been created yet) and stored it in sharedprefs:

private static String       uniqueID           = null;

public static String getDeviceUniqueID(Context context)
{
        if (uniqueID == null)
        {
            final String    PREF_UNIQUE_ID     = "PREF_UNIQUE_ID";
            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;        
} 

Caveats:

  • Of course it will be lost when user uninstalls the app, because sharedprefs are deleted too;
  • There's a chance of a unique id to be generated for 2 different users (but are trillions or more per one).

But, as I said above: "don't know why you're using it".

It works like a charm to me this way. Perhaps it'll work for you too. Cheers.

Christian
  • 7,062
  • 9
  • 53
  • 79
  • OK, I understand. In that case my solution is not suitable. – Christian May 05 '15 at 13:20
  • I think you don't need a uuid. If I was doing that, I'd rely on device's Google account. Does it matter if the user changes his device? What happen if user buys a new phone? Does he have to "re-buy" everything he has bought before? – Christian May 05 '15 at 13:32