Android How to Get a Device Unique Identity

  • 2021-12-11 19:05:05
  • OfStack

Directory 1. First, briefly summarize the disadvantages of several common solutions under 1: 1. IMEI
2. Android ID
3. MAC address
2. uuid + Local Files for a Common Solution 1. Thoughts
2. Solve the problem of mobile phone access to SDK
3. Adapt to Android 11
4. Flutter code practice

1. First, briefly summarize the disadvantages of several common solutions under 1:

1. IMEI

The official in Android 10 clearly stated that the third-party application could not obtain the privacy change in IMEI code: Android 10,

READ_PHONE_STATE permission is required for versions below Android 10.

2. Android ID

Android ID does not have true uniqueness,

ROOT, brushing machine, restoring factory settings, application of different signatures, etc. will all cause the acquired Android ID to change.

And the BUG of systems customized by different vendors may cause different devices to produce the same Android ID.

3. MAC address

MAC address in Android 10 has randomized characteristics: privacy change in Android 10-MAC address,

Although most mobile phones do not support this feature at present, this scheme will gradually become obsolete with the follow-up of manufacturers

When the logo of these devices is not enough to meet the demand, we will adopt another method.

2. uuid + Local Files for a Common Solution

1. Thoughts

When starting APP, check and read the file with uuid stored in the root directory. If there is no such file, it will be regarded as a new device, and the file will be created and written into uuid.

And make sure that when you uninstall the application, the file will not be removed by the system (which is why you want to create it in the root directory).

2. Solve the problem of mobile phone access to SDK

Below Android 6, add permissions:


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Android 6 and above, you need to apply for dynamic permission on this basis.

Android 10 and above, the file storage mechanism is modified to sandbox mode, that is, applications can only access files under their own sandbox and public media files.

"Benefiting" from the sandbox mechanism, the files created by the application belong to their own sandbox, so when the application is uninstalled, it will also be deleted.

At present, you can add the following 1 line of code to solve the sandbox problem:


<application
  ...
  android:requestLegacyExternalStorage="true">

In this way, we can create our own files in the "root directory".

3. Adapt to Android 11

Android 11 enforces sandbox mode. Before that, storage permissions can be simply divided into "forbidden" and "allowed". After that, storage permissions can be simply divided into "forbidden", "allowed access to media files" and "allowed access to all files".

"Allow access to media files" is what most applications can apply for, while "Allow access to all files" can only be applied for by file management applications. If you are not this kind of application but apply for this permission, you will not pass the audit of Google Play.

Allow access to all files: android.permission.MANAGE_EXTERNAL_STORAGE

Introduced here, in fact, a scheme has come out: directly apply for the permission of "allow access to all files", and the consequence is that it cannot pass the audit of Google Play.

There is another way: we will not upgrade SDK for the time being, but develop applications for Android 10 (SDK 29), so that our applications can run normally on Android 11 system due to the "backward compatibility mechanism".

4. Flutter Code Practice


import 'dart:io';
import 'package:uuid/uuid.dart';

//  Local persistent storage uuid Code practice 
class Storage {
 static File file;

 //  Entrance 
 static Future<String> init() async {
  bool boolCreateFile = await createFile();
  if (boolCreateFile) {
   String uuid = await readData();
   return uuid;
  } else {
   await writeData();
   String uuid = await readData();
   return uuid;
  }
 }

 //  Create a file 
 static Future<bool> createFile() async {
  file = File('/storage/emulated/0/uuid.ini'); //  Point to a file in the root directory uuid
  bool exists = await file.exists();
  return exists;
 }

 //  Write data 
 static writeData() async {
  //  If the file exists, the original contents will be overwritten ,  If it does not exist, create a file 
  String uuid = await getUuid();
  file.writeAsString('$uuid');
 }

 //  Read a file 
 static Future<String> readData() async {
  try {
   String uuid = await file.readAsString();
   return uuid;
  } catch (e) {
   return null;
  }
 }

 //  Get uuid Plug-ins adopted by :uuid
 static Future<String> getUuid() async {
  Uuid uuidObj = Uuid();
  String uuid = uuidObj.v1();
  return uuid;
 }
}

The above is Android how to obtain the device only 1 identification details, more about Android to obtain the device only 1 identification information please pay attention to other related articles on this site!


Related articles: