Android device unique identifier (more implementations)

Foreword

Project development, how many will encounter this demand: access to equipment that uniquely identifies DeviceId, for:

1. Identify a unique device, accurate data to do statistical analysis or data issued;
2. account and device binding;
3 .....

Reference and thanks: https://www.cnblogs.com/qixingchao/p/11652408.html


analysis

These articles, there is a lot of information online, for example: using IMEI, MAC, etc. used as a device identification.
However, reading these articles or in-depth research of the students should be clear, these data are flawed: some because permissions can not get to, and some get out is repeated, some less than a full acquisition, that is not perfect the only problem solving device.

So, what data to represent the only device it?


Program

Scheme 1: UUID + SharePreference (access)

APP首次使用时,创建UUID,并保存到SharePreference中。
以后再次使用时,直接从SharePreference取出来即可;

优点:数据唯一、不需要权限;
缺点:会随APP一起删除,即:重新安装APP,DeviceId值会改变(新UUID);

Scheme 2: UUID + SD card (access)

APP首次使用时,创建UUID,并保存到SD卡中。
以后再次使用时,直接从SD卡取出来即可;
很多APP就是这么做的;

优点:数据唯一、不随APP一起删除;
缺点:需要SD卡读写权限;防不住用户手动删除SD卡的文件;

Scheme 3: imei + android_id + serial + UUID hardware (self-generated)

If want to be unique, because users do not want to delete and regenerate UUID, how to do it?

不依赖随机的UUID,咱们根据硬件标识来创建唯一的数据;

我们可以将多个可获得的硬件标识拼接起来(尽可能不依赖权限),最大程度上降低重复性。
以imei、android_id、serial为例,如果能取到值,每个数据几乎可以代表唯一。
如果这些数据都能获取到,拼起来的数据重复性降到极低(UUID也存在重复性,重复性极低而已)

So, what hardware identification appropriate?

AndroidId : 如:df176fbb152ddce,无需权限,极个别设备获取不到数据或得到错误数据;
serial:如:LKX7N18328000931,无需权限,极个别设备获取不到数据;
IMEI : 如:23b12e30ec8a2f17,需要权限;
Mac: 如:6e:a5:....需要权限,高版本手机获得数据均为 02:00.....(不可使用)
Build.BOARD  如:BLA  主板名称,无需权限,同型号设备相同
Build.BRAND  如:HUAWEI  厂商名称,无需权限,同型号设备相同
Build.HARDWARE  如:kirin970  硬件名称,无需权限,同型号设备相同
Build......更多硬件信息,略

Analysis of so much hardware identification, we use imei + android_id + serial + hardware UUID (using Build properties generate, if the same hardware information, the UUID value unchanged). This is the actual program of our project, it can also be freely combined hardware identification according to their needs.

So, the question again, different hardware devices to identify different lengths, different DeviceId string length splicing process, how to unify the length of it?

也很简单,我们先拼接好DeviceId数据,取其SHA1值,再转16进制即可(统一40位长度)

achieve

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.security.MessageDigest;
import java.util.Locale;
import java.util.UUID;

/**
 * @author xc
 * @date 2018/11/16
 * @desc
 */
public class DeviceIdUtil {
    /**
     * 获得设备硬件标识
     *
     * @param context 上下文
     * @return 设备硬件标识
     */
    public static String getDeviceId(Context context) {
        StringBuilder sbDeviceId = new StringBuilder();

        //获得设备默认IMEI(>=6.0 需要ReadPhoneState权限)
        String imei = getIMEI(context);
        //获得AndroidId(无需权限)
        String androidid = getAndroidId(context);
        //获得设备序列号(无需权限)
        String serial = getSERIAL();
        //获得硬件uuid(根据硬件相关属性,生成uuid)(无需权限)
        String uuid = getDeviceUUID().replace("-", "");

        //追加imei
        if (imei != null && imei.length() > 0) {
            sbDeviceId.append(imei);
            sbDeviceId.append("|");
        }
        //追加androidid
        if (androidid != null && androidid.length() > 0) {
            sbDeviceId.append(androidid);
            sbDeviceId.append("|");
        }
        //追加serial
        if (serial != null && serial.length() > 0) {
            sbDeviceId.append(serial);
            sbDeviceId.append("|");
        }
        //追加硬件uuid
        if (uuid != null && uuid.length() > 0) {
            sbDeviceId.append(uuid);
        }

        //生成SHA1,统一DeviceId长度
        if (sbDeviceId.length() > 0) {
            try {
                byte[] hash = getHashByString(sbDeviceId.toString());
                String sha1 = bytesToHex(hash);
                if (sha1 != null && sha1.length() > 0) {
                    //返回最终的DeviceId
                    return sha1;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        //如果以上硬件标识数据均无法获得,
        //则DeviceId默认使用系统随机数,这样保证DeviceId不为空
        return UUID.randomUUID().toString().replace("-", "");
    }

    //需要获得READ_PHONE_STATE权限,>=6.0,默认返回null
    private static String getIMEI(Context context) {
        try {
            TelephonyManager tm = (TelephonyManager) 
context.getSystemService(Context.TELEPHONY_SERVICE);
            return tm.getDeviceId();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 获得设备的AndroidId
     *
     * @param context 上下文
     * @return 设备的AndroidId
     */
    private static String getAndroidId(Context context) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), 
Settings.Secure.ANDROID_ID);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 获得设备序列号(如:WTK7N16923005607), 个别设备无法获取
     *
     * @return 设备序列号
     */
    private static String getSERIAL() {
        try {
            return Build.SERIAL;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 获得设备硬件uuid
     * 使用硬件信息,计算出一个随机数
     *
     * @return 设备硬件uuid
     */
    private static String getDeviceUUID() {
        try {
            String dev = "3883756" +
                    Build.BOARD.length() % 10 +
                    Build.BRAND.length() % 10 +
                    Build.DEVICE.length() % 10 +
                    Build.HARDWARE.length() % 10 +
                    Build.ID.length() % 10 +
                    Build.MODEL.length() % 10 +
                    Build.PRODUCT.length() % 10 +
                    Build.SERIAL.length() % 10;
            return new UUID(dev.hashCode(), 
Build.SERIAL.hashCode()).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }

    /**
     * 取SHA1
     * @param data 数据
     * @return 对应的hash值
     */
    private static byte[] getHashByString(String data)
    {
        try{
            MessageDigest  messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.reset();
            messageDigest.update(data.getBytes("UTF-8"));
            return messageDigest.digest();
        } catch (Exception e){
            return "".getBytes();
        }
    }

    /**
     * 转16进制字符串
     * @param data 数据
     * @return 16进制字符串
     */
    private static String bytesToHex(byte[] data){
        StringBuilder sb = new StringBuilder();
        String stmp;
        for (int n = 0; n < data.length; n++){
            stmp = (Integer.toHexString(data[n] & 0xFF));
            if (stmp.length() == 1)
                sb.append("0");
            sb.append(stmp);
        }
        return sb.toString().toUpperCase(Locale.CHINA);
    }
}

transfer

String deviceId = DeviceIdUtil.getDeviceId(application);

结果输出:FE00DDE9298310CDFEEFE69229B8DB248534710F

to sum up

Scenario 1 great limitation is not recommended;
Option 2 is adopted by many software programs, because very few people delete SD card files; but note that permission;
Option 3 compared to the previous two options, less restrictive, as long as The result is the same information the same hardware. The program can customize their own and in combination.

What kind of programs appropriate, we should be according to their project requirements, it is a reasonable choice.

Published 303 original articles · won praise 26 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_31433709/article/details/105120500