Uniquely identify a mobile device

Posted on 10/21/2013 @ 10:52 AM in #Vanilla .NET by | Feedback | 2705 views

Sometimes you need to identify every device your app is installed on uniquely.
This is for instance important where you have per-device licensing restrictions.

For Win8 store apps,

You can use ASHWID (Application Specific Hardware Identifier). ASHWID will be different app to app and device to device. Any hardware changes to the device will cause the unique id to change. You can also detect minor change vs. major change to build custom level of tolerance in what is considered a change. For instance, ejecting a USB stick is a minor change.

The below code snippet shows you how to get the unique device id,

   1:  HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
   2:  byte[] idBytes = token.Id.ToArray(); 
   3:  string deviceID = idBytes.Select(b => b.ToString()).Aggregate((b, next) => b + "," + next); 


For Windows Phone 7,

You need to add the ID_CAP_IDENTITY_DEVICE capability, and use the following code snippet,

   1:  byte[] deviceID = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId"); 
   2:  string deviceID = Convert.ToBase64String(deviceID); 


For Windows Phone 8,

You could use the Win Phone 7 version, or just use HostInformation.PublisherHostId. HostInformation.PublisherHostId is unique per device/per app publisher, where as the Windows Phone 7 style code is only per device.


For Android, you can use the below code,

   1:  import android.provider.Settings.Secure;
   2:  private String deviceID = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);


For iOS, before iOS7, you could use UDID or MAC or UUID. After iOS7, UDID and MAC are deprecated, and UUID changes every time the app is reinstalled. You should preferably use the below code snippet,

   1:  [[UIDevice currentDevice] identifierForVendor];

Since the ID can change after reinstall, you should probably store it in the key chain using

   1:  [_authenticationTokenKeychain setObject:authenticationToken forKey: kSecValueData];

For PhoneGap, Cordova, All the above with their unique finickiness, are abstracted into a simple one line of code as below,

   1:  var string = device.uuid

Note that you still need to worry about changing ids, storing in keychain etc.

Sound off but keep it civil:

Older comments..