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

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

how to know the network status

gprs connnection,wifi connection is there or not

Please use plain text.
Developer
joelajean
Posts: 132
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900

Re: how to know the network status

Please use plain text.
Developer
Developer
p8
Posts: 132
Registered: ‎01-24-2013
My Device: blackberry z10

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);
    }
}

 

 

 

Please use plain text.
Developer
Shashadhar
Posts: 18
Registered: ‎04-09-2013
My Device: Z10

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!

 

Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520

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...

 

 

Please use plain text.