• 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 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Adtech Affiliate Marketing Afilias Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Developers Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use DeviceAtlas Display dmexco DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany git repositories Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP HTTP Headers Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia m.dot macOS Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native Native Apps NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Nokia 3310 Non-Mobile NVIDIA Omate On7 OnePlus 5 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV programmatic PS4 Publishers Python QMobile QR Codes Redirection Research Reseller Responsive Images Responsive web design RESS Review reviews RIM Ringmark RTB RWD Safari Samsung Scala ScientiaMobile Screen Screen resolution Screen Size SEO Server Server-side optimisation Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore Sitecore version 9 SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Tips Top 5 TOTW Tutorial TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 Video VoLTE VR Wearable Web Web Apps Web content management WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone WURFL Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE