Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Device Id in Xamarin Forms?

Tags:

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"

like image 750
Srusti Thakkar Avatar asked May 08 '18 11:05

Srusti Thakkar


People also ask

How can I get device ID in xamarin iOS?

use UIDevice. CurrentDevice. IdentifierForVendor. ToString() to get the device ID on iOS.

How do I find my android device ID?

Open your phone's dial pad, usually named “Phone” in the apps drawer. 2. Here dial this code *#*#8255#*#*. As soon as you will enter the last digit, Gtalk Service Monitor will open up and show your Android device ID along with your email.

What is xamarin essentials?

Xamarin. Essentials provides a single cross-platform API that works with any iOS, Android, or UWP application that can be accessed from shared code no matter how the user interface is created. See the platform & feature support guide for more information on supported operating systems.


3 Answers

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.

like image 55
Johannes Avatar answered Sep 26 '22 23:09

Johannes


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;
        }
    }
like image 26
nzrytmn Avatar answered Sep 26 '22 23:09

nzrytmn


There is a plugin presenting by James Montemagno.

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

like image 42
Erdogan Avatar answered Sep 22 '22 23:09

Erdogan