Build.getSerial()返回的序列号类型及安卓设备序列号匹配异常问题
Hey there, let's tackle your questions step by step:
Build.getSerial() return? Build.getSerial() returns the hardware serial number of the device — this is a unique string tied to the device's main hardware component (typically the motherboard).
A quick note: Starting from Android 10 (API level 29), accessing this method requires the READ_PHONE_STATE permission. Even with the permission, some devices might return a truncated value or a placeholder due to privacy restrictions introduced in later Android versions.
Build.getSerial() result and the serial number in settings/dialer (*#06#) This is a common headache with devices like OnePlus 7T and Samsung models. The root cause is that Build.getSerial() pulls the hardware serial number linked to the motherboard, while the serial number you see in settings or via *#06# is often the IMEI/MEID (for cellular-enabled devices) or a manufacturer-specific identifier used for warranty, inventory, and user-facing purposes.
You shared a reflection-based workaround that works for Samsung devices and OnePlus 6T but not the OnePlus 7T. Let’s break down this code and discuss why it behaves differently:
The Reflection Workaround Code
public static String getSerialNumber() { String serialNumber; try { Class<?> c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class); // Try GSM serial property first serialNumber = (String) get.invoke(c, "gsm.sn1"); if (serialNumber.equals("")) serialNumber = (String) get.invoke(c, "ril.serialnumber"); Log.e("return_ril.serialnumber","My number "+serialNumber); if (serialNumber.equals("")) serialNumber = (String) get.invoke(c, "ro.serialno"); Log.e("return_ro.serialno","My number "+serialNumber); if (serialNumber.equals("")) serialNumber = (String) get.invoke(c, "sys.serialnumber"); Log.e("return_sys.serialnumber","My number "+serialNumber); // Fallback to Build.getSerial() if prior methods fail if (serialNumber.equals("")) serialNumber = Build.getSerial(); Log.e("return_Build.getSerial","My number "+serialNumber); if (serialNumber.equals("")) serialNumber = Build.SERIAL; Log.e("return_Build.SERIAL","My number "+serialNumber); // Set to null if all methods return empty if (serialNumber.equals("")) serialNumber = null; } catch (Exception e) { e.printStackTrace(); serialNumber = null; } Log.e("return_serialNumber","My number "+serialNumber); return serialNumber; }
How This Code Works
This method iterates through multiple system properties because different manufacturers store the user-facing serial number in different places:
gsm.sn1/ril.serialnumber: These properties often hold cellular-related identifiers (which align with what*#06#shows on Samsung devices)ro.serialno: This is usually the same hardware serial number returned byBuild.getSerial()sys.serialnumber: Another manufacturer-specific property for serial number storage
Why It Fails on OnePlus 7T
OnePlus likely changed the system property where they store the user-facing serial number on the 7T model. To fix this, you can first identify the correct property using ADB:
adb shell getprop | grep serial
This command will list all serial-related properties on the OnePlus 7T. Once you find the property that matches the serial number shown in settings, add it to your fallback chain in the reflection code.
内容的提问来源于stack exchange,提问作者nitinkumarveridic

