Retrieving network status

How to

Retrieve the cellular or Wi-Fi network status for a BlackBerry 10 device.


int shutdown  = 0;
bool is_available = false;
netstatus_ip_status_t ip4status;
int numgateways = 0;
const char * defaultgateway;
int index = 0;
const char * defaultinterface;
int numnameservers = 0;
const char * nameserver;
int numsearchdomains = 0;
const char * searchdomain;
bool loginrequired = false;

netstatus_request_events(0);      // 0 requests all network status events

while (!shutdown) {
  bps_event_t *event = NULL;
  bps_get_event(&event, -1);    // -1 means that the function blocks
                                      // while waiting for an event

  if (event) {
    if (bps_event_get_domain(event) == netstatus_get_domain()) {
      if (NETSTATUS_INFO == bps_event_get_code(event)) {
        netstatus_info_t *info = netstatus_event_get_info(event);
        if (info) {
          // Extract information using netstatus_info_get_*() functions
          is_available = netstatus_info_get_availability(info);
          ip4status = netstatus_info_get_ip4_status(info);
          numgateways = netstatus_info_get_num_default_gateways(info);          
          defaultgateway = netstatus_info_get_default_gateway(info, index);
          defaultinterface = netstatus_info_get_default_interface(info);
          numnameservers = netstatus_info_get_num_name_servers(info);
          // netstatus_info_get_name_server() gets the name server at the 
          // specified index from info. The value of index must be less
          // than numnameservers.
          nameserver = netstatus_info_get_name_server(info, index);
          numsearchdomains = netstatus_info_get_num_search_domains(info);
          // netstatus_info_get_search_domain() gets the search domain at 
          // the specified index from info. The value of index must be
          // less than numsearchdomains.
          searchdomain = netstatus_info_get_search_domain(info, index);
          loginrequired = netstatus_info_get_http_proxy_login_required(info);

          // Adjust app based on network status information here.

Build requirements

You must include the following header files from the BlackBerry Platform Services (BPS) library:

#include <bps/bps.h>
#include <bps/netstatus.h>


When you're developing applications, you might need to know if the device is connected to a network, either cellular or Wi-Fi. You can use the network status service in the BlackBerry Platform Services library to find out.

You can retrieve the current network status at any time by calling netstatus_get_info(), which returns the network status information in a netstatus_info_t structure.

The netstatus_info_t structure contains various network status properties (for example, IPv4 status or default interface). You must use the appropriate netstatus_info_get_ functions (for example, netstatus_info_get_ip4_status(), netstatus_info_get_default_interface()) to retrieve the properties.

Before calling any functions, you must initialize the BlackBerry Platform Services library by calling bps_initialize(). You can retrieve the network status at any time in your application by calling netstatus_get_info(). You can then use the various netstatus_info_get_*() functions to retrieve status information about specific network properties and elements. For example, you can call netstatus_info_get_ip4_status() with the network status information returned by netstatus_get_info() to retrieve the IPv4 status. You can then use this status information to adjust your app's operation.

You may want to check the network status in response to a network status event. To do this, you must request that network status events be delivered to your application by calling netstatus_request_events(). After your application is registered to receive network status events, you can call bps_get_event() to retrieve the next available event.

For most apps, bps_get_event() is called repeatedly to respond to events in a timely manner. You can use an event loop with a simple exit condition to accomplish this, and retrieve events inside this loop.

After an event is retrieved, you should test it to see if it's part of the network status domain by calling bps_event_get_domain() and comparing the result to netstatus_get_domain(). You also need to make sure that the event code is NETSTATUS_INFO, because this is the type of event that's generated when the network status changes. After you've verified the event domain and code, retrieve the network status by calling netstatus_get_info() and then respond to the status appropriately.

Related resources

Last modified: 2014-05-14

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus