I created Index.php controller for that at app/code/Vendor/Modulename/Controller/Index/ and add this below code :
<?php
namespace VendorModulenameControllerIndex;
class Index extends MagentoFrameworkAppActionAction {
/**
* @var MagentoFrameworkViewResultPageFactory
*/
protected $resultPageFactory;
/**
* @var MagentoFrameworkHTTPHeader
*/
protected $httpHeader;
/**
* @param MagentoFrameworkAppActionContext $context
* @param MagentoFrameworkHTTPHeader $httpHeader
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkHTTPHeader $httpHeader,
MagentoFrameworkViewResultPageFactory $resultPageFactory
) {
$this->httpHeader = $httpHeader;
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Detect Mobile view or Desktop View
*
* @return void
*/
public function execute() {
$resultPage = $this->resultPageFactory->create();
$userAgent = $this->httpHeader->getHttpUserAgent();
$isMobile = Zend_Http_UserAgent_Mobile::match($userAgent, $_SERVER);
if ($isMobile) {
$resultPage->getConfig()->getTitle()->prepend(__("Mobile View")); // Mobile view logic add here
} else {
$resultPage->getConfig()->getTitle()->prepend(__("Desktop View")); // Desktop view logic add here
}
return $resultPage;
}
}
You can add this above code in your controller or any file. You need to inject MagentoFrameworkHTTPHeader class into your construct.

