Every HTTP request for a web page made by the BlackBerry® Browser, or an embedded web view (BrowserField) contains a collection of HTTP headers. One of these headers is the User Agent. The User Agent header contains information that identifies various characteristics about the BlackBerry device making the web request. This article presents an overview of the User Agent and provides examples on how you use this information to give your BlackBerry device users an optimal mobile browsing experience based on which BlackBerry device they are using.
User Agent in BlackBerry 10
The format of the User Agent used by BlackBerry® 10 is similar to the following:
Mozilla/5.0 (BB10; <Device Type>) AppleWebKit/537.10+ (KHTML, like Gecko) Version/<BB Version #> Mobile Safari/537.10+
As shown in this specific example, the user agent identifies three important pieces of information:
For the BlackBerry Z10 device, this will be "Touch"
For the BlackBerry Q10 device, this will be "Kbd"
User Agent in BlackBerry Tablet OS
The format of the user agent of the BlackBerry® Tablet OS is similar to the following:
Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.0.0; en-US) AppleWebKit/535.8+ (KHTML, like Gecko) Version/7.2.0.0 Safari/535.8+
User Agent in BlackBerry 6 and BlackBerry 7
The format of the User Agent used by BlackBerry® 6 and BlackBerry® 7 is similar to the following:
Mozilla/5.0 (BlackBerry; U; BlackBerry AAAA; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/X.X.X.X Mobile Safari/534.11+
As shown in this specific example, the user agent identifies three important pieces of information:
User Agent in BlackBerry Device Software 4.2 to 5.0
The format of the user agent used by BlackBerry® Device Software 4.2 to 5.0 is similar to the following:
BlackBerry9000/5.0.0.93 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179
As shown in this specific example, the user agent identifies three important pieces of information:
The underlying web engine that powers the BlackBerry web platform has evolved over time. As new releases to the BlackBerry device software are published, so have they included improvements and advancements to the web engine. As the user experience can be different for web content rendered by different rendering engines, it is important for developers to have a clear understanding of which engines are used on each platform, and for each type of web view.
| Web View objects | ||||
| OS | Browser application | BrowserField2 | RenderingApplication | QnxWebStageView |
| Tablet OS 1.x - 2.x | WebKit | N/A | N/A | WebKit |
| BlackBerry 7 | WebKit | WebKit | MIDP v1 | N/A |
| BlackBerry 6 | WebKit | WebKit | MIDP v1 | N/A |
| 5.0 | MIDP v2 | MIDP v2 | MIDP v1 | N/A |
| 4.7 | MIDP v2 | N/A | MIDP v1 | N/A |
| 4.6 | MIDP v2 | N/A | MIDP v1 | N/A |
| 4.5 | MIDP v1 | N/A | MIDP v1 | N/A |
Note: RenderingApplication is the legacy web view object. Developers are highly encouraged to use BrowserField2 instead for OS 5.0, BlackBerry 6 and 7 development as it offers access to more advanced web rendering engines (WebKit or MIDP v2).
How to detect the BlackBerry web platform
There are many ways your web content can read and evaluate the user agent header. Below you will find examples that demonstrate how to use various web technologies to implement this capability in your web site. Choose whichever technology suits your web site best (for example, if your existing web site is written in PHP, see the PHP sample).
Note: To detect the BlackBerry web platform for the BlackBerry 10 device, replace the search term "BlackBerry" with "BB10" or "blackberry" with "bb10" in the samples below.
Detecting the BlackBerry web platform in BlackBerry Device Software 6.0 using JavaScript®:
When using Javascript®, the following script can be executed inline or made into a function and called when the page’s onload event occurs:
<script type="text/javascript">
var ua = navigator.userAgent;
var url = "http://optimizedmobile.yoursite.com/";
if (ua.indexOf("BlackBerry") >= 0)
{
if (ua.indexOf("WebKit") >= 0)
{
window.location = url;
}
}
</script>
Detecting the BlackBerry Browser in BlackBerry Device Software 6.0 using ASP:
When using ASP, a server-side scripting language, you read the user agent value from the ServerVariables collection:
<%
Dim userAgent
Dim url
userAgent = Request.ServerVariables("HTTP_USER_AGENT")
url = "http://optimizedmobile.yoursite.com/"
If InStr(LCase(userAgent), "blackberry") > 0 Then
If InStr(LCase(userAgent), "webkit") > 0 Then
Response.Redirect(url)
End If
End If
%>
Detecting the BlackBerry web platform in BlackBerry Device Software 6.0 using PHP:
When using PHP, a server-side scripting language, you read the user agent value from the _SERVER collection:
<?php
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$pos_blackberry = strrpos($ua, "blackberry");
$pos_webkit = strrpos($ua, "webkit");
$url = "http://optimizedmobile.yoursite.com/";
if (!($pos_blackberry === false))
{
if (!($pos_webkit === false))
{
header("Location: " . $url);
}
}
?>
Detecting the BlackBerry web platform in BlackBerry Device Software 6.0 using JSP:
When using JSP®, a server-side scripting language, you read the user agent value from the Request object:
<%!
String detectBlackBerry(HttpServletRequest req)
{
String ua = req.getHeader("user-agent").toLowerCase();
String url = "";
if (ua.indexOf("blackberry") >= 0)
{
if (ua.indexOf("webkit") >= 0)
{
url = "http://optimizedmobile.yoursite.com/";
}
}
return url;
}
%>
<%
//Note: separate code block
String url = detectBlackBerry(request);
if (url.length() > 0)
{
response.sendRedirect(url);
}
%>
Connections made from the browser can be routed through a BlackBerry® Enterprise Server, the BlackBerry® Internet Service, a wireless service provider WAP gateway, or a W-iFi® network. For information on detecting the transport route, refer to Retrieve information about the BlackBerry Browser.
For more information about the BlackBerry browser, see: