Android unique device ID

Device ID, is simply a string of symbols (or digital), mapping real hardware devices. If any of these devices and is one to one, to be called "a unique device ID (Unique Device Identifier)"

Unfortunately, for the Android platform, there is no stable API allows developers to obtain such a device ID.

Developers often encounter such a dilemma: With the evolution of the project, more and more places need to use the device ID; however, with the Android version of the upgrade, obtain the device ID but more and more difficult.

Plus Android platform fragmentation problem, obtain the device ID of the road, can be said to be struggling.

Get Device ID API few, and are more or less there are some problems.

 

IMEI

IMEI is this the ideal device ID, with unique, restore factory settings does not change (related to real device), by dialing * # 06 # query phone imei code.

However, the need to obtain IMEI READ_PHONE_STATE permission, it is estimated we all know how much trouble this privilege.

Especially after Android 6.0, such rights apply to dynamic, many users may choose to refuse authorization. We see some of APP does not authorize this right can not be used, which may reduce the user favorability of APP.

Moreover, Android 10.0 IMEI third-party applications will be completely disabled acquisition device (even if the application READ_PHONE_STATE permission). So, if the new APP, it is not recommended for use as a device identification IMEI;

If the IMEI has been used as a logo, do not hesitate to make compatible work, especially doing the new device IMEI identification and mapping.

 

devise serial number

In the earlier system or Android 7.1 (SDK <= 25), java by android.os.Build.SERIAL obtained, provided by the manufacturer.

If vendors fairly standard, then the device serial number + Build.MANUFACTURER should be able to uniquely identify the device. But the reality is that not all vendors according to specifications, especially the older devices.

The most deadly is, Android 8.0 and above (SDK> = 26), android.os.Build.SERIAL total return "unknown"; To obtain the serial number, you can call Build.getSerial () , but need to apply READ_PHONE_STATE permission.

To the Android 10.0 (SDK> = 29) or more, and IMEI, as also banned acquired.

In UE4, a C ++ code to achieve the following:

int GetPublicStaticInt(const char *className, const char *fieldName)
{
#if PLATFORM_ANDROID
    JNIEnv* env = FAndroidApplication::GetJavaEnv();
    if (env != NULL)
    {
        jclass clazz = env->FindClass(className);
        if (clazz != nullptr) {
            jfieldID fid = env->GetStaticFieldID(clazz, fieldName, "I");
            if (fid != nullptr) {
                return env->GetStaticIntField(clazz, fid);
            }
        }
    }
#endif

    return 0;
}

FString GetPublicStaticString(const char *className, const char *fieldName)
{
#if PLATFORM_ANDROID
    JNIEnv* env = FAndroidApplication::GetJavaEnv();
    if (env != NULL)
    {
        jclass clazz = env->FindClass(className);
        if (clazz != nullptr) {
            jfieldID fid = env->GetStaticFieldID(clazz, fieldName, "Ljava/lang/String;");
            if (fid != nullptr) {
                jstring content = (jstring)env->GetStaticObjectField(clazz, fid);
                return ANSI_TO_TCHAR(env->GetStringUTFChars(content, 0));
            }
        }
    }
#endif

    return FString();
}

FString GetStaticMethodNoParametersRetString(const char *className, const char *fieldName)
{
     JNIEnv* env = FAndroidApplication::GetJavaEnv();
     IF (the env =! NULL) 
     { 
         JClass clazz = env-> the FindClass (className);
          IF (! = clazz nullptr a) {
             // () for the type of the parameter list, the parameter is null indicates no
             // Ljava / lang / String ; return value type 
             jmethodID FID = (the env) -> GetStaticMethodID (clazz, " the fieldName " , " () Ljava / lang / String; " );
             IF (! FID = NULL) 
            { 
                 the jstring Content = (the jstring) ((the Env ) -> CallStaticObjectMethod (clazz, FID)); 
                Serial= ANSI_TO_TCHAR(env->GetStringUTFChars(content, 0));
             }
         }
     }
}

FString serial = TEXT("")
int sdk = GetPublicStaticInt("android/os/Build$VERSION", "SDK_INT");
if (sdk >= 29 ) // Android Q(>= SDK 29)
{
    
}
else if (sdk >= 26) // Android 8 and later (>= SDK 26)
{
    serial = GetStaticMethodNoParametersRetString("android/os/Build", "getSerial");
}
else //Android 7.1 and earlier(<= SDK 25)
{
    serial = GetPublicStaticString("android/os/Build", "SERIAL");
}

Overall, the equipment serial number a bit tasteless: eat a token gesture.


MAC address

Most devices have android wifi module, therefore, MAC address wifi module can be used as a device identifier. For privacy reasons, officials do not recommend getting

Get the MAC address is more and more difficult, Android 6.0 later acquired by WifiManager mac will be fixed: 02:00: 00:00: 00:00 

After reading 7.0 / SYS / class / NET / wlan0 / address also get less than the (millet 6).

Today only following this method can be obtained (no open wifi can also get to):

public static String getWifiMac() {
    try {
        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        if (enumeration == null) {
            return "";
        }
        while (enumeration.hasMoreElements()) {
            NetworkInterface netInterface = enumeration.nextElement();
            if (netInterface.getName().equals("wlan0")) {
                return formatMac(netInterface.getHardwareAddress());
            }
        }
    } catch (Exception e) {
        Log.e("tag", e.getMessage(), e);
    }
    return "";
}

