JStephen
JStephen

Reputation: 1149

how reliable is android Build.SERIAL

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.

Upvotes: 2

Views: 1725

Answers (1)

Mickäel A.
Mickäel A.

Reputation: 9397

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.

Upvotes: 1

Related Questions