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
Contributor
paulgee31
Posts: 24
Registered: ‎06-14-2011
My Device: Bold 9870
My Carrier: O2
Accepted Solution

Connected to Wifi check

Is there a means of checking when the device is connected via Wifi?

 

I've registered for  netstatus_request_events but don't seem to be receving any events when I do the check:

 

if (bps_event_get_domain(event) == netstatus_get_domain())

 

I can check the network availibility using:

 

    bool result;

    netstatus_get_availability(&result);

 

But this doesn't give me the type.

 

Is there a simpler way of checking?

 

Thanks.

 

Please use plain text.
BlackBerry Development Advisor
sabdelsayed
Posts: 249
Registered: ‎08-17-2012
My Device: BlackBerry Z10 and Q10
My Carrier: Bell

Re: Connected to Wifi check

[ Edited ]

Hi paulgee31, you can use the QNetworkInterface API to check if Wifi is connected. Use the following code snippet to achieve this:

 

	bool available = false;
	foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()){
		if(interface.flags().testFlag(QNetworkInterface::IsUp) &&
				interface.name().compare(QString("tiw_sta0")) == 0){
			available = true;
                        qDebug()<<"Wifi is up";
			break;
		}
	}

 

@SamarAbdelsayed

Did this answer your question? Please accept post as solution.
Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Please use plain text.
Developer
greenmr
Posts: 865
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Connected to Wifi check

Earlier today I upvoted your reply, only to find out later it doesn't really work. That code seems to successfully determine if WiFi is turned on, but not if it is actually connected. Instead, if WiFi is turned on then "available" is always set to true even if not connected.

 


sabdelsayed wrote:

Hi paulgee31, you can use the QNetworkInterface API to check if Wifi is connected. Use the following code snippet to achieve this:

 

	bool available = false;
	foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()){
		if(interface.flags().testFlag(QNetworkInterface::IsUp) &&
				interface.name().compare(QString("tiw_sta0")) == 0){
			available = true;
                        qDebug()<<"Wifi is up";
			break;
		}
	}

 






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
maarko
Posts: 119
Registered: ‎07-02-2013
My Device: Q10 v10.2.0.424
My Carrier: {unbranded}

Re: Connected to Wifi check

[ Edited ]

when i get bps event i use something like following to detect how network is connected. when turned gsm and turned wifi it gives me that wifi is connected. when disconnecting wifi it gives gsm connected......

 

 

 

	if(bps_event_get_domain(event)!=netstatus_get_domain()){

		// not a network status event.
		//
		return;
	}
	if(NETSTATUS_INFO!=bps_event_get_code(event)){

		// not a network information event.
		return;
	}

	const char* type="Unknown";
	bool status=false;

	// get connection status
	//
	netstatus_get_availability(&status);

	// get connection type at default interface
	//
	char* interface;
	if(BPS_SUCCESS==netstatus_get_default_interface(&interface)){
		netstatus_interface_details_t* details = NULL;
		if(BPS_SUCCESS==netstatus_get_interface_details(interface, &details)){
			switch (netstatus_interface_get_type(details)){

				case NETSTATUS_INTERFACE_TYPE_WIRED:	type="Wired";		break;
				case NETSTATUS_INTERFACE_TYPE_WIFI:	type="Wifi";		break;
				case NETSTATUS_INTERFACE_TYPE_BLUETOOTH_DUN:	type="Bluetooth";break;
				case NETSTATUS_INTERFACE_TYPE_BB:
				case NETSTATUS_INTERFACE_TYPE_USB:	type="USB";		break;
				case NETSTATUS_INTERFACE_TYPE_VPN:		type="VPN";		break;
				case NETSTATUS_INTERFACE_TYPE_CELLULAR:	type="GSM";	break;
				case NETSTATUS_INTERFACE_TYPE_UNKNOWN:			break;
			}
		}
	}

	emit networkStatusUpdated(status, type);

is that what you are looking for?

Please use plain text.
Developer
greenmr
Posts: 865
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Connected to Wifi check

[ Edited ]

Thank you, thank you, thank you!

 

I have been all over the Internet and back looking for a way to determine not only if WiFi is switched on, but also if it is connected. Every "solution" I found online actually only reported if WiFi was up or down, not its connection state. However, I was able to adapt your code example to do exactly what I needed. Here is the function I created:

 

 

#include <bps/netstatus.h>

bool onWiFi() {
    bool retVal = false;
    char* interface;

    if ( BPS_SUCCESS == netstatus_get_default_interface( &interface ) ) {
        netstatus_interface_details_t* details = NULL;
        if ( BPS_SUCCESS == netstatus_get_interface_details( interface, &details ) ) {
            retVal = ( netstatus_interface_get_type( details ) == NETSTATUS_INTERFACE_TYPE_WIFI );
            netstatus_free_interface_details( &details );
        }
        bps_free( interface );
    }

    return retVal;
}

 

This function returns true ONLY if WiFi is turned on AND connected as the default interface.



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
maarko
Posts: 119
Registered: ‎07-02-2013
My Device: Q10 v10.2.0.424
My Carrier: {unbranded}

Re: Connected to Wifi check

[ Edited ]

glad i could help :smileyhappy:

 

i've just also found out you can skip getting default interface. netstatus_get_interface_details(null,details) will return details of default one - as needed here :smileyhappy:

 

http://developer.blackberry.com/native/reference/core/com.qnx.doc.bps.lib_ref/com.qnx.doc.bps.lib_re...

 

Arguments:
interface

If NULL, reports the details of the default interface, if there is one.

Please use plain text.
Developer
greenmr
Posts: 865
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Connected to Wifi check

Thanks, I'll look into that tomorrow.

 


maarko wrote:

glad i could help :smileyhappy:

 

i've just also found out you can skip getting default interface. netstatus_get_interface_details(null,details) will return details of default one - as needed here :smileyhappy:

 

http://developer.blackberry.com/native/reference/core/com.qnx.doc.bps.lib_ref/com.qnx.doc.bps.lib_re...

 

Arguments:
interface

If NULL, reports the details of the default interface, if there is one.






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.
Developer
greenmr
Posts: 865
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Connected to Wifi check

Yes, you are in fact, correct. Supplying NULL to netstatus_get_interface_details() as the interface parameter does indeed get the details for the default inteface. With that in mind I have simplified my code. Also, since I wanted onWiFi() to return true in the simulator too I altered the condition a little to include a test for NETSTATUS_INTERFACE_TYPE_WIRED. As you will see below, I like to use switch() when I have more than one value to match rather than (condition)&&(condition) since I find it far more readable, especially when comparing to long #define names like this:

 

Here is the new simplified version:

 

bool onWiFi() {
    bool retVal = false;
    netstatus_interface_details_t* details = NULL;
    if ( BPS_SUCCESS == netstatus_get_interface_details( NULL, &details ) ) {
        switch ( netstatus_interface_get_type( details ) ) {
            case NETSTATUS_INTERFACE_TYPE_WIFI:
            case NETSTATUS_INTERFACE_TYPE_WIRED:
                retVal = true;
                break;
            default:
                break;
        }
        netstatus_free_interface_details( &details );
    }

    return retVal;
}

maarko wrote:

glad i could help :smileyhappy:

 

i've just also found out you can skip getting default interface. netstatus_get_interface_details(null,details) will return details of default one - as needed here :smileyhappy:

 

http://developer.blackberry.com/native/reference/core/com.qnx.doc.bps.lib_ref/com.qnx.doc.bps.lib_re...

 

Arguments:
interface

If NULL, reports the details of the default interface, if there is one.






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.
Please use plain text.