It might be handy to get BlackBerry OS version when developing for multiple versions of BlackBerry OS. Following is slightly modified code I took BlackBerry Forums (sorry cannot find the post, but all the credit goes to the original author of the post):
public static String getOSVersion() {
String swVersion = null;
ApplicationManager appMan = ApplicationManager.getApplicationManager();
ApplicationDescriptor[] appDes = appMan.getVisibleApplications();
int size = appDes.length;
for (int i = 0; i < size; i++){
if ((appDes[i].getModuleName()).equals("net_rim_bb_ribbon_app")) {
swVersion = stripBuildId(appDes[i].getVersion());
break;
}
}
return swVersion;
}
public static int[] getOSVersionInt() {
String version = getOSVersion();
Vector result = new Vector();
int num = version.length();
int start = 0;
int end = 0;
for (int i = 0; i < num; i++) {
char ch = version.charAt(i);
if (ch == '.' || i == num - 1) {
end = (i == num - 1) ? i + 1 : i;
result.addElement(version.substring(start, end));
start = end + 1;
}
}
num = result.size();
int[] resultArr = new int[num];
for (int i = 0; i < num; i++) resultArr[i] = Integer.parseInt((String)result.elementAt(i));
return resultArr;
}
public static String getUserAgent() {
String version = getOSVersion();
return "BlackBerry" + DeviceInfo.getDeviceName() + "/" + version + " "
+ "Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/" + Branding.getVendorId();
}
private static String stripBuildId(String version) {
int index = version.lastIndexOf('.');
return version.substring(0, index);
}