How to get Device Id in Xamarin Forms?

There is a plugin presenting by James Montemagno.

Search it in NuGet Packages as Xam.Plugin.DeviceInf. Github link: https://github.com/jamesmontemagno/DeviceInfoPlugin


It is detailed described here. But actually you don't need to do like this and trying to get an Id from each devices. Simply creating a Guid and saving to device is also working. Xamarin has Prefences for persitent a Value in a device.

You can create a guid and save it to Prefecences as I did below:

var deviceId = Preferences.Get("my_deviceId", string.Empty);
if(string.IsNullOrWhitespace(deviceId))
{
  deviceId = System.Guid.NewGuid().ToString();
  Preferences.Set("my_deviceId", deviceId);
}

Benefit of this approach is you will still have same Id after you transferred your app to another device; but if you uninstall and reinstall again than you will get a new Id. For the other cases that you get an Id from device, it will be change when you transfer your app to another device.

For other cases that you want to get Id from device:

iOS: IdentifierForDevice

public string Id => UIDevice.CurrentDevice.IdentifierForVendor.AsString();

Android: Serial, getSerial & AndroidId

    string id = string.Empty;
    public string Id
    {
        get
        {
            if (!string.IsNullOrWhiteSpace(id))
                return id;

            id = Android.OS.Build.Serial;
            if (string.IsNullOrWhiteSpace(id) || id == Build.Unknown || id == "0")
            {
                try
                {
                    var context = Android.App.Application.Context;
                    id = Secure.GetString(context.ContentResolver, Secure.AndroidId);
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Warn("DeviceInfo", "Unable to get id: " + ex.ToString());
                }
            }

            return id;
        }
    }

UWP: GetPackageSpecificToken or GetSystemIdForPublisher

 string id = null;
    public string Id
    {
        get
        {

            if (id != null)
                return id;

            try
            {
                if (ApiInformation.IsTypePresent("Windows.System.Profile.SystemIdentification"))
                {
                    var systemId = SystemIdentification.GetSystemIdForPublisher();

                    // Make sure this device can generate the IDs
                    if (systemId.Source != SystemIdentificationSource.None)
                    {
                        // The Id property has a buffer with the unique ID
                        var hardwareId = systemId.Id;
                        var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);

                        var bytes = new byte[hardwareId.Length];
                        dataReader.ReadBytes(bytes);

                        id = Convert.ToBase64String(bytes);
                    }
                }

                if (id == null && ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
                {
                    var token = HardwareIdentification.GetPackageSpecificToken(null);
                    var hardwareId = token.Id;
                    var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);

                    var bytes = new byte[hardwareId.Length];
                    dataReader.ReadBytes(bytes);

                    id = Convert.ToBase64String(bytes);
                }

                if (id == null)
                {
                    id = "unsupported";
                }

            }
            catch (Exception)
            {
                id = "unsupported";
            }

            return id;
        }
    }

According your posted blogpost http://codeworks.it/blog/?p=260 and your short problem description I try to answer your question.

For android use Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);

For iOS see your blogpost.. or optionally save the IdentifierForVendor e.g. in your AppDelegate and return this value in your IOSDevice class (using the name in the blogpost). use UIDevice.CurrentDevice.IdentifierForVendor.ToString() to get the device ID on iOS.