5

What is the proper way to get the serial number from native on Android O without calling the Java Build.getSerial().

On Android < 26 versions from native we could get the device serial number using the following code:

string serial = read_property("ro.boot.serialno");

...

string read_property(const string& property_name) {
  char propertyValue[PROP_VALUE_MAX];
  int propertyLen = __system_property_get(property_name.c_str(), propertyValue);
  ...
}

On Android O this throws an error:

Access denied finding property "ro.boot.serialno"

Although the READ_PHONE_STATE permission is granted. Seems to be related to the deprecated Build.SERIAL in Android 26.

I managed to get this property using adb, so the value is not removed and is there:

adb shell getprop ro.boot.serialno
pirags
  • 125
  • 1
  • 12
  • 1
    "Access denied" says it all, the build serial is on Android O not available to apps anymore (the adb shell is not an app). Even if it was accessible in some way, you should not use it, since the access was denied to protect the privacy of Android users. If you find a loophole, Google will likely close it later one, so better find an alternative way to do what you want to do. – M66B Jul 28 '17 at 16:52

1 Answers1

0

You can use Build.getSerial() to get the serial number.

If the user has granted the READ_PHONE_STATE permission then there is no problem but if the user revoked the permission or has never granted it - you should take into consideration that you will get a SecurityException at runtime when trying to retrieve it.

In general - it looks like Google is slowly pushing to prevent apps from using personal identifiable information (like serial numbers of device) over the last few years so and you should look into alternatives like installationId.

FunkSoulBrother
  • 2,057
  • 18
  • 27