Getting Unique Device ID for iOS, Android and Windows with Xamarin

Over a year ago, I wrote a quick tip on getting device Unique ID for Windows and Windows Phone (8 & 7). Lately, at RedBit, we have been doing quite a bit of iOS, Android and Windows projects using Xamarin and recently put two articles up on our learnings, Cross Platform Animations using Xamarin Forms and GZip Compression with Xamarin Forms. For different reasons, we needed to use a unique ID to identify devices and getting the device ID on each platform is obviously different.

For iOS you can use the UIDevice.CurrentDevice.IdentifierForVendor which can be called from a Xamarin iOS or Xamarin Forms app.

UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString();

For Android, again it’s a different API but note, that you need API Level 9 or higher for this to work. It is called as follows from Xamarin Android or Xamarin Forms

Android.OS.Build.Serial;

For Windows you can reference this post I wrote a while back Getting Device ID on Windows & Windows Phone.

At RedBit we have an internal library that we use and here is an excerpt of our RedBit.Mobile.Hardware class

public class Hardware
{
    private static Hardware _Default;
    public static Hardware Default
    {
        get
        {
            return _Default ?? (_Default = new Hardware());
        }
    }

    ///

    /// Gets a device unique identifier
    ///

public string DeviceId { get { return GetDeviceIdInternal(); } } ///
/// Gets a device unique identifier depending on the platform ///

/// string representing the unique id private string GetDeviceIdInternal() { var id = default(string); #if __IOS__ id = UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString(); #elif WINDOWS_PHONE id = Windows.Phone.System.Analytics.HostInformation.PublisherHostId; #else id = Android.OS.Build.Serial; #endif return id; } }

I’ve put a sample up on GitHub that outputs the device ID for iOS, Android and Windows. Here is the output on iOS.

deviceId-ios

As always, comments welcome!

Using GZip Compression with Xamarin Forms

In a recent project the RedBit team worked on, we decided to use Xamarin.Form to  build an app for Windows Phone, iOS and Android. Xamarin Forms is still a v1 product but the Xamarin team is rapidly releasing updates to fill in some gaps. As a developer you still have the opportunity to extend Xamarin Forms, for example by building custom renderers. We’ll have a series of blog posts on some learnings from the team to help extend Xamarin.Forms and meet our client’s requirements.

For this first post we’ll be looking at how to implement GZip Compression across iOS, Android and Windows. The benefit to using GZip is there is less data going over the wire reducing mobile data consumption. In the US it’s not much of a problem to send large amounts of data because of unlimited data plans, but in Canada (and other parts of the world) data is not so cheap. In terms of compression, here is a chart showing the difference in data size making a request to http://en.wikipedia.org/wiki/Gzip.

To read more about how to use GZip Compression with Xamarin Forms head over to the RedBit post to see the entire walkthrough.

Imagine Cup Student DevCamps Followup

imagineCamp2Over the weekend I was at University of Ottawa presenting to the students in the Electrical Engineering and Computer Science program talking about building for Windows and Windows Phone and the best way to leverage and re-use code across the different platforms.  It’s always exciting to meeting and share with students my experience in software development and helping them along in their endeavours/careers.  You can read more about the event on the RedBit blog 

As a follow up here are bunch of resources and links to things I mentioned during the two days

If you attended (or if you didn’t, heard it was mid terms) feel free to connect with me here or via twitter to ask questions on developing for both Windows and Windows Phone or any other questions in general.

Next up will be Queens University register here and another one end of March in the Kitchener Waterloo Region (will post details on that soon).

Quick Tip: Getting Device ID on Windows & Windows Phone

When building out apps, there are some situations where you are required to get a unique ID of the device whether it be for analytic data or to potentially limit devices from accessing some private services.

This quick tip will show you how to get the device ID on different platforms such as Windows Phone 7, Windows Phone 8 and Windows 8.

Windows Phone 7

To get device ID on Windows Phone 7 you can use the following

private string m_DeviceUniqueId;
public string DeviceId
{
    get
    {
        if (m_DeviceUniqueId == null)
        {
            object val;
            if (Microsoft.Phone.Info.DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out val))
                m_DeviceUniqueId = Convert.ToBase64String((val as byte[]));
        }
        return m_DeviceUniqueId;
    }
}

Windows Phone 8

Getting device ID on Windows Phone 8 is slightly different the the previous version and can be accomplished with the following

private string m_DeviceUniqueId;
public string DeviceId
{
    get
    {
        if (m_DeviceUniqueId == null)
        {
            m_DeviceUniqueId = Windows.Phone.System.Analytics.HostInformation.PublisherHostId;
        }
        return m_DeviceUniqueId;
    }
}

Windows 8

