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:
Could anyone point me in the correct direction?
How to get an Android device's serial number, visible to the user in "Settings > About phone/tablet/device > Status > 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.
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.
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With