• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Blog

Published on Thursday, July 21, 2011

51Degrees.mobi Mobile Detection Outside of IIS

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.

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 http://51degrees.codeplex.com", DataFilePath);
        }
 
    }
}

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

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:

Figure 3 - Changing Target Framework


And the code should now compile.

Example Detectors

Alternatively, if you don’t fancy writing the code, here is a solution that includes two projects demonstrating the code. Both of these programs use the same logic and they both need access to the wurfl files. By default the files are stored in a folder titled App_Data next to the executable.

Command Line Detector:

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

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

Download the source code here. To get it 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.

Comments (0)

Author: Products Team

Categories: Development

Tags: Device Detection , .NET

Tom Holmes
>

Products Team

Other posts by Products Team
Contact author

Name:
Email:
Subject:
Message:
x

Tags

.NET 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Affiliate Marketing Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Developers Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use Display dmexco DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany git repositories Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia m.dot Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native Native Apps NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Non-Mobile NVIDIA Omate On7 OnePlus 5 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV programmatic PS4 Python QMobile QR Codes Redirection Research Reseller Responsive Images Responsive web design RESS Review reviews RIM Ringmark RTB RWD Samsung Scala Screen Screen resolution Screen Size SEO Server Server-side optimisation Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Tips Top 5 TOTW TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 VoLTE VR Wearable Web Web Apps WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE