Android Ultimate Plugin part 15 – Android Advance Info

Hi Guys, today we will show you on how to access some advance Information on Android Devices, so let’s Start

Requirements

You need to have an Android Manifest File on “Assets/Plugins/Android” Directory and then you need to make sure that  you have this permission

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

If you are sure that you have an Android Manifest File and it’s place on the proper Directory “Assets/Plugins/Android” and you the above Permission we can now start coding on C#.

First we need to have the instance of DeviceInfoPlugin

private DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin.GetInstance();

Next we need to off the toast Debug Message, because it’s annoying to see it always

deviceInfoPlugin.SetDebug(0);

And then we need to Initialized it

deviceInfoPlugin.Init();

Next we setup call back Listener for Android Advertising ID event

deviceInfoPlugin.SetDeviceInfoCallbackListener(onGetAdvertisingIdComplete,onGetAdvertisingIdFail);   

private void onGetAdvertisingIdComplete(string advertisingId){        
    Debug.Log("onGetAdvertisingIdComplete Advertising ID: " + advertisingId);
}

private void onGetAdvertisingIdFail(string errorMessage){
    Debug.Log("onGetAdvertisingIdFail errorMessage: " + errorMessage);
}

To get  Android Advertising Id just do this line of code

public void GetAdvertisingId(){
    deviceInfoPlugin.GetAdvertisingId();
}

To get Android Id

public void GetAndroidId(){
    string androidId = deviceInfoPlugin.GetAndroidId();        
    Debug.Log(" Android ID: " + androidId);
}

To get Telephony Device ID

public void GetTelephonyDeviceId(){
    bool hasSim = deviceInfoPlugin.CheckSim();
    if(hasSim){
        string telephonyDeviceId = deviceInfoPlugin.GetTelephonyDeviceId();            
    }
}

To get Telephony Sim Serial Number

public void GetTelephonySimSerialNumber(){
    bool hasSim = deviceInfoPlugin.CheckSim();
    if(hasSim){
        string telephonySimSerialNumber = deviceInfoPlugin.GetTelephonySimSerialNumber();            
        Debug.Log("Telephony SimSerial Number: " + telephonySimSerialNumber);
    }
}

To generate Unique ID just do this line of code

public void GenerateUniqueId(){
    bool hasSim = deviceInfoPlugin.CheckSim();
    if(hasSim){
        string uniqueId = deviceInfoPlugin.GenerateUniqueId();            
        Debug.Log("Unique ID: " + uniqueId);
    }
}

 

For Reference here’s the code the we used on Advance Android Info Demo

Note: To Make this code run you need to Drag Text UI on this code Inspector.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class AndroidAdvanceInfoDemo : MonoBehaviour {

    public Text androidIdText;
    public Text telephonyDeviceIdText;
    public Text telephonySimSerialNumberText;
    public Text advertisingIdText;
    public Text uniqueIdText;
    public Text simStatusText;

    private DeviceInfoPlugin deviceInfoPlugin;

    private bool hasSim = false;

    // Use this for initialization
    void Start () {
        deviceInfoPlugin = DeviceInfoPlugin.GetInstance();
        deviceInfoPlugin.SetDebug(0);
        deviceInfoPlugin.Init();
        deviceInfoPlugin.SetDeviceInfoCallbackListener(onGetAdvertisingIdComplete,onGetAdvertisingIdFail);

        hasSim = deviceInfoPlugin.CheckSim();
    }
    
    public void GetAndroidId(){
        string androidId = deviceInfoPlugin.GetAndroidId();
        androidIdText.text =  string.Format("Android ID: {0}",androidId);
        Debug.Log(" Android ID: " + androidId);
    }

    public void GetTelephonyDeviceId(){
        if(hasSim){
            string telephonyDeviceId = deviceInfoPlugin.GetTelephonyDeviceId();
            telephonyDeviceIdText.text =  string.Format("telephony Device ID: {0}",telephonyDeviceId);
            Debug.Log(" telephony Device ID: " + telephonyDeviceId);
        }else{
            telephonyDeviceIdText.text =  string.Format("telephony Device ID: {0}","no sim");
            Debug.Log("no sim");
        }
    }

    public void GetTelephonySimSerialNumber(){
        if(hasSim){
            string telephonySimSerialNumber = deviceInfoPlugin.GetTelephonySimSerialNumber();
            telephonySimSerialNumberText.text =  string.Format("Telephony SimSerial Number: {0}",telephonySimSerialNumber);
            Debug.Log("Telephony SimSerial Number: " + telephonySimSerialNumber);
        }else{
            telephonySimSerialNumberText.text =  string.Format("Telephony SimSerial Number: {0}","no sim");
            Debug.Log("no sim");
        }
    }

    public void GetAdvertisingId(){
        deviceInfoPlugin.GetAdvertisingId();
    }

    public void GenerateUniqueId(){
        if(hasSim){
            string uniqueId = deviceInfoPlugin.GenerateUniqueId();
            uniqueIdText.text =  string.Format("Unique ID: {0}",uniqueId);
            Debug.Log("Unique ID: " + uniqueId);
        }else{
            uniqueIdText.text =  string.Format("Unique ID: {0}","no sim");
            Debug.Log("no sim");
        }
    }

    public void CheckSim(){
        bool hasSim = deviceInfoPlugin.CheckSim();
        simStatusText.text =  string.Format("Has Sim: {0}",hasSim);
        Debug.Log("Has Sim: " + hasSim);
    }

    private void onGetAdvertisingIdComplete(string advertisingId){
        advertisingIdText.text =  string.Format("Advertising ID: {0}",advertisingId);
        Debug.Log("onGetAdvertisingIdComplete Advertising ID: " + advertisingId);
    }

    private void onGetAdvertisingIdFail(string errorMessage){
        Debug.Log("onGetAdvertisingIdFail errorMessage: " + errorMessage);
    }
}

 

And That’s it , that’s how easy and simple to used it.

For questions or comments or suggestions or any feed back just email us at gigadrillgames@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.