26

How can I get Device Unique Id in Android and iOS using c# in Xamarin Froms? I am using Azure Notification Hub for sending Notification. I am referring this blog. But in Android I am not able to find related "Settings"

Srusti Thakkar
  • 1,835
  • 2
  • 22
  • 52

3 Answers3

42

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.

Himen Suthar
  • 80
  • 1
  • 3
  • 11
Johannes
  • 958
  • 9
  • 14
  • 5
    For android, Forms.Context is obsolete , use Android.App.Application.Context istead. – Baqer Naqvi Feb 13 '19 at 12:19
  • If you got an error with Android.Provider user this "global::Android.Provider" – cho May 27 '19 at 00:42
  • 1
    I thought it was illegal to store a users device id in ur app i may be wrong on that. – c-sharp-and-swiftui-devni Jun 08 '19 at 15:57
  • 2
    Becareful! 'UIDevice.CurrentDevice.IdentifierForVendor.ToString()' just gives you new guid. When you delete your app an re-install it, The value will change. So it is not exactly device Id. – ayberkzeray Nov 13 '19 at 08:21
  • i used `Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);` on 5 application but one application return different device id but 4 other applications return same device id, I tested all 5 application on same device – Deepak yogi Sep 29 '21 at 09:32
15

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;
        }
    }
nzrytmn
  • 6,193
  • 1
  • 41
  • 38
  • 3
    +1 ! And apart from the technical reasons for looking into this method, I am also going the route of app/device guids in secure storage so as to avoid any regulatory issues with pseudonymisation. A physical device id may be considered personal data. – Frank May 07 '21 at 06:45
  • Assuming your user has to login, you already have user-specific identification. What is the benefit of also generating a GUID? I have yet to encounter an app that didn't ask for some form of user identification (usually email). – ToolmakerSteve Jul 19 '21 at 14:09
  • Using a generated GUID is not an Device ID and it will be readily removed on a reset, therefore your "device id" will change. I would suggest not using this approach. I used to use IMEI but of course that is no longer available. Look at Erdogan's suggestion. – Mark Anderson Apr 03 '22 at 06:28
1

There is a plugin presenting by James Montemagno.

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

Erdogan
  • 952
  • 12
  • 26