How to get Serial number of Android device programmatically
12,241
Solution 1
Use this upto Android 7.1(SDK 25)
Build.SERIAL
And this for Android 8(SDK 26+)
Build.getSerial()
Solution 2
You can use this gist file for get device id. It works on all device before android Pie. If you targeting pie so you can add below code on gist file.
if (serialNumber.equals("unknown")){
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serialNumber = Build.getSerial();
}
} catch (SecurityException e) {
e.printStackTrace();
Log.d("responseData", String.valueOf(e.getMessage()));
}
}
put this code above this line of code
// If none of the methods above worked
if (serialNumber.equals("")) {
serialNumber = null;
}
I tried this code on Nokia 6.1 plus and its working.
Author by