How do I get a Unique Identifier for a Device within Windows 10 Universal?

If you google a bit about this problem, you can’t find a right solution because all people are speaking about Hardware Token. Unfortunately this functionality doesn’t exists for Universal Windows Application.

There are at the moment only a way. You have to add the Extension reference "Windows Desktop Extensions for the UWP" or "Windows Mobile Extensions for the UWP", then you can use the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.System.Profile;

namespace PSC.Code
{
    public sealed class DeviceInfo
    {
        private static DeviceInfo _Instance;
        public static DeviceInfo Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new DeviceInfo();
                return _Instance;
            }

        }

        public string Id { get; private set; }
        public string Model { get; private set; }
        public string Manufracturer { get; private set; }
        public string Name { get; private set; }
        public static string OSName { get; set; }

        private DeviceInfo()
        {
            Id = GetId();
            var deviceInformation = new EasClientDeviceInformation();
            Model = deviceInformation.SystemProductName;
            Manufracturer = deviceInformation.SystemManufacturer;
            Name = deviceInformation.FriendlyName;
            OSName = deviceInformation.OperatingSystem;
        }

        private static string GetId()
        {
            if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
            {
                var token = HardwareIdentification.GetPackageSpecificToken(null);
                var hardwareId = token.Id;
                var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);

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

                return BitConverter.ToString(bytes).Replace("-", "");
            }

            throw new Exception("NO API FOR DEVICE ID PRESENT!");
        }
    }
}

 

Someone speaks about to use EasClientDeviceInformation provides a unique Id but this is working only for Windows Store Apps.

var deviceInformation = new EasClientDeviceInformation();
string Id = deviceInformation.Id.ToString();

 

Happy coding!

Using SQLite in Windows 10 Universal apps

Using SQLite in Windows 10 Universal apps is really easy even in this preview phase. Even though Entity Framework 7 support for Windows 10 Universal apps is almost here, you still might decide to just continue using a lighter SQLite.Net-PCL library that you're used to since Windows Phone 8/WinRT.

If you are using Visual Studio 2015 RTM and 10240 SDK, there's now the official (no more pre-release stuff) SQLite VSIX package that you can download from SQLite.org. Search for "Universal App Platform" and you're good to go! The rest of the blog post should apply to this version as well.

sqlite-latest_wcqqnu

The next step is to add the SQLite.Net-PCL library that I already mentioned. Use the (redesigned) NuGet to do that.

sqlite_uap_2

It will add two references

  • SQLite.Net
  • SQLite.Net.Platform.WinRT

Now, remember the VSIX package (Visual Studio extension) installed earlier? It installs SQLite extensions that you need to reference by simply right-clicking on References and choosing "Add Reference..." and then finding the right reference under Windows Universal -> Extensions.

sqlite-latest-extension

And that's it! You can start using SQLite!

To test it, I defined a simple model class called User.

public class User  
{
    public int Id { get; set; }
    public string Name { get; set; }
}

And then I created a table which will hold User entities.

var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
 
using (SQLite.Net.SQLiteConnection conn = 
       new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))  
{
    conn.CreateTable<user>();
}

Adding SQLite support to Windows 10 Universal apps is really simple! After all of the extensions are added, and the ORM/client library fetched over NuGet, the usage is the same as before. This is really neat for simpler scenarios and until Entity Framework 7 officially, fully supports Windows 10 Universal apps.

Advertsing

125X125_06

TagCloud

MonthList

CommentList