androidxamarintelephonymanagerandroid-9.0-pieimei

Xamarin Android 9.0 GetImei error Java.Lang.SecurityException: getImeiForSlot android.permission.READ_PHONE_STATE


I try to get my unique ID IMEI for Android 9.0 API LVL 28

When i tried with my code i have this error :

Java.Lang.SecurityException: getImeiForSlot: Neither user 10154 nor current process has android.permission.READ_PHONE_STATE.

        public class DeviceInfo : IDeviceInfo
        {
            TelephonyManager telephonyManager;
            public DeviceInfo()
            {
                telephonyManager = 
     (TelephonyManager)Application.Context.GetSystemService(Context.TelephonyService);          
            }

            public string Id
            {
                get
                {
                    telephonyManager = (TelephonyManager)Application.Context.GetSystemService(Context.TelephonyService);
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                        // TODO: Some phones has more than 1 SIM card or may not have a SIM card inserted at all
                        return telephonyManager.GetMeid(0);
                    else
    #pragma warning disable CS0618 // Type or member is obsolete
                        return telephonyManager.DeviceId;
    #pragma warning restore CS0618 // Type or member is obsolete
                }
            }

and my androdManifest.xml

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" /> 
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.VIBRATE" />

Solution

  • You have to :