Android O device serial number from native

懵懂的女人 提交于 2019-12-10 22:53:33

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/45372687/android-o-device-serial-number-from-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!