Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Regular Contributor
Posts: 60
Registered: ‎06-08-2012
My Device: 8900,9550
My Carrier: tatadocomo
Accepted Solution

how to know the network status

gprs connnection,wifi connection is there or not

Highlighted
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: how to know the network status

Developer
Developer
Posts: 132
Registered: ‎01-24-2013
My Device: blackberry z10
My Carrier: none

Re: how to know the network status

 

Hello.

 

I do something like this when I want to send a network request (there could be an easier way).

Querying

_Session->isOpen()

should be what youre looking for

 

//-----------------------------------------------------
tNetMan::tNetMan(QObject * Par)
    : QObject(Par), _Session(0),  _waitingForNetwork(false),_waitingForSession(false), _waitingForUpdateConfigs(false), _updatedConfigs(false)
{

    _NetAccMan = new QNetworkAccessManager(this);
    _NetConfMan = new QNetworkConfigurationManager(this);

    _PendingReqs = new tNetReqs();
    _RecentReqs = new tNetReqs();
    _PendingReplies = new QList<QNetworkReply *>;

    connect(_NetConfMan, SIGNAL(updateCompleted()), this, SLOT(slotUpdateConfigsCompleted()));
    connect(_NetAccMan,  SIGNAL(finished(QNetworkReply*)), SLOT(slotReplyFinished(QNetworkReply* )));

}
//----------------------------------------------------------------
void tNetMan::sendReq(tNetReq Req)
{
    setPrevReq(Req);
    pendingReqs()->append(Req);
    ensureSessionOpen();  // make sure network stuff set up 
}

//-----------------------------------------------------
void tNetMan::updateConfigs()
{

    setWaitingForUpdateConfigs(true);
    netConfMan()->updateConfigurations();
}
//-----------------------------------------
void tNetMan::slotUpdateConfigsCompleted()
{
    setUpdatedConfigs(true);
    setWaitingForUpdateConfigs(false);
    ensureSessionOpen();
}
//-----------------------------------------------------
void tNetMan::ensureSessionOpen()
{

    if(waitingForUpdateConfigs())
        return; // wait..still busy scanning WLAN

    if(!updatedConfigs())
    {
        updateConfigs(); // scan at least once
        return;
    }

    setWaitingForSession(true);

    if(!_Session)
    {
        QNetworkConfiguration Config = netConfMan()->defaultConfiguration();
        _Session = new QNetworkSession(Config);


        connect(_Session, SIGNAL(opened()), this, SLOT(slotSessionOpened()));
        connect(_Session, SIGNAL(error(QNetworkSession::SessionError)), this, SLOT( slotSessionError(QNetworkSession::SessionError)));
        connect(_Session, SIGNAL(stateChanged(QNetworkSession::State)), this, SLOT(slotSessionStateChanged(QNetworkSession::State)));

    }

    if (_Session->isOpen())
        slotSessionOpened();
    else
        _Session->open();

}
//-----------------------------------------------------
// Actually send the request
void tNetMan::slotSessionOpened()
{

    setWaitingForSession(false);

    // preform any pending requests
    while(!pendingReqs()->isEmpty())
    {
        tNetReq Req = pendingReqs()->takeLast();
        sendReqNow(Req);
    }
}

 

 

 

Developer
Posts: 18
Registered: ‎04-09-2013
My Device: Z10
My Carrier: Vodafone

Re: how to know the network status

Hi created a class to check internetAvailability .Hope it helps

 

----Header file--

#ifndef CHECKINTERNETAVAILABILITY_H_
#define CHECKINTERNETAVAILABILITY_H_
#include<qobject.h>
#include<bps/netstatus.h>
#include<bps/bps.h>
#include <bb/AbstractBpsEventHandler>
#include<qdebug.h>



class CheckInternetAvailability :public QObject,public bb::AbstractBpsEventHandler{
 Q_OBJECT
public:
CheckInternetAvailability();
virtual ~CheckInternetAvailability();
virtual void event(bps_event_t *event);
void requestEventsToSubscribe();
int handleEventsSubscribed();


};

#endif /* CHECKINTERNETAVAILABILITY_H_ */

 Cpp file

#include "CheckInternetAvailability.h"

CheckInternetAvailability::CheckInternetAvailability() {
	// TODO Auto-generated constructor stub
	bps_initialize();
	requestEventsToSubscribe();

}

CheckInternetAvailability::~CheckInternetAvailability() {
bps_shutdown();
}

void CheckInternetAvailability::requestEventsToSubscribe() {
	subscribe(netstatus_get_domain());
	netstatus_request_events(0);

}
int  CheckInternetAvailability::handleEventsSubscribed() {
	bool is_Available;
			netstatus_get_availability(&is_Available);
	if (is_Available) {
		qDebug() << "Connected to Internet Service";
		return 1;
	}
	else
	{
		qDebug()<<"No Internet Available";
	}
	return 0;
}
void CheckInternetAvailability::event(bps_event_t *event)
{
netstatus_interface_details_t *details;
qDebug()<<"Got subscribed event ";
if(bps_event_get_domain(event)==netstatus_get_domain())
{
	if(bps_event_get_code(event)==NETSTATUS_INFO)
	{
     bool status=netstatus_event_get_availability(event);
     const char * interface=netstatus_event_get_default_interface(event);
      int success=netstatus_get_interface_details(interface,&details);
      QString type;
      if(success==BPS_SUCCESS)
      {
    	  switch(netstatus_interface_get_type(details))
    	  {
    	  case NETSTATUS_INTERFACE_TYPE_UNKNOWN:
    	          type = "Unknown";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_WIRED:
    	          type = "Wired";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_WIFI:
    	          type = "Wi-Fi";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_BLUETOOTH_DUN:
    	          type = "Bluetooth";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_USB:
    	          type = "USB";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_VPN:
    	          type = "VPN";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_BB:
    	          type = "BB";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	      case NETSTATUS_INTERFACE_TYPE_CELLULAR:
    	          type = "Cellular";
    	          qDebug()<<"Internet Connectivity:"<<type;
    	          break;

    	  }
      }

	}
	}
}



 Just call to check connectivity.

 

Thanks!

 

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: how to know the network status

Thanks for the code. It lights me out and things are clearer now...

However I would like to get network type used so i wrote this code:

 

int status;
char **interface = NULL;

status = netstatus_get_default_interface(interface);
if (status == BPS_SUCCESS) {
  m_networkType = interface[0];
}

return m_networkType;

 However it always return an empty String and i got internet connection...