image002

51Degrees.mobi Mobile Detection Outside of IIS

Engineering

7/21/2011 10:51 AM

Device Detection .NET Development
Edit: I wrote this blog short after we released version 1.2. We have now released version 2.0 using our own 51Degrees.mobi that required a few api tweaks. The source code and this article have been rewritten for 2.0; so if you're reading this for referencing to update a 1.x version of the Foundation we highly recommend you upgrade.

51Degrees.mobi has a proven record of fast, reliable detection on ASP.NET website but it has always been restricted to environments within IIS. The 1.2 release has now changed this and detection can now be used offline outside of IIS from any .NET code. To show how, I have made two example projects showing local based detection; a lightweight command line implementation, and a slightly more advanced GUI driven based version. You will also need 51Degrees.mobi Device Data to use with the project. You can download it here.

But first some code:

static void Main(string[] args) { //Creates an instance of the device data detection provider var Provider = FiftyOne.Foundation.Mobile.Detection.Binary.Reader.Create(DataFilePath); //Creates DeviceInfo objects to obtain data from. BaseDeviceInfo deviceInfo = Provider.GetDeviceInfo(Device); Console.WriteLine( String.Format( "The brand name is '{0}'. Press a key to finish.", deviceInfo.GetFirstPropertyValue(Capability))); Console.ReadKey(); }

This example has been up on Codeplex since the 1.2 release. Normally, the Foundation is a complex beast with many configuration options when used with a website, but here only two objects matter: The Provider and DeviceInfo.

The Provider interprets the wurfl files and makes data available for DeviceInfo, the bit which gets all the data for the particular device being queried.

The code is simple enough so let's move on to setting up a project to use the FiftyOne namespace.

Creating a Project

The first step is to download FiftyOne.Foundation.DLL. It can be found in any of the projects on Codeplex or compiled from the source code. After that is has to be referenced from Visual Studio. Create a project (any .NET project will do, but this article uses the console C# template). Right click on References in the Solution Explorer and then click ‘Add Reference…' and navigate to the DLL to add it. You may want copy a version of the DLL to the project directory.

image002

Figure 1 - Adding a reference in Visual Studio 2010

You will now be able to use the FiftyOne namespace. The Reader and DeviceInfo object themselves are in the namespace FiftyOne.Foundation.Mobile.Detection.

There are now two different Reader objects, one for binary and xml data formats. The location of the file is given to the Reader constructor.

The code so far is with file checking:

using System; using System.IO; using System.Web; using FiftyOne.Foundation.Mobile.Detection; using System.Collections.Generic; namespace Detect { public class GetCapability { /// /// Device data file path. /// const string DataFilePath = @"App_Data/51Degrees.mobi-Lite.dat"; /// /// Get data about a particular capability or property of a device. /// /// The name of the Device to query. /// The name of the Capability of Property to retrieve data about. /// public static string AboutDevice(string Device, string Capability) { if (File.Exists(DataFilePath)) { //Creates provider var Provider = FiftyOne.Foundation.Mobile.Detection.Binary.Reader.Create(DataFilePath); //If you would prefer to use the xml format then use this code //var xmlList = new List(); //xmlList.Add(@"App_Data/51Degrees.mobi-Lite.xml.gz"); //var Provider = FiftyOne.Foundation.Mobile.Detection.Xml.Reader.Create(xmlList); //Creates DeviceInfo objects to obtain data from. BaseDeviceInfo deviceInfo = Provider.GetDeviceInfo(Device); //Will be an empty string if either a capability or device wasn't identified. return deviceInfo.GetFirstPropertyValue(Capability); } throw new FileNotFoundException("A data file was not found. You can download a Lite data file from https://archive.codeplex.com/?p=51degrees", DataFilePath); } } }

This code is accepted as being fine in the IDE, but when executed…

image002

Figure 2 - Warnings

It seems odd that FiftyOne.Foundation couldn't be found when it was referenced but this because it depends on System.Web to function. Add ‘using System.Web' and it should work. If System.Web isn't available it is probably because you are using .NET Client Profile Framework. To change it right click on the project file and choose a different target framework:

image002

Figure 3 - Changing Target Framework

And the code should now compile.

Example Detectors

Command Line Detector:

image002

This program takes the first argument as the name of the device and the second as the name of the capability to be found. This program could be used in batch processes or called from other programs. This way detection can be used from other programming languages without using the .NET runtime.

Detection GUI

image002

This program uses the same logic as the command line but presents it in a slightly prettier way.

To get the code working, you will need to download device data. You can get it from Codeplex.

If you having problems retrieving data, bear in mind that the capability needs to be a wurfl one. A complete list can be found here. Also remember that it identifies by useragent. iPad is a nice example of when a device name will be matched, but this isn't always the case. ipad, for instance (lower-case P) will not be identified, as an iPad useragent always has the capital P.

And there it is, an easy way to get reliable device detection without leaving the comfort of your local system.