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

Java API

What's Covered

This tutorial illustrates how to use the findProfiles function to return a list of matching profiles for a given property value pair. The following steps are covered:

  • How to find all the mobile device profiles.
  • Find all the mobile profiles with a screen size of 1080 pixels.
  • Do the same for non-mobile devices, and display how many have that screen size.

Code and Explanation

Full Source File
    // Device detection provider which takes User-Agents and returns matches.
    protected final Provider provider;

    /**
     * Initialises the device detection Provider with the included Lite data
     * file. For more data see:
     * <a href="https://51degrees.com/compare-data-options">compare data options
     * </a>
     *
     * @throws IOException can be thrown if there is a problem reading from the
     * provided data file.
     */
    public FindProfiles() throws IOException {
        provider = new Provider(StreamFactory.create(Shared.getLitePatternV32(), false));
    }

    /**
     * Main entry point for this example. For the value "IsMobile is
     * True and False:
     * <ol>
     * <li>invokes
     * {@code Provider.DataSet.FindProfiles(java.lang.String, java.lang.String, List<Profile>)}.
     * Profiles are then stored in the {@link List<Profile>} list and can be
     * accessed using the {@code profiles.get(int index)} method.
     * <li>prints results.
     * </ol>
     *
     * @param args command line arguments, not used.
     * @throws java.io.IOException if there is a problem accessing the data file
     * that will be used for device detection.
     */
    public static void main(String[] args) throws IOException {
        System.out.println("Starting FindProfiles example.");
        FindProfiles fp = new FindProfiles();
        try {
            // Retrieve all mobile profiles from the data set.
            List<Profile> profiles = fp.provider.dataSet.findProfiles("IsMobile", "True", null);
            System.out.println("There are "
                    + profiles.size()
                    + " mobile profiles in the "
                    + fp.provider.dataSet.getName()
                    + " data set.");
            // Find how many have a screen width of 1080 pixels.
            profiles = fp.provider.dataSet.findProfiles("ScreenPixelsWidth", "1080",
                    profiles);
            System.out.println(profiles.size()
            + " of them have a screen width of 1080 pixels.");

            // Retrieve all non-mobile profiles from the data set.
            profiles = fp.provider.dataSet.findProfiles("IsMobile", "False", null);
            System.out.println("There are "
                    + profiles.size()
                    + " mobile profiles in the "
                    + fp.provider.dataSet.getName()
                    + " data set.");
            // Find how many have a screen width of 1080 pixels.
            profiles = fp.provider.dataSet.findProfiles("ScreenPixelsWidth", "1080",
                    profiles);
            System.out.println(profiles.size()
                    + " of them have a screen width of 1080 pixels.");
        } finally {
            fp.close();
        }
    }

    /**
     * Closes the {@link fiftyone.mobile.detection.Dataset} by releasing data
     * file readers and freeing the data file from locks. This method should
     * only be used when the {@code Dataset} is no longer required, i.e. when
     * device detection functionality is no longer required, or the data file
     * needs to be freed.
     *
     * @throws IOException if there is a problem accessing the data file.
     */
    @Override
    public void close() throws IOException {
        provider.dataSet.close();
    }

Full Source File

Summary

This tutorial covered how to work with the 51Degrees device Data Model to obtain a subset of device profiles that meet several conditions. Each condition in this case is a specific value for a chosen property. The result should be read as follows: this is a subset of device profiles where property1 has value1 and property2 has value2 and property3 has value3 and so on.

One real world application for this is building a set of interlinked menus where each choice will narrow down the available options for subsequent choices. This can be useful when part of the API is exposed to the end user:

  • An ad agency could benefit from allowing their clients to target specific devices based on pre-defined criteria, such as DeviceType, ScreenInchesWidth or even PriceBand.
  • A program that uses a 51Degrees API to generate/augment reports could be enhanced to allow the user to choose the report parameters. By providing a finite set of choices and avoiding arbitrary input the chance of errors occurring is reduced and user experience improved.

The Lite data file contains considerably fewer properties and values than the Premium or Enterprise files, making the usefulness of this tutorial slightly harder to appreciate. With Premium and Enterprise data files there are many more possibilities for creating subsets of device profiles. For example: using Premium device data you can generate a subset of all 'Samsung' 'Smartphone' devices. Or get all the properties of a specific HTC model.