Friday, March 11, 2011

Blackberry trick: retrieving the MNC correctly

Recently I need to retrieve the MNC value from the blackberry device, but though the real network testing, I realize that the value might not be correct for some cases.

The 3 different solutions of retrieving the MNC
  1. via GPRSInfo
  2. int mnc = GPRSInfo.getHomeMNC(); 
  3. via RadioInfo
  4. int currentIndex = RadioInfo.getCurrentNetworkIndex();  
          int mnc = RadioInfo.getMNC(currentIndex); 
  5. via GPRSCellInfo
  6. GPRSInfo.GPRSCellInfo cellInfo = GPRSInfo.getCellInfo();  
           int mnc = cellInfo.getMNC(); 
Note:in blackberry the returned MNC value is decimal, which is different with the MNC standard, which is hexadecimal.

I found that solution 1-2 are always consistent, they are always same if you are not in the roaming network. But in solution 3, in some cases you will get the wrong value. For example I tested the following UK carriers, the retrieved mnc values are all wrong:

Carrier Name MNC(Hex) retrieved(dec) retrieved(hex)
3 UK 20 527 20F
Orange UK3383133F
Vodafone UK1535115F
T-Mobile3078330F
O21027110F

From the above table, we can tell that all the mnc values are appended letter F at the end, if we removed the F, then the value is correct. This is the first time I have ever met. Actually I found this article mentioned this issue a little bit:
...therefore the corresponding hexadecimal value returned by getMCC()or getMNC()may contain the letter F as padding. For example, for T-Mobile®, the MCCs can be identified as either 260 or 26F.
If you want to use cellInfo ( ex. MCC, MNC, LAC, cellID) to get the current location via openCellID for example, then you need to be careful for this issue.

Conclusion
So the solution to get the correct MNC value from blackberry API should be: first check the last 4 bits value, if it is F (or more than 9), then right shift 4 bits, that is it!