2

I have an app that checks the value returned by Build.SERIAL, and then uses that as a sort of validation when transferring data between the app and the website. Everything is working fine except for occasionally a user is unable to connect. In the docs it says Build.SERIAL returns the serial id if available, My question is does this mean that some devices will have it and some won't, or does it mean that a device may be able to retrieve it at some times, and others it won't?

I was assuming its the first, but I'm have users that are having an issue with it and I cannot repeat it. And when I tried searching for more information on what exactly "If available" meant I couldn't really find anything.

EDIT Just for more clarification, I have a user that has a serial id and can use the software just fine. The problem is occasionally he gets an error saying the serial id is not registered, I have not been able to recreate the problem so I am only guessing that the device sometimes does not grab the serial id. I was wondering if anyone could confirm that this is, or is not, an issue. I have already put some things in place to catch the error if it happens again, but it seems to be randomly happening.

EDIT 2 So i found out that the Build.serial is still returning the serial id so that is not the problem, ill eventually figure it out, but for now I'll just leave this question up until someone can explain when it is or isn't available in case it helps someone in the future.

JStephen
  • 1,059
  • 3
  • 13
  • 28
  • What are you trying to use it for? Just a unique id for the device? – Gabe Sechan Jul 10 '14 at 20:01
  • Basically, I also have a login but if a user is using two different tablets, or two users using the same tablet we needed a way to distinguish, so I went with serial id. – JStephen Jul 10 '14 at 20:04
  • 1
    The typical way to do it is the ANDROID_ID. Also check out this question: http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id – Gabe Sechan Jul 10 '14 at 20:08
  • the comments in that question say that it is known to be null, which possibly puts me in the same boat. I do not know for certain that the Build.SERIAL is returning null since i cannot repeat the problem, I can only guess, which is why I'm asking for clarification. – JStephen Jul 10 '14 at 20:14
  • The usual way, AFAIK, is to use `ANDROID_ID`, and if it's null (or a known buggy ID which shipped on all the devices of a particular manufacturer/model) then create a new UUID and store it in user preferences, then return it afterwards. I don't think a "perfect" solution exists. – matiash Jul 10 '14 at 20:18
  • This isn't strictly an answer to your question, but I would use Java's UUID generator: http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html the first time the app is launched. This will give you a unique ID for each instance of the application, similarly what you're trying to use. – Chris Thompson Jul 10 '14 at 20:27

1 Answers1

1

Build.SERIAL can be empty or sometimes return a different value (proof 1, proof 2) than what you can see in your device's settings.

There are several ways to get that number depending on the device's manufacturer and Android version, so I decided to compile every possible solution I could found in a single gist. Here's a simplified version of it :

public static String getSerialNumber() {
    String serialNumber;

    try {
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class);

        serialNumber = (String) get.invoke(c, "gsm.sn1");
        if (serialNumber.equals(""))
            serialNumber = (String) get.invoke(c, "ril.serialnumber");
        if (serialNumber.equals(""))
            serialNumber = (String) get.invoke(c, "ro.serialno");
        if (serialNumber.equals(""))
            serialNumber = (String) get.invoke(c, "sys.serialnumber");
        if (serialNumber.equals(""))
            serialNumber = Build.SERIAL;

        // If none of the methods above worked
        if (serialNumber.equals(""))
            serialNumber = null;
    } catch (Exception e) {
        e.printStackTrace();
        serialNumber = null;
    }

    return serialNumber;
}

I try to update the gist regularly whenever I can test on a new device or Android version. Contributions are welcome too.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71