admin管理员组

文章数量:1391836

Here is the code to recognize the tablet devices using the spring mobile dependency but it is not working and kept returning null, do you know any solution?


    public static boolean isTablet(HttpServletRequest request) {
        var res = ResolverUtils.isTablet(DeviceUtils.getCurrentDevice(request), getSitePreference(request));
        return res;
    }
    
    private static SitePreference getSitePreference(HttpServletRequest request) {
        String param = request.getParameter("site_preference");

        if ("normal".equalsIgnoreCase(param)) {
            return SitePreference.NORMAL;
        } else if ("mobile".equalsIgnoreCase(param)) {
            return SitePreference.MOBILE;
        } else if ("tablet".equalsIgnoreCase(param)) {
            return SitePreference.TABLET;
        }

        return null;
    }

Also I tried com.github.ua-parser but it is not recognizing the tablet devices too, here is my code for this one too :


    private static final Parser UA_PARSER = new Parser();

    
    public static boolean isTablet(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return false; // No User-Agent means we cannot detect the device
        }

        Client client = UA_PARSER.parse(userAgent);
        Device device = client.device;

        // Check if the device is a tablet (common pattern)
        return device.family != null && device.family.toLowerCase().contains("tablet");
    }

本文标签: htmlHow to recognise tablet devices in browsers in javaStack Overflow