Search code examples
androidxamarinxamarin.formsxamarin.androidimei

Getting IMEI number without runtime permission


Is there a way I can get IMEI(GSM)/MEID(CDMA) without implementing run time permission? I have included the following permission in Android, however for some devices I get the IMEI number and for some Android devices I am getting my IMEI number and for some devices I get Exception {Java.Lang.SecurityException: getDeviceId at Java.Interop.JniEnvironment+InstanceMethods... Why is that I have to handle the permission in the runtime despite listing them out in Manifest?

    var telephonyManager = (TelephonyManager)Forms.Context.GetSystemService(Android.Content.Context.TelephonyService);
    if (telephonyManager != null)
    {
        sIMEI = telephonyManager.DeviceId;
    }

If there is no way other than giving permission at runtime and then getting IMEI, could someone suggest the best way to get Unique ID for the android device programmatically. (The device ID should be seen from device as well) I tried the below but few post suggests that AndroidId is not the same everytime.

 var android_id = Android.Provider.Settings.Secure.GetString(Forms.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);

Solution

  • Answer is NO, If you're running on Marshmallow or above version then you need android.permission.READ_PHONE_STATE permission to access the device id.

    Alternatively you can generate unique id using below code -

    String uniqueID = UUID.randomUUID().toString();
    

    OR you can follow below link for more options

    deviceID