Android get Serial Number
Asked Answered
L

6

7

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?

Leodora answered 12/10, 2015 at 11:15 Comment(2)
I used Lenovo S660 running on 4.4.2 and it returns correct value. What device or version you are using?Learned
Possible duplicate of How to find serial number of Android device?Disregard
J
7

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.

Jaquelin answered 2/10, 2018 at 10:38 Comment(1)
Not working for Android 10, Android 10 add restrictions developer.android.com/about/versions/10/privacy/…Vietnam
L
4

This worked for me:

String serialNumber;

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

    serialNumber = (String) get.invoke(c, "sys.serialnumber", "error");
    if (serialNumber.equals("error")) {
        serialNumber = (String) get.invoke(c, "ril.serialnumber", "error");
    }
} catch (Exception e) {
    e.printStackTrace();
}
Leodora answered 12/10, 2015 at 11:36 Comment(0)
P
0

this is not your answer?:

TelephonyManager tManager = (TelephonyManager)myActivity.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();

I found it in here

Phonograph answered 12/10, 2015 at 11:24 Comment(1)
Sadly not. I tried this, but its returning null. This is a tablet, so not sure if that makes a differenceLeodora
C
0

I am trying to get the device serial number programmatically

This feature is broken on Android for ages. Some devices return null, some return the same ID for all the devices etc. If your app is using Google Play Services, use InstanceId instead, or see this, but ancient now, but still valid, blog post.

Cabaret answered 12/10, 2015 at 11:32 Comment(0)
F
0

Since the Android ID is broken on many devices and Android builds alike I am using a mix of the Android ID and the devices WIFI-MAC address, which is unique per device. Both are used to create a UUID, which is always unique, no matter if the Android ID is null or not.

Try something along the lines of this:

final WifiManager wm = (WifiManager) myAct.getSystemService(Context.WIFI_SERVICE);
String macAddr = null;
if (wm != null)
    macAddr = wm.getConnectionInfo().getMacAddress();
String androidId = Secure.getString(myAct.getContentResolver(), Secure.ANDROID_ID);

if (androidId == null)
    androidId = "0000000000000000";

if (macAddr == null || macAddr.contains("\\s+") || !macAddr.contains(":"))
    macAddr = "00:00:00:00:00:00";

UUID deviceUuid = new UUID(macAddr.hashCode(), androidId.hashCode());
String deviceId = deviceUuid.toString();
Faucet answered 12/10, 2015 at 11:44 Comment(0)
H
0

I know this question is several years old now, but I wanted to post an updated version, especially for those who may be using Xamarin. This is a solution for C# Xamarin Android, based on the solution posted above by user flawyte.

Here is the code:

public static string GetDeviceSerialCode ( )
{
    string serial_number = string.Empty;

    try
    {
        var c = Java.Lang.Class.ForName("android.os.SystemProperties");
        var get = c.GetMethod("get", Java.Lang.Class.FromType(typeof(Java.Lang.String)));

        serial_number = (string)get.Invoke(c, "gsm.sn1");
        if (string.IsNullOrEmpty(serial_number))
        {
            serial_number = (string)get.Invoke(c, "ril.serialnumber");
        }
        if (string.IsNullOrEmpty(serial_number))
        {
            serial_number = (string)get.Invoke(c, "ro.serialno");
        }
        if (string.IsNullOrEmpty(serial_number))
        {
            serial_number = (string)get.Invoke(c, "sys.serialnumber");
        }
        if (string.IsNullOrEmpty(serial_number))
        {
            serial_number = Build.GetSerial();
        }
    }
    catch (Exception e)
    {
        serial_number = string.Empty;
    }

    return serial_number;
}
Hygrometry answered 6/5, 2020 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.