An Alternative:
I have some code that does a comparison between WebView pixels and Native View pixels… I am not absolutely sure this gets you hardware DPI but, in my experience using it, I think it does (I would appreciate someone correcting me if I am wrong)
Java:
AppiumDriver appDriver = <something passed into the method... can be AndroidDriver or IOSDriver at runtime>
Set<String> contextNames = ((AppiumDriver) appDriver).getContextHandles();
for (String contextName : contextNames) {
if (contextName.contains("NATIVE")) {
((AppiumDriver<MobileElement>) appDriver).context(contextName);
}
}
double screenWidth = appDriver.manage().window().getSize().getWidth();
double screenHeight = appDriver.manage().window().getSize().getHeight();
Good Luck!