Check out the latest documentation.

What's Covered

This tutorial illustrates how to return a match for a Device Id. It shows how to first get the Device Id from a match, then get a match for that Device Id and return the value of the IsMobile property. This can be useful to look at devices that have already been matched at a previous date.

Code and Explanation

Match for device id example of using 51Degrees device detection. The example shows how to:

  1. Set the data set for the 51Degrees detector

    												
    string fileName = args[0];
    DataSet dataSet = StreamFactory.Create(fileName, false);
    
    												

  • Instantiate the 51Degrees device detection provider with these settings

    									
    Provider provider = new Provider(dataSet);
    
    									
  • Produce a match for a single device id

    									
    match = provider.MatchForDeviceId(deviceId);
    
    									
  • Extract the value of the IsMobile property

    									
    IsMobile = match["IsMobile"].ToString();
    
    									
    This tutorial assumes you are building this from within the 51Degrees Visual Studio solution. Running the executable produced inside Visual Studio will ensure all the command line arguments are preset correctly. If you are running outside of Visual Studio, make sure to add the path to a 51Degrees data file as an argument.

    Full Source File
    												
            public static void Run(string fileName)
            {
                // DataSet is the object used to interact with the data file.
                // StreamFactory creates Dataset with pool of binary readers to 
                // perform device lookup using file on disk.
                DataSet dataSet = StreamFactory.Create(fileName, false);
    
                // Provides access to device detection functions.
                Provider provider = new Provider(dataSet);
    
                // Used to store and access detection results.
                Match match;
    
                // Device id string of an iPhone mobile device.
                string mobileDeviceId = "12280-48866-24305-18092";
    
                // Device id string of Firefox Web browser version 41 on desktop.
                string desktopDeviceId = "15364-21460-53251-18092";
    
                // Device id string of a MediaHub device.
                string mediaHubDeviceId = "41231-46303-24154-18092";
    
                Console.WriteLine("Starting Match For Device Id Example.");
    
                //Carries out a match for a mobile device id.
                match = provider.MatchForDeviceId(mobileDeviceId);
                Console.WriteLine("\nMobile Device Id: " + mobileDeviceId);
                Console.WriteLine("   IsMobile: " + match["IsMobile"]);
    
                // Carries out a match for a desktop device id.
                match = provider.MatchForDeviceId(desktopDeviceId);
                Console.WriteLine("\nDesktop Device Id: " + desktopDeviceId);
                Console.WriteLine("   IsMobile: " + match["IsMobile"]);
    
                // Carries out a match for a MediaHub device id.
                match = provider.MatchForDeviceId(mediaHubDeviceId);
                Console.WriteLine("\nMediaHub Device Id: " + mediaHubDeviceId);
                Console.WriteLine("   IsMobile: " + match["IsMobile"]);
    
                // Finally close the dataset, releasing resources and file locks.
                dataSet.Dispose();
            }
    
    
    												
    Full Source File

  • Summary

    In this tutorial you have seen how to use the detector to retrieve the IsMobile property for a pre-defined Device ID string. The example can easily be modified to retrieve the value of any other property. Premium and Enterprise data files provide considerably more properties such as IsCrawler , PriceBand , HardwareVendor and ScreenInchesWidth . For a full list of properties and the data files they exist in please see the Property Dictionary .