5 votes

How to get IMEI in Xamarin.Android?

I have to make an App in Xamarin (Android) where the client asked me to validate if the IMEI of the phone corresponds to the one assigned to the user instead of logging in as usual (username and password).

From what I found on the internet, the way to do it is as follows:

TelephonyManager tm = (TelephonyManager)GetSystemService(TelephonyService);
string imei = tm.GetImei(0);

But when I fill in the imei variable I get an error that says that I don't have the necessary permissions, even though I have already checked them in the Manifest .
From what I understand, This arises because you have to ask permission from the user when the device has Android 6.0 or higher (I am testing it on a Moto G5S with Android 8.1).

I looked for the solution to this problem and the only thing I found were some examples of how to do it with Android Studio. In most cases it ends up as follows:

if(Build.VERSION.SDK_INT  < Build.VERSION_CODES.M){
    //Menores a Android 6.0
    String imei= getIMEI();
    return imei;
} else {
    // Mayores a Android 6.0
    String imei="";
    if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] {Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
        imei="";
    } else {
        imei= getIMEI();
    }

    return imei;

}

Could someone help me to convert Android to Xamarin, because I've just started developing in Xamarin and I don't have much idea how to convert it.

For the sake of clarity, I clarify that my project is not from Xamarin.Forms is a Xamarin Android project that I am developing with Visual Studio.

Thank you in advance!

3voto

Matias Points 300

This is how the code I posted in the question is written:

if (Build.VERSION.SdkInt < Build.VERSION_CODES.M)
{
    //Menores a Android 6.0
    imei = tm.GetImei(0);
}
else
{
    //Mayores a Android 6.0
    int permissionCheck = Convert.ToInt32(ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadPhoneState));

    if (permissionCheck != PermissionChecker.PermissionGranted){ ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadPhoneState }, 0);
    imei = "";
    }else{
        imei = tm.GetImei(0);
    }
}

The first time we enter the App it will ask for the permissions, the second time it will return the IMEI without problems by means of the following sentence:

TelephonyManager tm = (TelephonyManager)GetSystemService(TelephonyService);
string imei = tm.GetImei(0);

0voto

jtaborga Points 501

Try this code:

[assembly: Xamarin.Forms.Dependency(typeof(UniqueIdAndroid))]
namespace UniqueId.Droid
{
    public class UniqueIdAndroid : IDevice
    {
        public string GetIdentifier()
        {
            return Settings.Secure.GetString(Forms.Context.ContentResolver, Settings.Secure.AndroidId);
        }
    }
}

And then

string deviceIdentifier = DependencyService.Get<IDevice>().GetIdentifier();

0 votes

Wouldn't this be for Xamarin.forms? Because it doesn't work for me

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X