3

I want to generate a uuid for my app i tried many things like wifi mac address , getting android id, serial number , creating pseudo uuid from device android.os.Build properties .

i also came to know that java itself got Uuid creator class java.util.UUID

by going through several articles and blogs i am little confused about this . i want to know that if two android device are creating uuid using this java class(java.util.UUID) will that be unique or is there any chance for duplication?

Also in some devices i was not able to find serial number using android.os.Build.SERIAL . is there alternative way to get serial number of device

edwin
  • 7,985
  • 10
  • 51
  • 82

2 Answers2

9

Assuming you're using the java.util.UUID's randomUUID() function there's a theoretical chance of duplication, but it's incredibly remote. The ids generated are RFC4122 version-4 ids, which have 122 bits randomly set. That means there are 5.32 x 10^^36 possible values. For some perspective on that, if you had a billion devices, each generating a billion IDs per second, it would take roughly 168 billion years for them to finish (~10X the age of the universe).

So, yes, duplication is possible, but (assuming Java is using a high quality random number generator), the odds of it actually happening are so remote as to be meaningless.

Community
  • 1
  • 1
broofa
  • 37,461
  • 11
  • 73
  • 73
  • to be more clear you mean that the chance of duplication is negligible in real world aspect, am i right? – edwin Dec 19 '12 at 06:07
  • 1
    Yes. See http://stackoverflow.com/questions/2513573/how-good-is-javas-uuid-randomuuid – broofa Dec 19 '12 at 07:17
1

to Get Device ID Use Telephoney U can use it here is it is

public static String deviceUDID(Context ctx) {
     final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

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

     UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
     String deviceId = deviceUuid.toString();
     Log.d("Device Id", deviceId);
     return deviceId;
} 
edwin
  • 7,985
  • 10
  • 51
  • 82
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • is this device id an device serial number are same? Will this code work on tablets? (unfortunately i don't have tablet device for testing) – edwin Dec 19 '12 at 06:05
  • 1
    no both are different thing both are Added up to resolve the Conflict (if there occur any) in the UUID and yes it will work on Tablet not yet tested but its a generic you can add many other parameters other than this "getSimSerialNumber()" there are Plenty more features – Usman Kurd Dec 19 '12 at 06:15
  • but "getSimSerialNumber()" will return null in the case of CDMA device (i read in may articles that some tablet devices also give null for this methode) – edwin Dec 19 '12 at 06:19
  • have you ever checked this methode "android.os.Build" ? will there any assurance that this methode will always return value (in some device i got "unknown" as result)? – edwin Dec 19 '12 at 06:22