How to get Configured Network types from the Network Type Bitmask (Bearer)


OVERVIEW : 

Please refer to the overview section in https://techblogs.42gears.com/how-to-get-configured-apn-types-from-the-apn-type-bitmask/ to get an overview of Access Point Name (APN).

While developing an application where we can configure an APN, view and edit the configured APN, one of the places where I was stuck was how to get the configured Bearer (Network types) so that the user can view and edit this particular field.

NETWORK TYPE BITMASK (BEARER) : 

Network Type Bitmask (Bearer) describes the Radio Technologies (Network Types) which this APN may use. One APN entry may take multiple Network types. By default, its value will be TelephonyManager#NETWORK_TYPE_UNKNOWN (or Unspecified).

We can calculate Network type bitmask from NETWORK_TYPE defined in TelephonyManager.

Example of Network Types include NETWORK_TYPE_1xRTT, NETWORK_TYPE_CDMA, NETWORK_TYPE_EDGE, NETWORK_TYPE_EHRPD, NETWORK_TYPE_EVDO_0, NETWORK_TYPE_EVDO_A, NETWORK_TYPE_EVDO_B, NETWORK_TYPE_GPRS, NETWORK_TYPE_GSM, NETWORK_TYPE_HSDPA, NETWORK_TYPE_HSPA, NETWORK_TYPE_HSPAP, NETWORK_TYPE_HSUPA, NETWORK_TYPE_IDEN, NETWORK_TYPE_IWLAN, NETWORK_TYPE_LTE, NETWORK_TYPE_NR, NETWORK_TYPE_TD_SCDMA, NETWORK_TYPE_UMTS, NETWORK_TYPE_UNKNOWN.

Following code can be used to create a Network type bitmask from the different Network Types selected by the user mSelectedNetworkTypesList

int mIntNetworkTypeBitmask = 0;
for(final int networkType : mSelectedNetworkTypesList)
{
   if(networkType == 0)
   {
       mIntNetworkTypeBitmask = 0;
       break;
   }
   else
   {
       mIntNetworkTypeBitmask |= (1 << (networkType - 1));
   }
}

Now this mIntNetworkTypeBitmask can be passed to setNetworkTypeBitmask().

HOW TO SEGREGATE DIFFERENT NETWORK TYPES :

To get the configured Network Type Bitmask, we can use getNetworkTypeBitmask().

Now the problem arises where we want to segregate the different Network types present in the bitmask. If we carefully see all the integer values of different Network types, we can observe that all these values are distinct and lie from 0 to 20.

If we study the logic of creating a network type bitmask from different network types in bits, we can verify that the set bit in the network type bitmask at nth place from right represents the network type having integer value as n.

For eg : let’s suppose the user selects NETWORK_TYPE_EDGE (value : 2), NETWORK_TYPE_EVDO_0 (value : 5) and NETWORK_TYPE_HSDPA (value : 8), then network type bitmask would be

(1 << (2-1)) | (1 << (5-1)) | (1 << (8-1)) = (1 << 1) | (1 << 4) | (1 << 7) = (10010010)

So now to derive NETWORK_TYPE_EDGE, NETWORK_TYPE_EVDO_0, NETWORK_TYPE_HSDPA from the above bitmask (10010010), we can reverse engineer this by starting a while/for loop and doing bitwise AND of bitmask with 1, comparing the result with 1 in every iteration, if the comparison is true, then the network type having the integer value equal to that iteration count is present in the bitmask. Also we need to right shift the bitmask by 1 in every iteration.

For eg : the bitmask (10010010) contains the set bit at 2nd, 5th and 8th position from right, that means the network type having integer values equal to 2 (NETWORK_TYPE_EDGE), 5 (NETWORK_TYPE_EVDO_0) and 8 (NETWORK_TYPE_HSDPA) are present in this bitmask.

Following code can be used to get the configured network types from the bitmask mIntNetworkTypeBitmask

if(mIntNetworkTypeBitmask == 0)
{
   //NETWORK_TYPE_UNKNOWN i.e. default value of network type bitmask is present
}
else
{
   for (int i = 1; mIntNetworkTypeBitmask != 0; i++)
   {
       if((mIntNetworkTypeBitmask & 1) == 1)
       {
           //network type having integer value as i is present in the bitmask
           //Your Code
       }
       mIntNetworkTypeBitmask >>= 1;
   }
}

REFERENCES :

Leave A Comment

Your email address will not be published. Required fields are marked *