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

Java Development

Reply
Regular Contributor
Posts: 57
Registered: ‎09-22-2010
My Device: Not Specified
Accepted Solution

check network status

Hi,

 

Maybe this question had been posted before, but unfortunatelly I couldn't see anything relevant.

For OS 5.0 and later, what is the best way to see if data connectivity is available?

Is below code snippet correct and complete?

protected boolean isNetworkConnected() {
		if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE && WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_DISCONNECTED) {
			return false;
		}
		return true;
	}

 

Regards,

gunar

New Developer
Posts: 77
Registered: ‎09-02-2010
My Device: torch
My Carrier: blackberry developer

Re: check network status

Hi

 

I am using the same to test the network status. And its giving correct result for all the times. I could not get you what the exact problem you are facing from this.

Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: check network status

i use http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/CoverageInfo.html#isCov... with the appropriate flag, depending on my needs MDS, BIS_B or DIRECT.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
Posts: 57
Registered: ‎09-22-2010
My Device: Not Specified

Re: check network status

@harinath: the question is if this is a best practice for checking network availability ... In your oppinion this is Smiley Happy

 

Thanks!

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: check network status

I use an approach like Simon's, because I typically let the users decide how they want to connect.  There is not point saying the network is available when there is Mobile cover but no WiFi, if the user has specified WiFi only....

Regular Contributor
Posts: 57
Registered: ‎09-22-2010
My Device: Not Specified

Re: check network status

Thanks Peter and Simon, your answers and comments are welcomed anytime!

 

I am not sure I understand this sentence "I typically let the users decide how they want to connect." ... in the context where the application needs to know if network is available or not. When creating a phisical connection - that's a different story - it will check then which connection paths should be followed or not, based on their availability.

 

Maybe my comment is subject to another thread, but why would the application be concerned about which connection should be followed (when there are at least 2 available, let's say MDS and WiFi)?

 

I'm used with Androids way of handling connectivity where connectivity is simply available or not ... when creating a connection it doesn't matter which path is followed.

 

Thanks,

gunar

Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: check network status

yes, of course it is important which connection is followed, but the main thing you want to know is:
Is network available?

Which exact route to take can either be decided by http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/transport/ConnectionFactory...
or, if the OS is too low, using different mechanisms, like the code Peter wrote (we also have our own connectionfactory for below <5.0).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
Posts: 57
Registered: ‎09-22-2010
My Device: Not Specified

Re: check network status

yes, that was my question: for OS 5 and later, is the code from the question post correct and complete to answer the question: "Do we have Internet connectivity?" How we setup the connection, that is a complete story that I am aware of.

Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: check network status

yes, your code reliably tells you if there is no network at all.
i think that the first half is sufficient, as the wlanstate does not give you extra information.

i use the other way around (isCoverageSufficient) to ensure that the connection type i am using is available, but that is mostly due to the fact that i use MDS.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: check network status

"I'm used with Androids way of handling connectivity where connectivity is simply available or not "

 

Oh that would be so lovely......

 

Unfortunately you are going to have to get used to a different approach. 

 

And in fact Android's approach is not without its problem.  For example, if Android is connecting via Mobile, then it can get hit by things the Mobile company's put in their gateway,specifically parental blocks.  But WiFi does not get parental blocks.  So you do have to have some connection specific code even in Android. 

 

And another case that also applies to Android:  Say the app had to download a large number of megabytes of data, so it allowed the user to specify if this would only be done when the user was on WiFi (so didn't cost).  Then there is no point testing for mobile connectivity. 

 

But back with BlackBerry.

 

Type network in the Search box and review one of the networking videos. 

 

Assuming you are coding for OS 5.0, then you have two choices

a) Use ConnectionFactory, give it the choice of options, and let it find a connection method (or write your own code to do the same sort of thing)

b) Initially test out the connectivity options and choose the one that works the best.

 

I must admit I typically do (b).  And so once I have chosen a method (or two) I will test for specific connectivity related to the connection methods I am going to try, rather than just check for general connectivity. 

 

Why don't I do (a).  Because there is potentially a delay for every network connection while it checks out the methods, for some users, some methods cost and some are free, and some methods have parental blocks or proxy servers, or optimization transcoders and some don't, and for some corporate users, you have to use BES anyway.   

 

Does that clarify my point?