Solve the problem that Android 11 cannot get the Serial number method

Due to the adoption of the sandbox storage mode and the update of the permission policy from Android 10, various problems cannot be obtained during the adaptation process. Based on this analysis, the method of not getting the serial number is as follows:

1. Adaptation of R

1. The method to obtain SN from 8.0 is the following code:

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                serialNumber = Build.getSerial();
                LOGE.E("serialNumber + " + serialNumber);
            }

However, this method still does not work on 11.

2. Click on serialNumber = Build.getSerial(); Build in the method to view the source code, you need to apply for the following permissions,

android.permission.READ_PHONE_STATE

After applying for permission, it still cannot be obtained. . . .

3. Next, look at the system source code, first check frameworks/base/core/java/android/os/Build.java, where the code has the following definitions. The most important thing is the definition of IDeviceIdentifiersPolicyService for its acquisition.

    @SuppressAutoDoc // No support for device / profile owner.
    @RequiresPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
    public static String getSerial() {
        IDeviceIdentifiersPolicyService service = IDeviceIdentifiersPolicyService.Stub
                .asInterface(ServiceManager.getService(Context.DEVICE_IDENTIFIERS_SERVICE));
        try {
            Application application = ActivityThread.currentApplication();
            String callingPackage = application != null ? application.getPackageName() : null;
            return service.getSerialForPackage(callingPackage, null);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
        return UNKNOWN;
    }

4. Follow the source code all the way.

frameworks/base/services/core/java/com/android/server/os/DeviceIdentifiersPolicyService.java

        @Override
        public @Nullable String getSerial() throws RemoteException {
            // Since this invocation is on the server side a null value is used for the
            // callingPackage as the server's package name (typically android) should not be used
            // for any device / profile owner checks. The majority of requests for the serial number
            // should use the getSerialForPackage method with the calling package specified.
            if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mContext,
                    /* callingPackage */ null, null, "getSerial")) {
                return Build.UNKNOWN;
            }
            return SystemProperties.get("ro.serialno", Build.UNKNOWN);
        }

        @Override
        public @Nullable String getSerialForPackage(String callingPackage,
                String callingFeatureId) throws RemoteException {
             if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mContext,
                     callingPackage, callingFeatureId, "getSerial")) {
                 return Build.UNKNOWN;
             }
            return SystemProperties.get("ro.serialno", Build.UNKNOWN);
        }

At this point, you will find that there are these two method restrictions on obtaining the SN number, resulting in the state of being UNKNOWN all the time. If you want to modify the application roughly to get the SN number, then directly comment the following code:

        public @Nullable String getSerialForPackage(String callingPackage,
                String callingFeatureId) throws RemoteException {
            // if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mContext,
            //         callingPackage, callingFeatureId, "getSerial")) {
            //     return Build.UNKNOWN;
            // }
            return SystemProperties.get("ro.serialno", Build.UNKNOWN);
        }
    }

If it is reasonable and beautiful, only specific applications can obtain it, and it is necessary to judge the package name in the callingPackage field. So far, it is best to grow a little every day, cheer yourself up!

Guess you like

Origin blog.csdn.net/lwz622/article/details/115267838