Search Unity

Function to get your Android Device ID (useful for Admob)

Discussion in 'Scripting' started by kor6k, Feb 4, 2019.

  1. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    Hello,
    I had hard time to get my Android Device ID to test Admob on my app... I installed 4 different apps to get it bu I got 4 different Device ID... (0 correct)
    I decided to create my own function to get it and I wanted to give it to you. I tested it and it works fine so feel free to use it.

    Code (CSharp):
    1.  
    2.     // Get Android DeviceID
    3.     public static string GetDeviceID()
    4.     {
    5.         // Get Android ID
    6.         AndroidJavaClass clsUnity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    7.         AndroidJavaObject objActivity = clsUnity.GetStatic<AndroidJavaObject>("currentActivity");
    8.         AndroidJavaObject objResolver = objActivity.Call<AndroidJavaObject>("getContentResolver");
    9.         AndroidJavaClass clsSecure = new AndroidJavaClass("android.provider.Settings$Secure");
    10.  
    11.         string android_id = clsSecure.CallStatic<string>("getString", objResolver, "android_id");
    12.  
    13.         // Get bytes of Android ID
    14.         System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    15.         byte[] bytes = ue.GetBytes(android_id);
    16.  
    17.         // Encrypt bytes with md5
    18.         System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    19.         byte[] hashBytes = md5.ComputeHash(bytes);
    20.  
    21.         // Convert the encrypted bytes back to a string (base 16)
    22.         string hashString = "";
    23.  
    24.         for (int i = 0; i < hashBytes.Length; i++)
    25.         {
    26.             hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
    27.         }
    28.  
    29.         string device_id = hashString.PadLeft(32, '0');
    30.  
    31.         return device_id;
    32.     }
    kor6k
     
    Last edited: Feb 4, 2019
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Thanks, works great :)

    Although, you may want to remove the line
    Code (CSharp):
    1. GlobalVars.Text = GlobalVars.Text + "android_id: " + android_id + "\n";
     
  3. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    Argh, you're right, I forgot to remove my debug texts....
    thanks....
     
    Munchy2007 likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    thiagofm33 likes this.
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Last edited: Feb 4, 2019
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm not sure about when/if it changed. I just know we use it for guest accounts in our games to get unique ids.

    I recall reading something long ago about it maybe behaving differently, but now I don't remember what that was.
     
  7. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    I never found this function when I was searching for a way to get Device ID...
    Thanks for the info
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    No problem, the SystemInfo class contains a lot of good information about devices. We use it also for our support tickets to get additional information about the device. Super useful for trying to track down bugs that we don't experience ourselves.
     
    thiagofm33 likes this.
  9. mshamsi

    mshamsi

    Joined:
    Mar 27, 2022
    Posts:
    1
    Great! Thank you very much