Home | Services | Portfolio | Resources | About

Detecting Mobile Devices Using Java

Thanks to the magical powers of the Internet, a kind fan of the PHP scripts for detecting mobile devices ported the code to Java. So for those folks using Java, here is a fantastic new option. And of course -- it's free!

For more detailed information about the caveats of attempting to detect mobile devices based on User Agent strings, please see our article on PHP scripting.

Quick apologies: I'm not as versed in Java as JavaScript and PHP, so any errors introduced in the code are mine. Satish, the gentleman who ported the PHP code to Java, did a great job! Since he sent the code to me, I've made a few modifications which are commented in the source code. Please accept my apologies in advance for any errors. And if you find any, please let me know!

 

Summary

As with PHP and JavaScript code, the basic concept is to do the following:

  1. Instantiate the class in a JSP page. (Some people may wish to do this in a Struts Action class.)
  2. Detect whether the current web site visitor is using a mobile device;
  3. If yes, redirect to a mobile-optimized page. (Or similar type of custom action for mobile devices.)

 

Introducing the "UAgentInfo" Class

The Java class called "com.handinteractive.mobile.UAgentInfo" encapsulates all of the logic for detecting mobile devices. This class is easy to use and its API is highly modularized so that you can detect broad classes of devices (such as smartphones or WAP/WMP-capable devices) and specific platforms (such as the iPhone/iPod Touch, Symbian S60 or BlackBerry).

 

Using the "UAgentInfo" Class

First, instantiate the UAgentInfo object, then call one of its functions. The device detection functions return booleans: true or false. It's as simple as that! Here's an example:

//1. Instantiate the object to do our testing with.
<%@page import="com.handinteractive.mobile.UAgentInfo"%>

<%
  //2. Initialize the browser info variables
  String userAgent = request.getHeader("User-Agent");
  String httpAccept = request.getHeader("Accept");

  //3. Create the UAgentInfo object
  UAgentInfo detector = new UAgentInfo(userAgent, httpAccept);

  //4. Detect whether the visitor is using a mobile device.
  // For example, if it's an iPhone, redirect them to the
  // iPhone-optimized version of your web site.
  if (detector.detectIphoneOrIpod()) {
   response.sendRedirect("http://www.mycompany.com/iphone");
  } else {
   response.sendRedirect("http://www.mycompany.com/default");
  }
%>

 

Introducing MobileESP!

Since the original publication of this article in 2008, we've seen interest grow dramatically for this easy-to-use mobile detection library for web site publishers. As a result, we have spun this code library off into a separate free open source (Apace 2 license) code library called MobileESP. We've continuously expanded the capabilities of the PHP code library as new devices came out. So, for the latest information, including updates, bug fixes, new features, and more, please see the project web site: www.MobileESP.org. Here are a few quick links:

  • Get MobileESP: Download the latest version of the MobileESP code libraries! Learn more.
  • The MobileESP API: See the complete list of mobile detection functions which are largely consistent across supported web technologies (PHP, Java, APS.NET, Ruby, and JavaScript).
  • Live MobileESP API Demos: Want to see how the MobileESP APIs work with your device? Check out the PHP & JavaScript live demos!
  • Download Samples: There are several sets of sample web pages you can download to see how easy it is to integrate MobileESP into your web site. Learn more.
  • License Information: MobileESP is free to use, though donations are greatly appreciated and encouraged. For more information, please visit the Licensing page on the MobileESP web site.

 

Alternatives

MobileESP is easy to implement and generally great if you're primarily concerned about the class of device (e.g., smartphone or not, phone vs. tablet), or the mobile platform (e.g., iPhone or Symbian S60). If you need detailed device information or usage metrics, you may wish to check out WURFL (free!) or HandsetDetection.com.

 

About the Contributor

Our gratitude to Satish K. Nookala, who wrote the initial port of the PHP code to Java. Unfortunately, Satish doesn't have a web site where you can learn more about his great talents, but here's a brief bio in his own words:

"I am a Java programmer and systems analyst working for an Indian software company (right now working at a client location in USA). I have been coding in Java for the last 7 years or so. Also did coding in PERL, PHP, and Visual Basic. I love UNIX and whenever I find some time, I try to explore Linux. :-)"