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


51 Degrees Java API now also available on Scala

Published on Wednesday, March 4, 2015

51 Degrees Java API now also available on Scala

Using 51Degrees device data with Scala and Maven

Scala is becoming increasingly popular as it allows concise code with a strong type system. It can also access all Java libraries as it runs in the the Java Virtual Machine (JVM). In this blog we'll use the 51Degrees Maven Java library to perform device detection using Scala code. The blog assumes the reader is familiar with Maven, Scala and the Eclipse IDE.

Checking Everything's Working with "Hello World"

I've been using the Scala IDE for Eclipse. This IDE allows us to easily get just about any Java library from Maven. See Scala Maven guide to see how to create a project with Maven for Scala.

You should see some code in the new project that looks like this:

package com.degrees.scalademo

 * Hello world!
object HelloApp extends App {
  println( "Hello World!" )

Note that some templates use old Scala (pre 2.11) that won't compile with the latest Scala distributions. Make sure the code inherits from App, not Application. You may need to rename the App super class if it was called Application before.

You should be able to run this program and see 'Hello world!' returned in the console. We'll change this so that the results of a device detection are output using the 51Degrees Maven pacakage.

Adding 51Degrees from Maven

Right click on the project (not the source files), and go to Maven -> Add Dependency.

You can search for 51Degrees or add the ids yourself. The Group Id for the 51Degrees package is com.51degrees. The Artifact Id is 51Degrees.detection.core.

Device Detection Code

Start by creating a standard device detection provider which references 51Degrees device data. Device data can be stored as a single file on disks, loaded into memory, or in the default implementation is embedded into the 51Degrees package. The choice of data source is made when the data set passed to the provided is created. The followed example code initialises the provider using the embedded device data.

val embeddedProvider = new Provider()
val deviceMatch = embeddedProvider.`match`("Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3")
val list = List(
  ).map(p => deviceMatch.getValues(p).toString)

The example will result in the following output to the console.

List(True, 640, 960, Mobile Safari, 5.1)

Specifying a Data File

To use an external data set from a file you need to pass the file path to a factory method:

val fileProvider = new Provider(StreamFactory.create("path/to/datafile"))

This provider can be used in exactly the same way the embedded provider, they are both 'Provider' objects.

Using this your Scala project can use the latest lite or premium device data.

Further Options

It's quick and easy to get 51Degrees device detection working with Scala we almost forgot we'd already done it and it was available as an option for customers. You can download the Scala project.

For a full run down of the classes, properties and methods available see our Java API documentation. To understand the properties returned about your customers devices see our property dictionary.

Comments (0)

Author: Anonym

Categories: Development, Device Data

Tags: Java , Scala



.NET 4G 51Degrees 5G Acer Adform Adtech Advertising Afilias Alcatel Amazon AMP Analysis Analytics Android Apache API Apple Asian Market ASP.NET Asus Blackberry Browser C C# Centro Chrome chromium Client hints Cloud CMS CPU CSS3 Data Data Blog Data File Daydream Design Detection Developers Device Device Data Device Detection Device Intelligence Device Models Device property DeviceAtlas Disney dmexco DotNetNuke Download ebay Ericsson Event Facebook Firefox Foundation Framework Galaxy git repositories Google Google Analytics Google Daydream GPU GSMA Guess HAProxy Hash Trie HTC HTML5 HTTP HTTP Headers Huawei Infinix Ingeniux Internet usage iOS iOS 13 ipad iPadOS iPhone iPhone 11 Java Javascript Kentico LG Liferay LTE m.dot Memory Memory leak Meta Data Microsoft Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Motorola Mozilla MWC MWC 2017 MWC16 Native Apps NET New Release News Nexus NFC NGINX Nokia OnePlus 5 Opera Operating System Oppo Optimisation OS Patent Performance PHP Press Release Price Band programmatic Publishers Python Redirection Research Responsive Images Responsive web design RESS Review RTB RWD Safari Samsung Scala ScientiaMobile SEO Server Server-side optimisation Seznam.cz Sitecore Smart TV Smartphone Smartwatches Sony Swedish Beers Tablet Tencent Testing Tips Tutorial Umbraco Update User Agent User-Agent Valgrind Varnish Varnish Cache Video Vodafone VoLTE Web Web Apps Web content management Webtrekk White Paper Widgets WiFi Windows WURFL Xiaomi Xperia ZTE