Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get Serial Number

Tags:

java

android

I am trying to get the device serial number programmatically. I have used the following line:

Build.SERIAL

Which returns something like :

95b9efad04ad28

However which I go to the settings on the device, I can see that it displays a different string:

enter image description here

Could anyone point me in the correct direction?

like image 768
James King Avatar asked Oct 12 '15 11:10

James King


People also ask

How do I find the serial number of my Android 10?

How to get an Android device's serial number, visible to the user in "Settings > About phone/tablet/device > Status > Serial number".

Does Android have serial number?

To find your device's serial number in the software, go to Settings > System. Then jump into About Phone > Status. Your device's serial number will generally be located toward the bottom of this screen.

How to get programmatically Android serial number?

This example demonstrate about How to get programmatically android serial number. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show serial number.

How to find the serial number of an Android device using Kotlin?

This example demonstrates how to find the serial number of an Android device using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.kt

How do I Find my Device’s serial number?

But if you have a device with a removable battery, you will often be able to find the serial number printed underneath. To find your device’s serial number in the software, go to Settings > System. Then jump into About Phone > Status. Your device’s serial number will generally be located toward the bottom of this screen.

Why can't I retrieve IMEI and serial number in Android 10?

In the interests of user privacy, Google has restricted APIs that retrieve non-resettable device identifiers such as the serial number and IMEI. This is documented in Google's Privacy changes in Android 10 guide.


1 Answers

There are several ways to get that number depending on the device's manufacturer and Android version :

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;
}

Taken from this gist.

like image 193
flawyte Avatar answered Sep 21 '22 22:09

flawyte