"SystemInfo.deviceUniqueIdentifier"是什么?在Android版本中使用? [英] What does "SystemInfo.deviceUniqueIdentifier" use in Android builds?

查看:948
本文介绍了"SystemInfo.deviceUniqueIdentifier"是什么?在Android版本中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档指出了iOS/Windows版本中使用的标识符,但不是Android版本. SystemInfo.deviceUniqueIdentifier在Android上使用什么标识符?

The documentation notes what identifier is used in iOS/Windows builds, but not Android builds. What identifier does SystemInfo.deviceUniqueIdentifier use on Android?

推荐答案

我不明白为什么文档中未提及该问题.

I don't understand why that is not mentioned in the Doc.

我上次检查时,Unity使用 Secure.ANDROID_ID 来获取Android ID字符串,然后将其转换为字符串.如果执行此操作,则应使用与 SystemInfo.deviceUniqueIdentifier 相同的值. .这就是我的设备上正在发生的事情.

Last time I checked, Unity uses Secure.ANDROID_ID to get the Android ID string then converts that to string. If you do that, you should the the-same value as SystemInfo.deviceUniqueIdentifier. This is what's happening on my device.

不幸的是,更多的事情在后台进行.

Unfortunately, more things are going on in the background.

Unity在其论坛中记录了其唯一标识符详细信息"实现. .

Unity documented their Unique Identifier Details implementation on their forum.

1 .使用context.getSystemService(Context.TElEPHONY_SERVICE).getDeviceId()

2 .如果#1 失败,请使用context.getContentResolver().getString(Secure.ANDROID_ID);

2.If #1 fails, get Android ID with context.getContentResolver().getString(Secure.ANDROID_ID);

3 .如果#2 失败,请获取Mac地址.

3.If #2 fails, get the Mac Address.

4 .将结果转换为#1 #2 #3 成功)到MD5哈希.

4.Convert result from #1, #2 or #3(which ever one that was successful) to MD5 Hash.

值得一读的是论坛帖子,因为该行为是在某些Unity版本上有些不同.

It's worth reading that forum post since the behavior is a little different on some Unity versions.

以下是他们提供的示例代码:

Here is a sample code provide by them on what it looks like:

// Hash an input string and return the hash as
// a 32 character hexadecimal string.
static string getMd5Hash(string input)
{
    if (input == "")
        return "";
    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
    StringBuilder sBuilder = new StringBuilder();
    for (int i = 0; i < data.Length; i++)
        sBuilder.Append(data[i].ToString("x2"));
    return sBuilder.ToString();
}

static string generateDeviceUniqueIdentifier(bool oldBehavior)
{
    string id = "";
    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass contextClass = new AndroidJavaClass("android.content.Context");
    string TELEPHONY_SERVICE = contextClass.GetStatic<string>("TELEPHONY_SERVICE");
    AndroidJavaObject telephonyService = activity.Call<AndroidJavaObject>("getSystemService", TELEPHONY_SERVICE);
    bool noPermission = false;
    try
    {
        id = telephonyService.Call<string>("getDeviceId");
    }
    catch (Exception e) {
        noPermission = true;
    }
    if(id == null)
        id = "";
    // <= 4.5 : If there was a permission problem, we would not read Android ID
    // >= 4.6 : If we had permission, we would not read Android ID, even if null or "" was returned
    if((noPermission && !oldBehavior) || (!noPermission && id == "" && oldBehavior))
    {
        AndroidJavaClass settingsSecure = new AndroidJavaClass("android.provider.Settings$Secure");
        string ANDROID_ID = settingsSecure.GetStatic<string>("ANDROID_ID");
        AndroidJavaObject contentResolver = activity.Call<AndroidJavaObject>("getContentResolver");
        id = settingsSecure.CallStatic<string>("getString", contentResolver, ANDROID_ID);
        if(id == null)
            id = "";
    }
    if(id == "")
    {
        string mac = "00000000000000000000000000000000";
        try
        {
            StreamReader reader = new StreamReader("/sys/class/net/wlan0/address");
            mac = reader.ReadLine();
            reader.Close();
        }
        catch (Exception e) {}
        id = mac.Replace(":", "");
    }
    return getMd5Hash(id);
}

这篇关于"SystemInfo.deviceUniqueIdentifier"是什么?在Android版本中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