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
Developer
austinfrance
Posts: 65
Registered: ‎01-05-2010
My Device: Not Specified

Detecting wifi connection not working

[ Edited ]

I am using the following class to work out my URL suffix to append when making an http or https request. It is based on other peoples suggestions on how to connect.

 

public static class Net {

public final static int FORCE_NOTHING = 0;
public final static int FORCE_WIFI = 1;
public final static int FORCE_DIRECT = 2;

private static int timeoutWifi = 15000;
private static int timeoutDirect = 15000;
private static int timeoutOther = 45000;

public static int forceConnectionType = Net.FORCE_NOTHING;

public static boolean inService = true;
public static boolean inDataService = true;

public static int setForceConnectionType(int type) {
	int prev = forceConnectionType;
	forceConnectionType = type;
	return prev;
}

public static void checkService() {
	// See if we have a radio signal (are in service)
	if(RadioInfo.getSignalLevel() != RadioInfo.LEVEL_NO_COVERAGE) {
		App.DEBUG(1, "DEVICE HAS RADIO SIGNAL");
		inService = true;
	}
	
	// Data service available
	if((RadioInfo.getNetworkService() & RadioInfo.NETWORK_SERVICE_DATA)>0) {
		App.DEBUG(1, "DEVICE HAS DATA SERVICE");
		inDataService = true;
	}
}

public static String getConnectionString()
{
	// Check if we are in service
	checkService();
	
	// Forced Connection Types (Used mainly for testing atm)
	
	// Force connection through wifi
	if (forceConnectionType == FORCE_WIFI) {
		App.DEBUG(5, "WIFI is FORCED");
		return "interface=wifi;ConnectionTimeout=" + timeoutWifi;
	}

	// Force direct
	if (forceConnectionType == FORCE_DIRECT) {
		App.DEBUG(5, "DIRECT TCP FORCED");
		return "deviceside=true;ConnectionTimeout=" + timeoutDirect;
	}
	
	// Simulator Check (might want to disable this if testing MDS etc)

	// The Device is a simultaor --> TCP (disabled atm)
	if (false && DeviceInfo.isSimulator()) {
		App.DEBUG(5, "IS SIMULATOR USE TCP");
		return "deviceside=true;ConnectionTimeout=" + timeoutDirect;
	}

	// Give WiFi priority

	/* TODO how to detect if WIFI available
            - code below does not work!
	String networkName = RadioInfo.getCurrentNetworkName();
	if (null != networkName) {
		App.DEBUG(5, "WIFI AVAILABLE USE IT");
		return "interface=wifi;ConnectionTimeout=15000";
	}
	*/
	
	// Ok lets look at using a data service
	String st = "";
	
	// A carrier is providing us with the data service
	if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_BIS_B) == CoverageInfo.COVERAGE_BIS_B)
	{
		// blackberry internet service
		ServiceRecord rec = getBIBSRecord();
		if (rec == null) {
			//couldn't find the right record
			App.DEBUG(5, "NO BIBS record, just let phone decide");
			st = "deviceside=true";// let the phone try to do the work
		}
		else {
			// found the record, get the id
			App.DEBUG(5, "BISB UID " + rec.getUid());
			st = "deviceside=false;connectionUID=" + rec.getUid()
					+ ";ConnectionType=???-public";
		}
	}
	else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
		App.DEBUG(5, "USE MDS (Enterprise Server)");
		st = "deviceside=false";// use the clients blackberry enterprise server
	}
	else {
		App.DEBUG(5, "DONT KNOW, LET DEVICE DECIDE");
		st = "deviceside=true";// let the phone do the work if it can
	}

	// Add connection timeout
	return st + ";ConnectionTimeout=" + timeoutOther;
}

 

*edit* the ???-public above is mds- but the forum blocks it for some reason saying it's not allowed (depreciated, private?)

 

Basically I use this class as follows:

 

Url url = new Url(urlString);
if (null == url.options) {
    url.options = Net.getConnectionString();
}
request = (HttpConnection) Connector.open(requestUrl.toString());
However on the device it does not pick up wireless (unless I force it using Net.setForceConnectionType().

 

I can force it, but I need a reliable way to detect the presence of wireless, checking for a non-null return from RadioInfo.getCurrentNetworkName() does not seem to work as the device returns a name for the mobile network.

 

Using the above code, it appends ;deviceside=true to my URLs, but it is not picking up wifi (and wifi is connected) I thought deviceside=true was supposed to pick wireless if available?

 

Please use plain text.
Developer
austinfrance
Posts: 65
Registered: ‎01-05-2010
My Device: Not Specified

Re: Detecting wifi connection not working

[ Edited ]

Popular religious figure -  its taken nearly 2 hours to get the forum to accept that post!!  Due to a combination of not being allowed to post more than one topic in an hour and formatting / keyword errors seemingly resetting the timer.

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Detecting wifi connection not working

"not being allowed to post more than one topic in an hour"

This doesn't seem to effect me.

 

There are a number of ways of detecting WiFi.  Review this Thread for some SrviceBook options:

http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-c...

 

Or you can use CoverageInfo and WLANInfo to detect directly on supported OS's.

Please use plain text.
Developer
austinfrance
Posts: 65
Registered: ‎01-05-2010
My Device: Not Specified

Re: Detecting wifi connection not working

I did spend an afternoon reading through that long long thread about how to connect which is how I have ended up where I am, I could have missed something though I suppose.

 

I have just found (RadioInfo.getActiveWAFs() & RadioInfo.WAF_WLAN) != 0 which looks like it might do the job, if that evaluates true, I am guessing I can just append ;interface=wifi

 

Available in 4.2.1

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Detecting wifi connection not working

Only an afternoon!

 

This post tells you how to use ServiceBook to find out if WiFi is there:

http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-c...

 

But you are right, I think RadioInfo will do it for you too. 

Please use plain text.
Developer
austinfrance
Posts: 65
Registered: ‎01-05-2010
My Device: Not Specified

Re: Detecting wifi connection not working

Ok, RadioInfo.getActiveWAFs() indicates if the WiFi option is enabled.  It does not mean there is a WiFi network connected.

 

I will try using it in combination with the code you linked.

Please use plain text.
Contributor
sleepymg
Posts: 35
Registered: ‎02-11-2010
My Device: curve 8900

Re: Detecting wifi connection not working

[ Edited ]

Your app can extend WLANListener which contains method networkConnected and networkDisconnected which are called upon these events.

Here is API documentation:

http://www.blackberry.com/developers/docs/4.3.0api/net/rim/device/api/system/WLANConnectionListener....

 

It is used in combination with WLANInfo class

http://www.blackberry.com/developers/docs/4.3.0api/net/rim/device/api/system/WLANInfo.html

Please use plain text.