Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get IMEI of Android phone in Unity?

Discussion in 'Android' started by peetonn, Jun 27, 2018.

  1. peetonn

    peetonn

    Joined:
    Jul 24, 2017
    Posts:
    5
    Nothing works, I give up googling anymore.

    I use this call

    ```
    SystemInfo.deviceUniqueIdentifier
    ```
    which supposed to give IMEI when READ_PHONE_STATE is enabled. Please, raise your hand if you know how to properly set this permission in Unity.

    This is what I did:
    1. Since there is no information whatsoever about how to add new AndroidManifest to Unity project from scratch, I've added this (copied from existing plugin I use, and removed unnecessary lines):

    ```
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.ucla.remap.artest"
    android:versionCode="19"
    android:versionName="5.1.1" >

    <uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="27" />

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

    <application>

    </application>

    </manifest>

    ```

    to `Assets/Plugins/Android` folder. Build fails because "MainActivity is empty". How should I know which is the main Activity?

    2. Abandoned idea #1 and added
    ```
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    ```

    to an AndroidManifest of existing plugin that I use. However, when I start application it doesn't ask for user permission, hence, I believe, this permission request doesn't take any effect.

    Is it possible to get IMEI of an Android phone in Unity?
    Thanks,
     
  2. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    835
    Hi,
    SystemInfo.deviceUniqueIdentifier doesn't necessarily return IMEI. It returns a value that Uniquely identifies a device, but not necessarily the IMEI value.
    If you want to specifically get the IMEI, you should call methods from android API: getDeviceId (before android api level 26) or getImei / getMeid afterwards (see official documentation for more information here https://developer.android.com/reference/android/telephony/TelephonyManager#getDeviceId() ). You can call these methods using AndroidJavaObject (see https://docs.unity3d.com/ScriptReference/AndroidJavaObject.html for more information).
    If you want to know how to create android manifest and what parts should be included in it, you should read official documentation here https://developer.android.com/guide/topics/manifest/manifest-intro afterwards you can check this page https://docs.unity3d.com/Manual/android-manifest.html to find out how Unity uses that manifest.
    If the final manifest has unityplayer.SkipPermissionsDialog in it, then you will have to manually ask for those permissions. See previous link to android manifest in Unity docs.
     
    BMRG14, alireza97 and Yury-Habets like this.
  3. madfu

    madfu

    Joined:
    Aug 9, 2018
    Posts:
    19
    this explanation was very thorough. thanks for taking the time to post all the links. thankfully, this is very consistent with how native Android development looks under the hood. i did find one other link that i thought was especially helpful in terms of providing sample code. here it is : https://forum.unity.com/threads/android-and-iphone-macaddress.108506/. just skip down to the post by "Billboard."