Eatsam ul haq
Eatsam ul haq

Reputation: 347

How to Detect Device Type without Spring-Mobile

I know there are multiple questions asked like that but I have tried all of their solutions and none of them worked for me. Before now, we were using Spring-Mobile for detection of Device that is requesting from the browser. But because its support is no longer available, we need to switch to a different strategy.

I have seen different solutions using HttpServletRequest to detect the device type (if it is a mobile, Tablet or Desktop) from its Header through User-Agent. Following is the code I have used to detect Mobile device using this approach:

String userAgent = request.getHeader("User-Agent");
System.out.println("User-Agent => " + userAgent.contains("Mobi")); //prints false

I have tried this by sending request from browser through inspect element and also by using original Mobile Device but in both cases, it return false.

Mobileesp.org:

I also saw a solution that used mobileesp.org but I am not quite sure If I am implementing it correctly in my application or not. I only copied its folder of Java and pasted in my application. There, I called its constructor and tried to detect mobile devices but all the methods returned false on mobile detection. Again, I tested that on real device and on browser as well. Following is my code:

        String userAgent = request.getHeader("User-Agent"); // request => HttpServletRequest (javax.servlet.http.HttpServletRequest)
        UAgentInfo user = new UAgentInfo(userAgent, "");

        // All of Below methods are returning False
        System.out.println(user.detectAndroidPhone());
        System.out.println(user.isAndroidPhone);
        System.out.println(user.isIphone);
        System.out.println(user.isMobilePhone);
        System.out.println(user.detectAndroid());
        System.out.println(user.detectSmartphone());
        System.out.println(user.detectAndroidTablet());
        System.out.println(user.detectMobileQuick());
        System.out.println(user.detectMobileLong());
        System.out.println(user.isAndroid);

Your help is really appreciated. Thanks!

Upvotes: 3

Views: 2318

Answers (0)

Related Questions