Device ID in GUID format

You can use Secure.Android_Id for getting unique id of the device. But if you need to pass it to as GUID like deviceId in iPhone, there doesn’t seem to be an easy way. The basic code I wrote for the conversion is something like this:

String android_id = getAndroidId(); long leastSignificantBits = 0; long mostSignificantBits = 0; UUID uuid = null;

    if (android_id != null) {
        String asciiConvertedText = "";
        int len = android_id.length();
        for (int i = 0; i < len; i++) {
            asciiConvertedText += "" + ((short) android_id.charAt(i));
        }

        len = asciiConvertedText.length();
        if (len > 16) {
            leastSignificantBits = Long.parseLong(asciiConvertedText.substring(0, 15));

            if (asciiConvertedText.length() > 31) {
                mostSignificantBits = Long.parseLong(asciiConvertedText.substring(16, 31));
            } else {
                mostSignificantBits = Long.parseLong(asciiConvertedText.substring(32,
                        asciiConvertedText.length()));
            }

            uuid = new UUID(mostSignificantBits, leastSignificantBits);
        } else if (len > 0) {

            leastSignificantBits = Long.parseLong(asciiConvertedText);
            uuid = new UUID(mostSignificantBits, leastSignificantBits);
        } else {
            uuid = UUID.randomUUID();
        }
    }

    if (uuid != null) {
        deviceUUID = uuid.toString();
    }
I posted this in stackoverflow and there was a good suggestion to use hashcodes. Keep in mind that hashcodes are guaranteed to return the same value only for only that JVM instance as per the java api docs. Android doc is not very clear about that.
 

Comments

Popular posts from this blog

Temples in Bangalore!

Blogspot.in?

Booked my first car!