Detecting Device Type in a web application
Asked Answered
B

8

38

We have a Java based application where in we want to detect the device type(mobile or desktop) for the device that is sending the request.

How is it possible?

Bailment answered 15/12, 2011 at 4:34 Comment(2)
See duplicate question and answer: #1005653Jollify
The concept is exactly the same. You check the user-agent.Jollify
Z
51

You'll have to read the User-Agent header from the request and decide on that.

In vanilla servlet apps, a crude way of doing it is:

public void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
  if(request.getHeader("User-Agent").contains("Mobi")) {
    //you're in mobile land
  } else {
    //nope, this is probably a desktop
  }
}
Zoezoeller answered 15/12, 2011 at 4:48 Comment(5)
Latest and better way on Mozilla site - developer.mozilla.org/en-US/docs/…Spina
Another, exhaustive, way - code.google.com/p/mobileesp/source/browse/Java/UAgentInfo.javaSpina
Another good library is bitwalker useragentutils: github.com/HaraldWalker/user-agent-utils which provides enums for BrowserType, DeviceType, OperatingSystem, Manufacturer, Version. It can be installed via Maven, and (perhaps most importantly) is maintained.Condensate
As per Mozilla: "In summary, we recommend looking for the string “Mobi” anywhere in the User Agent to detect a mobile device." Refer: developer.mozilla.org/en-US/docs/Web/HTTP/…Alleyne
This is not working for me and I am always getting fasle. any idea?Flawed
H
15

You can get device information by parsing http header

String browserType = request.getHeader("User-Agent");

You should parse browserType to get device type

This may help

  public String  getBrowserInfo( String Information )
  {
    String browsername = "";
    String browserversion = "";
    String browser = Information;
    if (browser.contains("MSIE"))
    {
      String subsString = browser.substring(browser.indexOf("MSIE"));
      String info[] = (subsString.split(";")[0]).split(" ");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Firefox"))
    {

      String subsString = browser.substring(browser.indexOf("Firefox"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Chrome"))
    {

      String subsString = browser.substring(browser.indexOf("Chrome"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Opera"))
    {

      String subsString = browser.substring(browser.indexOf("Opera"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    } else if (browser.contains("Safari"))
    {

      String subsString = browser.substring(browser.indexOf("Safari"));
      String info[] = (subsString.split(" ")[0]).split("/");
      browsername = info[0];
      browserversion = info[1];
    }
    return browsername + "-" + browserversion;
  }
Humanitarianism answered 15/12, 2011 at 4:46 Comment(4)
Can you provide me more information on this. I tried to go through the data that Http Header provides but did not find the required data.Bailment
can someone fake the User-Agent when sending request to server? say they hit curl command but fake that it is coming from Mobile?Bumkin
@Bumkin It is possible to change or "fake" what your web browser sends as its user agent. Some mobile web browsers will let you change what the browser identifies itself as (ie "Mobile Mode" or "Desktop Mode") in order to access certain websites that only allow desktop computers. If you change this setting, the user agent is what is affected.Podophyllin
there are plugins for both Chrome and Firefox and probably also others that allows you to write ANY useragent, even predefined "ipad" and "searchbot" useragents... not to be trusted, but good for statisticsTubing
B
6

You can try to use Spring Mobile. There are convenient classes to solve that.

Device currentDevice = DeviceUtils.getCurrentDevice(servletRequest);
if(currentDevice.isMobile()) { /* Mobile */ }
if(currentDevice.isTablet()) { /* Tablet */ }
if(currentDevice.isNormal()) { /* Desktop */ }
Billiards answered 14/1, 2018 at 0:20 Comment(1)
Spring-Mobile is no longer supported and has reached its deadlineFlawed
I
5

You could get a 3rd party software solution. There are plenty of Open Source ones out there. I've used 51Degrees.mobi's Java solution before now (and have also worked on their open source C solution). Follow that link and hit the download button. It's relatively easy to get up and running.

Idonna answered 17/9, 2012 at 9:56 Comment(0)
S
3

You can try this lib I think, yauaa can detect a user agent string is which device/software

https://github.com/nielsbasjes/yauaa

Singhal answered 10/4, 2017 at 10:13 Comment(0)
J
0

You can check the User-Agent HTTP header on the HttpServletRequest object.

Ref: http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#user-agent

Jollify answered 15/12, 2011 at 4:45 Comment(1)
I think it will be better to put it that duplicate link on comment under the question itself. Right now it suggest that your answer is the duplicate.Midcourse
C
0

For Java 51Degrees has a free API on GitHub https://github.com/51Degrees/Java-Device-Detection, it is undated regularly and should be able to cover all the basic detentions that you need.

There is a tutorial how to use the API for Device Type detection here https://51degrees.com/Developers/Documentation/APIs/Java-V32/Tutorials/Web-App

There is also a free cloud version that offers the DeviceType Property (if you want more than just IsMobile) you can find the details on this on the 51Degrees website. You will need to "purchase" the free cloud through the website, but it does not ask for bank details and is completely free. You can also use JavaScript and other integration dependant on your requirements.

Disclosure: I work at 51Degrees.

Communicate answered 12/4, 2017 at 9:34 Comment(0)
M
0

There's also http://openddr.mobi/, Open Source Device Detection for

  • Java
  • C#
  • VB.net

based on the W3C Device Description Repository.

OpenDDR is comparable to device repositories by 51Degrees, DeviceAtlas or ScientiaMobile/WURFL, but unlike those proprietary, closed source product, it's poen source and free to use.

Mcdermott answered 27/10, 2020 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.