12

The obvious way to detect Android by server is to search for "Android" string from User-Agent HTTP header. But - I've had complaints that this does not work on some devices (e.g. in my HTC Evo), they are not detected as Android. whatsmyuseragent.com gives for my HTC Evo 3D web browser: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.34 Safari/534.24 . No Android string or version. It could be a security software on the device who alters it it, or HTC-specific issue, not sure about it.

Obviously Android Chrome on same device has another, and better UA: Mozilla/5.0 (Linux; Android 4.0.3; HTC EVO 3D X515m Build/IML74K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19 , but I cannot just hope that e.g. QR Code reader app opens Chrome and not the built-in Android browser, which has invalid UA.

Is there a good trick (javascript call?) to detect reliably Android across devices and browsers?

Edit: looks like same issue with Galaxy S III, same User-Agent string: Android Phone Browser Detection

Community
  • 1
  • 1
JaakL
  • 4,107
  • 5
  • 24
  • 37
  • It does no solve your problem, but maybe you could look for browser resolution together with User-Agent? – RMalke Feb 01 '13 at 09:32
  • 1
    Maybe it would give extra info, but I'd like to use some ready and more or less bulletproof solution instead of starting management of device database with different user ID/resolution/some another header combinations. – JaakL Feb 02 '13 at 12:40

4 Answers4

6

It looks like I had selected "Show Desktop version" in Android web browser settings. Once turned off, the user-agent is with Android inside. This seems to be a feature of ICS, HTC is not to be blamed: google for the wrong user agent I've given to see that it happens on many different phones.

JaakL
  • 4,107
  • 5
  • 24
  • 37
1

For this particular task, I might suggest using wurfl server-side.

You can detect android by doing something very simple:

$requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
if ($requestingDevice->getCapability("device_os") == "Android") {
    //the magic
}
Aaron Saray
  • 1,178
  • 6
  • 19
  • 1
    I am really sceptical if wurfl will use anything better than User-Agent, so it would not possibly work. I tested with t.wurfl.com and it did not reveal device_os – JaakL Mar 12 '13 at 13:26
  • I see what you mean - well my research indicates that if 'orientation' is defined as well as 'ontouchstart' - and it is NOT an iphone, blackberry or windows phone from the user agent, it can be assumed it is probably an android - but unfortunately, that's as accurate as you can get if they're not sending their proper UA :( – Aaron Saray Mar 12 '13 at 13:40
1

Checking the user agent has always been the recommended way to reliably determine the user's device, (at least with default settings anyway). Perhaps you could try making your search more robust? Mozilla suggests searching for the string "Mobi" instead.

crazylpfan
  • 1,038
  • 7
  • 9
  • 2
    I gave a sample of user agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.34 Safari/534.24". There is no Mobi, Android etc – JaakL Mar 12 '13 at 09:38
0

What I understood from your problem is you are getting user agent on some device and on few of the device refusing to give that. so i am assuming that you have a application on android device which is calling your server through some webservice and from that you are trying to extract the user agent. so what solution I can see here is in the android application add the user agent parameter as from code so it will be posted in the HTTP header and from server you can extract that easily

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android");

I had same problem but I resolved in this way.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
  • He states in the question several times that he wants to detect Android devices in a browser, not by consuming a web service. – crazylpfan Mar 12 '13 at 05:30
  • Then write small utility with webview which will call android native build version which confirms that it's android device. Android.os.Build.VERSION. – Dinesh Prajapati Mar 12 '13 at 05:39
  • 1
    The use case is actually for QR code for app download: original problem is that QR reader with web browser is not detected correctly. Any extra utility for that would be pointless. – JaakL Mar 12 '13 at 10:47