Is there a way to get a unique device ID in android?

Discussion in 'Programming General' started by kmjt, Nov 21, 2014.

Is there a way to get a unique device ID in android?
  1. Unread #1 - Nov 21, 2014 at 5:40 AM
  2. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Is there a way to get a unique device ID in android?

    I want to be able to identify users without them registering for anything so I am creating a unique id for them if it is the first time they run the app. From what I looked online it doesn't look promising to get a truly unique device ID. I read some stuff about Froyo (2.2) botching the ANDROID_ID and some device manufactures screwing up where all ANDROID_ID are all the same. Well first off I don't even plan on letting the api go as low as 8 (I am going to set 10 minimum) so there goes that problem. I also read that in api 4.2+ where it allows profiles on a device, each profile is given a unique id. Is there anyway around this (assigning just 1 id to the whole device, not per profile)? This isn't really that big of a concern, although I would prefer not to allow it.

    The main priority is NEVER letting two users share the same device id. This would be very bad for the app I am making. It would be a plus if the unique device id remains the same if they were to uninstall and redownload my app, however I am not so much worried about this as opposed to users sharing a unique device id. If the unique device id for their device changes when they reinstall the app after deleting it, so be it, as long as it doesn't match any other device id in my database. As for the situation in which someone does a factory reset on their phone, I would prefer the device id to stay the same, although if this can't be done no big deal either.

    Here is what I have so far from what I found online:

    Code:
    final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
    
    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "" + tm.getSimSerialNumber();
    androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    
    UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
    String deviceId = deviceUuid.toString();
    

    Does anyone see an issue with this? Once again, I am only targeting api 2.4 and above.
     
  3. Unread #2 - Nov 23, 2014 at 9:45 PM
  4. 70i
    Joined:
    Jan 11, 2014
    Posts:
    462
    Referrals:
    0
    Sythe Gold:
    174

    70i Forum Addict
    Banned

    Is there a way to get a unique device ID in android?

    That seems good since serial numbers are never the same.
     
  5. Unread #3 - Nov 24, 2014 at 7:20 AM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Is there a way to get a unique device ID in android?


    Alright thank you :cool:
     
  7. Unread #4 - Nov 25, 2014 at 3:12 AM
  8. 70i
    Joined:
    Jan 11, 2014
    Posts:
    462
    Referrals:
    0
    Sythe Gold:
    174

    70i Forum Addict
    Banned

    Is there a way to get a unique device ID in android?

    I actually wrote something like this before by creating a random 10 digit string. But you code is better since there cant be duplicates.


    This is how you can make the code only run when the app is runs for the first time:
    SharedPreferences settings = getSharedPreferences("MyPrefs", Context.MODE_WORLD_READABLE);
    uniqueId=settings.getString("uniqueId","notSet");

    if (uniqueId.equals("notSet")) {
    ...}

     
< Where to start? | where to start programing >

Users viewing this thread
1 guest


 
 
Adblock breaks this site