Getting Started - Version 3
Up and running in four easy steps.
- Download the API .
- Unzip the file into a directory of your choice.
- Add "51Degrees.detection.core.jar" located in "dist" folder to your project.
- To use device detection:
import fiftyone.mobile.detection.Match ; import fiftyone.mobile.detection.Provider ; import fiftyone.mobile.detection.factories.StreamFactory ; import java.io.IOException ; public class Example { //FiftyOne Provider object - used to access data and perform match. private static Provider provider ; //Path to FiftyOne device data file. private static final String PATH_TO_DAT_FILE = "path\\to\\data_file.dat" ; // User agent string of the device in question. private static final String USER_AGENT = "Mozilla/5.0 (BlackBerry; U; " + "BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) " + "Version/7.1.0.346 Mobile Safari/534.11+" ; public static void main ( String [] args ) throws IOException { // Initialise provider with the data file to use. // 'false' flag indicates the data file is not temporary. provider = new Provider ( StreamFactory . create ( PATH_TO_DAT_FILE , false )); // Match object contains results of detection. Match match = provider . match ( USER_AGENT ); // Fetch specific property, in this case 'IsMobile'. boolean isMobile = match . getValues ( "IsMobile" ). toBool (); // Implement some logic bbased on detection results. if ( isMobile ) System . out . println ( "This device is mobile." ); else System . out . println ( "This device is not mobile." ); System . out . println ( match . getValues ( "BrowserName" )); } }
Initialising Provider
Provider object exposes several match methods that are used to perform device detection. Detection information is returned in a Match object. Provider requires a DataSet to be passes to it at initialisation. DataSet objects provide access to device data from the data files and can be constructed using two factories:
Memory Factory
All data from the data file is read into memory at initialisation. Detection time is faster but the amount of memory used is significantly greater than with StreamFactory. To instantiate Provider object using this method:
Provider p ; p = new Provider ( MemoryFactory . create ( "Path/to/data/file.dat" ));
StreamFactory
A limited amount of information is read into memory at initialisation. Match will actively use the data file to perform detection and read device information. Detection is slower as it involves disk I/O operations but the amount of memory required is very small. This method is ideal for environments with limited resources. To instantiate Provider object using this method:
Provider p ; p = new Provider ( StreamFactory . create ( "Path/to/data/file.dat" , false ));
Getting Started - Version 2
Complete the following five steps to integrate mobile device detection into your Java application:
Step 1 - Download "51degrees.mobi_Detection_Java_*.zip" from SourceForge.
Step 2 - Unzip the file into a directory of your choice.
Step 3 - locate "51Degrees.mobi.detection.core.jar" in the "dist" directory and add the JAR to your project.
Step 4 - Add the following code to your Java file:
import fiftyone.mobile.detection.* ; import fiftyone.mobile.detection.binary.* ;
Step 5 - You are now ready to detect devices. Use the following code as a reference when implementing a solution within your own code:
public static main ( String [] args ) throws BinaryException , NullPointerException { // Create a Provider object Provider p = Reader . create (); // Provider p = Reader.create(" PATH_TO_PREMIUM_DATA "); // Read in a User Agent String BaseDeviceInfo b = p . getDeviceInfo (< INSERT_USER_AGENT_STRING_HERE >); // Get the value of a property String result = b . getFirstPropertyValue ( "IsMobile" ); // Check the property value if ( result . equals ( "True" )){ system . out . println ( "This is mobile" ); } if ( result . equals ( "False" )){ system . out . println ( "This is not mobile" ); } }