3

A question came up recently which requires the generation of a unique identifier for the Pi using Win-Iot/C#. My first thought was to use a MAC code, but the approach that I've found at embedded101.com looks pretty cumbersome.

Is there a neat UUID-type identifier available for the Pi under Windows IoT, and how do I access it?

  • You can use a Guid – giammin Aug 4 '16 at 21:01
  • I could - a randomly generated guid would certainly hold a low collision risk. I kind of need to be able to retrieve a consistent, repeatable value without resorting to persistent storage. I don't know a good way to do that with a random guid. – goobering Aug 4 '16 at 21:12
3

If all you want to rely on is the MAC address of an interface you can use this code. It has been available since .NET 2 and it should be implemented in IoT and Mono alike

public static HashSet<string> NetworkIds()
{
  var result = new HashSet<string>();

  var networkProfiles = Windows.Networking.Connectivity.NetworkInformation.GetConnectionProfiles().ToList();

  foreach (var net in networkProfiles)
  {
    result.Add(net.NetworkAdapter.NetworkAdapterId.ToString());
  }

  return result;
}

You can also go and explore this namespace on the IoT

import Windows.Security.ExchangeActiveSyncProvisioning;

and try checking what lies on these object. Try and test them on two Pi's

var deviceInfo = new EasClientDeviceInformation;
deviceInfo. 

Here is another, more complete sample that may be usefull.

using System;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.System.Profile;


    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!");
        }
    }
  • 1
    +1 for consistent use of 'manufracturer', which is now my new favourite word for describing my role in the vast majority my Pi projects. – goobering Aug 1 '16 at 21:44
  • Did it work as expected ? I've not had time to test this my self and just curios – Piotr Kula Aug 6 '16 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.