Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Working with radios

There may be cases where you need to turn on a smartphone's radio to use a particular network transport. For example, you may want your app to connect over Wi-Fi to make efficient use of bandwidth. Or, you may want to compare your app's performance when you use different wireless technologies.

You can use the net.rim.device.api.system.RadioInfo class to gather information about the wireless technologies that are available on a smartphone. Using the rim.device.api.system.Radio class, you can manage the radios on the smartphone. You should also implement the net.rim.device.api.system.RadioStatusListener interface to receive notifications about changes in radio status.

The wireless technologies that are available on BlackBerry smartphones are grouped into three wireless access families.

Wireless access family

Description

3GPP

GPRS, EDGE, UMTS GERAN, UTRAN, and GAN

CDMA

CDMA2000 and EVDO

WLAN

IEEE 802.11™, 802.11a, 802.11b, 802.11g

Query a radio's availability and change its status

You can use the RadioInfo and Radio classes to determine whether a particular radio technology is supported on a BlackBerry smartphone, and to turn on or turn off the radio.

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class NetworkSample extends UiApplication {
    public static void main(String[] args) 
    {
        NetworkSample app = new NetworkSample();
        app.enterEventDispatcher();
    }
    public NetworkSample() 
    {
        pushScreen(new RadioControlScreen());
    }
}

class RadioControlScreen extends MainScreen implements FieldChangeListener 
{

    boolean[] _intRadioOn = new boolean[3];
    ButtonField _btn3G, _btnCDMA, _btniDEN, _btnWiFi;

    public RadioControlScreen()
    {
        VerticalFieldManager vfm = new VerticalFieldManager(Field.FIELD_HCENTER);

//Retrieve a bit field of supported wireless access families.
        int wafs = RadioInfo.getSupportedWAFs();
        if ((wafs & RadioInfo.WAF_3GPP) != 0 )
        {
            _btn3G = new ButtonField("3G Radio");
            _btn3G.setChangeListener(this);
            vfm.add(_btn3G);
        }
        if ((wafs & RadioInfo.WAF_CDMA) != 0 )
        {
            _btnCDMA = new ButtonField("CDMA Radio");
            _btnCDMA.setChangeListener(this);
            vfm.add(_btnCDMA);
        }

//Check if the wireless access family that you want to use is
//available on the smartphone. Perform a bitwise AND operation that 
//combines the intSupportedWAFs bit field and the constant for the
//wireless access family that you want to use. If the family is 
//available, the result will be nonzero.
        if ((wafs & RadioInfo.WAF_WLAN) != 0 )
        {
            _btnWiFi = new ButtonField("WiFi Radio");
            _btnWiFi.setChangeListener(this);
            vfm.add(_btnWiFi);
        }

//Retrieve a bit field of available radios.
        wafs = RadioInfo.getActiveWAFs();
        if ((wafs & RadioInfo.WAF_3GPP) != 0 )
        {
            _intRadioOn[0] = true;
            _btn3G.setLabel("3G Radio is on");
        }
        if ((wafs & RadioInfo.WAF_CDMA) != 0 )
        {
            _intRadioOn[1] = true;
            _btnCDMA.setLabel("CDMA Radio is on");

//Check if the radio that you want to use is turned off. If it is
//turned off, a bitwise AND operation applied to 
//intActiveWAFs and the constant for the radio returns
//0.
        }
        if ((wafs & RadioInfo.WAF_WLAN) != 0 )
        {
            _intRadioOn[2] = true;
            _btnWiFi.setLabel("WiFi Radio is on");
        }

        add(vfm);
        StandardTitleBar tb = new StandardTitleBar();
        tb.addTitle("Radio Control Demo");
        tb.addSignalIndicator();
        setTitleBar(tb);

    }

    public void fieldChanged(Field field, int context) 
    {
        if (field instanceof ButtonField)
        {
            if(field == _btn3G)
            {
                if (_intRadioOn[0])
                {
                    Radio.deactivateWAFs(RadioInfo.WAF_3GPP);
                    _btn3G.setLabel("3G Radio is off");
                    _intRadioOn[0] = false;
                } else
                {
                    Radio.activateWAFs(RadioInfo.WAF_3GPP);
                    _btn3G.setLabel("3G Radio is on");
                    _intRadioOn[0] = true;
                }
            } else if(field == _btnCDMA)
            {
                if (_intRadioOn[1])
                {
                    Radio.deactivateWAFs(RadioInfo.WAF_CDMA);
                    _btnCDMA.setLabel("CDMA Radio is off");
                    _intRadioOn[1] = false;
                } else
                {
                    Radio.activateWAFs(RadioInfo.WAF_CDMA);
                    _btnCDMA.setLabel("CDMA Radio is on");
                    _intRadioOn[1] = true;
                }
            } else if(field == _btnWiFi)
            {
                if (_intRadioOn[2])
                {
                    Radio.deactivateWAFs(RadioInfo.WAF_WLAN);
                    _btnWiFi.setLabel("WiFi Radio is off");
                    _intRadioOn[2] = false;
                } else
                {

//If the radio is turned off, then turn it on. 
		                   Radio.activateWAFs(RadioInfo.WAF_WLAN);
                    _btnWiFi.setLabel("WiFi Radio is on");
                    _intRadioOn[2] = true;
                }
            }
        }

    }
}