How to use 51Degrees in combination with Native Applications


8/31/2017 12:40 PM

Java Device Data

Benefits of our Native support

At MWC 2016 we announced our Native application support. In this blog I will go through some of the benefits of 51Degrees’ Native support and how you can use the Java SDK to gather the required information for the 51Degrees API.

With the increase in popularity of Android smart phones, targeted advertising on these devices will help make your marketing more efficient and effective. Using our solution we give you the tools to maximise your revenue on devices by understanding the limits of devices using your product, allowing you to adjust what the end-users see, knowing it will be applicable to them.

Google has released a list of all supported devices for the Play Store which can be found here. Native apps reports model information, using our solution you can take this information and using our API uniquely map it to a specific device.

The Java SDK has 3 requests you can make to get information from the device. These are Build.MODEL, Build.DEVICE and Build.BRAND. a simple application retrieving these values are shown below. These requests require no Application permissions so can be ran on every device without causing any issues.

package com.example.lily.myapplication; import; import android.os.Build; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView text = new TextView(this); text.setText( "The following values are retrieved from using the Java SDK." + "\r\n(Build.BRAND) Native Brand: " + Build.BRAND + "\r\n(Build.DEVICE) Native Device: " + Build.DEVICE + "\r\n(Build.MODEL) Native Model: " + Build.MODEL ); setContentView(text); } }

Figure 1: Example code for a simple Android application to retrive basic device info.

The output of this App will be similar to that below (this is for my Galaxy S7 Edge).

Figure 2: Example output from Android code snippet. 

The next step is to use the 51Degrees findProfiles example to find this device in our dataset. This example hones in on a profile based on matching properties. If we attempt to find a profile where:

  • NativeBrand = samsung
  • NativeDevice = hero2lte
  • NativeModel = SM-G935F

There should be 1 value, that matches the Samsung Galaxy S7 Edge. Example code for this is shown below. The majority of the time you should be able to match on the Device and Model alone, however sometimes this may return 2 results. This is why the Brand can be used as a 3rd validator. This unfortunately comes with a minor issue that brands are not always consistant. for example many companies have 3 varients, uppercase, lowercase and title case. I have added the extra

* This Source Code Form is copyright of 51Degrees Mobile Experts Limited. * Copyright © 2015 51Degrees Mobile Experts Limited, 51Degrees, Davidson House, Forbury Square, Reading, Berkshire, RG1 3EU, United Kingdom. * * This Source Code Form is the subject of the following patent * applications, owned by 51Degrees Mobile Experts Limited of 51Degrees Mobile Experts Limited, 51Degrees, Davidson House, Forbury Square, Reading, Berkshire, RG1 3EU, United Kingdom: * European Patent Application No. 13192291.6; and * United States Patent Application Nos. 14/085,223 and 14/085,301. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. * * If a copy of the MPL was not distributed with this file, You can obtain * one at * * This Source Code Form is "Incompatible With Secondary Licenses", as * defined by the Mozilla Public License, v. 2.0. */ package fiftyone.device.example.illustration; import; import; import; import; import; import; import java.util.List; import org.apache.commons.lang3.text.WordUtils; public class FindProfiles2 implements Closeable { // Snippet Start // Device detection provider which takes User-Agents and returns matches. protected final Provider provider; public String enterpriseDataFile = "[PATH]"; protected static Property[] hardwareProperties; /** * Initialises the device detection Provider with the declared enterprise data. * For more data see: * <a href="/compare-data-options">compare data options * </a> * [PATH] to be replaced with file path of Enterprise DataFile. * * @throws IOException can be thrown if there is a problem reading from the * provided data file. */ public FindProfiles2() throws IOException { provider = new Provider(StreamFactory.create(enterpriseDataFile, false)); hardwareProperties = provider.dataSet.getComponent("HardwarePlatform").getProperties(); } /** * Main entry point for this example. * * @param args command line arguments, not used. * @throws 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 for the Galaxy S7 Edge using Native Data samsung|hero2lte|SM-G935F."); FindProfiles2 fp = new FindProfiles2(); String Brand = "samsung"; String Device = "hero2lte"; String Model = "SM-G935F"; try { // Retrieve profiles based on the Native App search using the findProfiles method // Firstly gets a list of all Hardware Profiles that have a NativeBrand = Samsung List<Profile> profiles = fp.provider.dataSet.findProfiles("NativeDevice", Device, null); //Next uses the list created to find which of these Devices have the NativeDevice = heroqlteacg //The list is replaced by the new findProfiles method which narrows down the search, this is repeated for the other //two search criteria. profiles = fp.provider.dataSet.findProfiles("NativeModel", Model, profiles); //There are 3 Lists here that may not always be required, however sometimes the Model and Device is not unique //Requiring further analysis using the brand of the device. This is sometimes written in different ways so it is //advised to attempt the search using, lowercase, upper case and a capitalised first letter. List<Profile> profiles2 = fp.provider.dataSet.findProfiles("NativeBrand", WordUtils.capitalizeFully(Brand), profiles); List<Profile> profiles3 = fp.provider.dataSet.findProfiles("NativeBrand", Brand.toUpperCase(), profiles); profiles = fp.provider.dataSet.findProfiles("NativeBrand", Brand.toLowerCase(), profiles); //The final profiles list has all profiles that have matched the previous criteria, if you are matching on the //Native Key, there should only be 1 profile returned. System.out.println("There are " + (profiles.size() + profiles2.size() + profiles3.size()) + " device(s) that match this key"); //Print all property values for selected profile(s). for (Profile profile : profiles){ System.out.println("ProfileId: " + profile.profileId); for(Property property : hardwareProperties){ System.out.println(property + ": " +profile.getValues(property.getName())); } System.out.println(); } for (Profile profile : profiles2){ System.out.println("ProfileId: " + profile.profileId); for(Property property : hardwareProperties){ System.out.println(property + ": " +profile.getValues(property.getName())); } System.out.println(); } for (Profile profile : profiles3){ System.out.println("ProfileId: " + profile.profileId); for(Property property : hardwareProperties){ System.out.println(property + ": " +profile.getValues(property.getName())); } System.out.println(); } } finally { fp.close(); } } /** * Closes the {@link} 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(); } // Snippet End }

Figure 3: Example on how to adapt the find profiles tutorial to use with native applications

This finally produces an output that gives the full details of the requested device.

Figure 4: Example output from find profiles code