On Windows 8, it’s yet another API to get the device ID and can be accomplished with the following

private string m_DeviceUniqueId;
public string DeviceId
{
    get
    {
        if (m_DeviceUniqueId == null)
        {
            var token = Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null);
            var hardwareId = token.Id;
            var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);

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

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

Gotchas

If you are using this code to get ID with your apps, be aware that the ID returned will be different in every app.  For example, if you have APP1 and APP2, APP1 may return 12345 as the device ID and APP2 may return 09876 as the device ID even though they are running on the same device.

Enjoy!

Sharing Code Part IV: Portable Class Libraries

In the last three posts, I have gone through various ways to share common code across the various Windows based platforms. So far in this series of articles in sharing code we have looked at

  1. Sharing Code Part I: Common Source File (source code download)
  2. Sharing Code Part II: Partial Classes (source code download)
  3. Sharing Code Part III: Conditional Compile (source code download)

In this final part, we’ll look at using a Portable Class Library to share the Math2 class across the different platforms instead of the C# source code file. Personally, I prefer sharing the source code file across the platforms and not sharing an assembly as it gives a little more control. Of course, if I’m building out an API or SDK for external developers to use, a Portable Class Library is the way to go. Continue reading Sharing Code Part IV: Portable Class Libraries

Sharing Code Part III: Conditional Compile

In the first part of Sharing Code series we looked at sharing a common class source file across three different platforms, and in the second part we continued with using partial classes to separate the code out. In this third part of the series, we’ll look at using conditional compiles in our code and continue working on the sample code from the previous articles. Continue reading Sharing Code Part III: Conditional Compile

Sharing Code Part II: Partial Classes

In the first part of Sharing Code series we looked at sharing a common class source file across three different platforms. Continuing the series on Sharing Code across various platforms, we’ll look at leveraging partial classes to help share code across our many apps on Windows 8, Windows Phone 7 & Windows Phone 8 using the same source code from Part I.

What is a Partial Classes?

Essentially a partial class allows you to separate a class or structure into two or more separate files. Personally, I use this technique to hide any specific platform code from other platforms.

Continue reading Sharing Code Part II: Partial Classes

Sharing Code Part I: Common Source Files

In this article, I’ll go through how to share source code files between Windows Phone 8, Windows Phone 7.1 and Windows 8.  In this sample we’ll go through how to share a Math class between two separate Windows Phone projects.

Creating the Projects

First thing is to create two Visual Studio 2012 Projects one for Windows Phone 7.1, 8 and Windows 8.

Windows Phone 7.1 Project

Create a Windows Phone Project and call it WP7_App and uncheck ‘Create Directory for solution’.

Continue reading Sharing Code Part I: Common Source Files

Sharing Code Across Platforms: Introduction

In 2006 I did a presentation on that focused on .NET Compact Framework for Desktop Developers where the core focus was on how to leverage existing code from the full desktop .NET Framework and .NET Compact Framework.

Well seven years later (yes I’ve been doing this a long time!), some of those techniques are still usable with all the different mobile platforms available and as a developer you definitely want to leverage as much code as possible.

In the next series of articles, I will go through some techniques on sharing code across different platforms including Windows Phone 7, Windows Phone 8 and Windows 8. You can use some of the techniques also on iOS and Android using Xamarin.

The following topics will be covered:

  1. Sharing Code Part I: Common Source File (source code download)
  2. Sharing Code Part II: Partial Classes (source code download)
  3. Sharing Code Part III: Conditional Compile (source code download)
  4. Sharing Code Part IV: Portable Class Libraries (source code download)

I have personally used these techniques with various products we have built plus customer work.  By the end of the series, hopefully you will have a good understanding on how to leverage your code across different platforms using C#.

Note that the samples being used are not using anything like MVVM or other frameworks as it’s something I was using my 11yr old son on using programming to solve some math homework he had and I wanted to keep it as simple as possible.

Windows Phone & HTML5 Sample App Summary

I recently wrote a four part article about how to build a simple count down timer app using HTML5 and Windows Phone.  The articles covered getting started with HTML5 and Windows Phone, multiple resolutions, using the WebBrowser control to ‘talk’ to web page and finally getting ready for Windows Phone Store certification.

Here is the result of the articles.

finalImage

Here is a list of the articles available for easy reference

  1. XBoxReveal HTML5 Windows Phone Sample
  2. XBoxReveal HTML5 Windows Phone Sample: Multiple Resolutions
  3. XBoxReveal HTML5 Windows Phone Sample: WebBrowser Control
  4. XBoxReveal HTML5 Windows Phone Sample: Store Certification

As for the source code, if you want to run it yourself you can download the #xboxreveal source code from GitHub.

Hope it was useful and please leave comments or questions!