Retrieving network status
How to
Determine whether a cellular or Wi-Fi network is available for the BlackBerry 10 device.
Solution
Retrieve the current network status at any time
bool is_available; bps_initialize(); netstatus_get_availability(&is_available); // Handle the network status information here
Retrieve the current network status in response to an event
bool is_available;
bps_initialize();
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)) {
is_available = netstatus_event_get_availability(event);
// Handle the 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>
Discussion
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.
Before you call 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_availability() and passing a Boolean variable by reference to this function. That Boolean variable is populated with the current network status; a value of true means that a network connection is available, and a value of false means that a network connection isn't available. You can then use this information any way you want.
You can also retrieve the network status in response to an event. You must initialize the BlackBerry Platform Services library, and then you can 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. It's important to remember that when you call bps_get_event(), most services will report an event immediately with the initial values of the service.
In a typical application, you likely won't call bps_get_event() just once. You'll probably call it over and over again so you can process and respond to any events that occur while your application is running. 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(). You also need to make sure that the code of the event 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, you can retrieve the network status from the event by calling netstatus_event_get_availability() and then respond to the status appropriately.
Nice to know
The NETSTATUS_INFO event type contains other useful information. For example, if the device is connected to a network through an HTTP proxy, you can retrieve information such as the proxy host (using netstatus_event_get_http_proxy_host()), proxy port (using netstatus_event_get_http_proxy_port()), and so on.