Digital Agencies

Leading digital agencies need to keep up to date with the latest trends and tool sets to ensure your clients get the best results can be a challenge when you are busy managing the day to day demand from clients.

Using 51Degrees you can adapt online content to maximise the potential of online conversions and steer the end user purchase path.

Plugin us into yours and your clients’ web platforms and see a more granular level of detail about the devices used for clients site.

CMS

51Degrees provides the fastest and most accurate device information, including 217 device properties such as retail price, vendor, device age, screen size and more to allow more granular analytics and better content optimisation

APIs

Property Dictionary

Browse the full list of properties available in 51Degrees device detection solutions. Find out what values are supported in the Lite, Premium and Enterprise data sets, including full descriptions and advice on how they can be used.

Mobile Emulators

Blog

Published on Monday, August 21, 2017

How to Use the New Hash Trie algorithm in Java

51Degrees explains how to call the C Hash Trie API within Java.

Introduction

The new Hash Trie algorithm is by far the fastest method of detecting devices from their User-Agents (see the blog post for details). To retain this speed in Java, it is necessary to interface with the compiled C/C++ to keep all the heavy lifting in the C world and away from Java objects and types. This can be done very simply with a SWIG generated wrapper.

This blog will go through the steps required to create an illustrative example of how to call the C Hash Trie API within Java. The following assumes a sufficent knowledge of:

Compiling C code;

Java programming;

Before starting, you will need to get hold of the new Hash Trie source which is available on GitHub. You will also need to download the Hash Trie Device-Data file, access to the file can be obtained by signing up for a free subscription.

The steps involved are:

Generate the SWIG wrapper;

Compile the library;

Build the JAR.

And that's all there is.

Generate the SWIG Wrapper

If it is not already, then you will need to install SWIG on your system. This can be done through apt in Linux, and the Windows version can be found on the SWIG downloads page.

First clone the Device-Detection repository, open a terminal and move to the src/trie directory. Generating the wrapper files is then done with the command:

This will generate the 51Degrees_java.cpp which will be compiled in the library, the Java classes as defined in 51Degrees.i, and a JNI class for interfacing with the C++ layer.

Compile the Library

Attached at the bottom of this article is a template project to get started, it contains the scripts to build the library in both Windows and Linux, and a template Java project. Extract the folder to the Device-Detection directory. Now in a terminal (Visual Studio command prompt in Windows, or any Bash shell in Linux), go to the newly extracted Device-Detection/java directory.

Then build the shared library to the resources directory with the command:

The above code in a project will first get a match for the User-Agent string defined by mobileUserAgent. It will then print the value of the IsMobile property.

Including in a Project

The JAR can now be set as a dependancy in an external project by. Then, by using code similar to that in the above example, any Java project can benefit from the new, faster, Hash Trie device detection API.