© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
J. StevensonAndroid Software Internals Quick Referencehttps://doi.org/10.1007/978-1-4842-6914-5_7

7. Android Unique Identifiers

James Stevenson1  
(1)
London, UK
 

Unique identifiers can be used for an array of tasks, from allowing users to set advertising preferences, uniquely identifying a user, and identifying a specific device. There are also strong privacy concerns when it comes to the use of unique identifiers enforced by both Android permissions and the Google Play Developer Policy.

In an attempt to address user privacy concerns, as of Android 10 (API level 29), there has been a large change in how hardware unique identifiers can be accessed from within Android applications. This being that an application must be a device or profile owner, have special carrier permissions, or have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access nonresettable device identifiers. The READ_PRIVILEGED_PHONE_STATE permission is only available to applications signed with the device’s platform (system application) key1.

Google Play Advertising ID

As of Android KitKat (API level 4.4), the Google Play Advertising ID 2 can be used to uniquely identify a user of a device. When available on a device, it is an offense against the Google Play Developer Programme Policy to use any other device unique identifiers for advertising purposes. The benefit of the advertising ID for an end user is that it is both resettable and can be used for customizing personalized advertising. An example of the ID returned is a string representation of 9fdbfa02-7f28-422e-944e-f02393a9360e. As the advertising ID is provided by the Google Play Services API, it means that it will only be available on devices with Google Play Services.

The advertising ID can be used by adding the following library path to the build.gradle file’s dependencies tag:
implementation 'com.google.android.gms:play-services-ads:7.0.0'
Retrieving the advertising ID (this cannot be done on the main thread - this example uses an AsyncTask; see Chapter 9 for more information):
void getAdvertisingID(final Context context){
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            Info adInfo = null;
            try {
                adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }
            String adId = adInfo != null ? adInfo.getId() : null;
            Log.v("Advertising ID",adId);
        }
    });
}

Android ID (Secure Settings Android ID – SSAID)

This is a unique 64-bit number (e.g., ce79870fa5cbfb56) that is Android’s preferred approach for identifying a user of a device for activities outside of advertising. This unique identifier is available on all versions of Android, does not require any additional permissions, and is reset as part of a device factory reset.

Accessing the Android ID :
Log.v("Android ID",Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID));

SIM Serial Number

A SIM serial number is used for international identification and is typically broken down into 19 digits3. This breaks down to two digits for the Telecom ID, two digits for the country code, two digits for the network code, four digits for the month and year of manufacturing, two digits for the switch configuration code, six digits for the SIM number, and a single check digit.

The SIM serial number is available on Android up to and including Android Pie (API level 28), where it is restricted in Android 10 and above with the READ_PRIVILEGED_PHONE_STATE permission. To use the SIM serial number prior to Android 10 requires the READ_PHONE_STATE runtime permission.

Retrieving the SIM serial number:
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
Log.v("SIM Serial Number", telephonyManager.getSimSerialNumber());

Phone Number

When writing applications for devices that use telephony, the phone number can be used as a unique identifier. This identifier is available on all versions of Android and is tied to the SIM card on the device (if one is present) and requires the READ_PHONE_STATE or READ_PRIVILEGED_PHONE_STATE permission to access.

Retrieving the phone number :
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
Log.v("Phone Number", telephonyManager.getLine1Number());

IMEI and MEID

To both uniquely identify devices and prevent theft, all mobile devices are designated an IMEI or MEID number. Depending on the network of the device will depend on which identifier the device has. Devices will have an IMEI number if on GSM (Global System for Mobiles) systems and will have a MEID number if on a CDMA (code division multiple access) system. The main difference between the two is that an IMEI is a 14-digit number, while a MEID is a 15-digit number. Similar to other hardware identifiers, both IMEIs and MEIDs are available on devices prior to Android 10 where it can be read if the application has the READ_PHONE_STATE permission. However, as of Android 10, an application requires the READ_PRIVILEGED_PHONE_STATE.

Pre and including Android N (25) access:
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
Log.v("Device ID", telephonyManager.getDeviceId());
Post (and including) Android O (26):
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
Log.v("IMEI", telephonyManager.getImei());
Log.v("MEID", telephonyManager.getMeid());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.150