B4J Code Snippet Detect mobile device

* Detect mobile device

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    If req.GetHeader("User-Agent").Contains("Mobi") Then
        Log("Mobile Device")
        resp.SendRedirect("/mobile")
    Else
        Log("Not Mobile")
      
    End If
End Sub
 
Last edited:

Sandman

Expert
Licensed User
Longtime User
A small note: Considering that the header User-Agent can be set to be anything, this might not get the expected results in all cases.
 

AnandGupta

Expert
Licensed User
Longtime User
Considering that the header User-Agent can be set to be anything
Believe me, normal users do not care nor know user-agent, neither they care about privacy blah blah.
We the geeks are burning ourselves on it. Not bad, but simple code like this works for 95% of our users.

This I found out after making few simple apps for our users.
 

ivanomonti

Expert
Licensed User
Longtime User
* Rileva dispositivo mobile

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    If req.GetHeader("User-Agent").Contains("Mobi") Then
        Registro("Dispositivo mobile")
        resp.SendRedirect("/mobile")
    Altro
        Registro("Non mobile")
     
    Finisci se
Fine Sub[/CODICE]
[/QUOTE]
hi, example project!!
 
Top