And then further'm not sure this approach does not work, and the use and cherish ~


ANDROID_ID

Android ID is to obtain the minimum threshold, and does not require any permission, the range of 64bit, uniqueness regarded as a good.

But the shortcomings are obvious:

1, Brush, root, restore factory settings will change such that Android ID;
2, after Android 8.0, Android ID occurred rule changes :

For an upgrade to installed applications before 8.0, ANDROID_ID will remain unchanged. If after uninstalling, then reinstalling, ANDROID_ID will change.
For application installed in the system is 8.0, ANDROID_ID depending on the application and the user's signature is different. The only decision ANDROID_ID in application signatures, user and device combination of the three.

The results lead to two rules is this:
first, if the user installs APP equipment is 8.0, then uninstalled, upgrade to 8.0 and then reinstall the application, Android ID is not the same;
second, different signatures of APP, acquired Android ID is not the same.
The second point which may (if comparative data with one another, then Android ID), so Google Docs say "please use the Advertising ID", to have an impact on ad networks like
but we all know, Google's service in the country does not take .
Android ID to do a constraint, privacy protection play a role, and to make their own active APP statistics still no problem.

 

Put together with the hardware ID information

The advantage is that no additional authority, is the only drawback of not a hundred percent sure that

As a hardware millet 10 infos

BOARD: umi
BRAND: Xiaomi
DEVICE: umi
DISPLAY: QKQ1.191117.002 test-keys
HOST: c5-miui-ota-bd074.bj
ID: QKQ1.191117.002
MANUFACTURER: Xiaomi
MODEL: Mi 10
PRODUCT: umi
TAGS: release-keys
TYPE: user
USER: builder

java code to achieve the following:

Import the java.security.MessageDigest;
 Import java.security.NoSuchAlgorithmException; 

public  class MD5Tool {
     Private  Final  static String [] = {hexArray "0", ". 1", "2", ". 3", ". 4", ". 5" , ". 6", ". 7", ". 8", ". 9", "a", "B", "C", "D", "E", "F" }; 

    / ** * 
     * acquire the specified MD5 string 
     * / 
    public  static string CalcMD5 (string originString) {
         the try {
             // create MD5 message digest algorithms have 
            MessageDigest md = MessageDigest.getInstance ( "MD5");
             // with the specified byte array to digest last updated, and then completes the digest calculation 
            byte [] bytes =md.digest (originString.getBytes ());
             // the byte array into a string returns obtained 
            String S = byteArrayToHex (bytes);
             return s.toLowerCase (); 
        } the catch (NoSuchAlgorithmException E) { 
            e.printStackTrace ( ); 
        } 
        return  null ; 
    } 
    / ** 
     * converted into a hexadecimal byte array, and returns a string 
     * 128 refers to the bit. Binary too long, it is generally rewritten to hexadecimal, 
     * each hexadecimal 4-digit binary number can be replaced, so the 128-bit binary number written in hexadecimal becomes 128/4 = 32. 
     * / 
    Private  static String byteArrayToHex ( byte [] B) { 
        the StringBuffer SB = new newThe StringBuffer ();
         for ( int I = 0; I <to b.length; I ++ ) { 
            sb.append (byteToHex (B [I])); 
        } 
        return sb.toString (); 
    } 
    / ** 
     * a byte converted to hexadecimal and returns a string 
     * / 
    public  static string byteToHex ( byte B) {
         int n-= B;
         IF (n-<0 ) 
            n- = n-+ 256 ;
         int D1 = n-/ 16 ;
         int = 16% n-D2 ;
         return hexArray [D1] +  hexArray [D2];
    } 
}


String hardwareInfo = android.os.Build.BOARD + android.os.Build.BRAND + android.os.Build.DEVICE + android.os.Build.DISPLAY 
            + android.os.Build.HOST + android.os.Build.ID + android.os.Build.MANUFACTURER + android.os.Build.MODEL 
            + android.os.Build.PRODUCT + android.os.Build.TAGS + android.os.Build.TYPE +android.os. Build.USER;

string md5 = MD5Tool.CalcMD5(hardwareInfo);

In UE4, a C ++ code to achieve the following:

FString board = GetPublicStaticString("android/os/Build", "BOARD");
FString brand = GetPublicStaticString("android/os/Build", "BRAND");
FString device = GetPublicStaticString("android/os/Build", "DEVICE");
FString display = GetPublicStaticString("android/os/Build", "DISPLAY");= GetPublicStaticString (
FString host"android/os/Build", "HOST");
FString id = GetPublicStaticString("android/os/Build", "ID");
FString manufacturer = GetPublicStaticString("android/os/Build", "MANUFACTURER");
FString model = GetPublicStaticString("android/os/Build", "MODEL");
FString product = GetPublicStaticString("android/os/Build", "PRODUCT");
FString tags = GetPublicStaticString("android/os/Build", "TAGS");
FString type = GetPublicStaticString("android/os/Build", "TYPE");
FString user = GetPublicStaticString("android/os/Build", "USER");

FString hardwareInfo = board + brand + device + display + host + id 
            + manufacturer + model + product + tags + type +user; 
FString md5 = FMD5 :: HashAnsiString (* hardwareInfo)

 

reference

Talk unique device ID ( link2 )

Android device uniquely identify acquisition and construction

 

Guess you like

Origin www.cnblogs.com/kekec/p/12544674.html