Wrox Programmer Forums
|
BOOK: Professional Android 4 Application Development
This is the forum to discuss the Wrox book Professional Android 4 Application Development Reto Meier; ISBN: 978-1-1181-0227-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2014, 04:36 PM
Registered User
 
Join Date: Jul 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wifi seems to be not ready after reconnect !?

Hello,

I encounter a strange problem using WifiManager:
The code is:

Code:
	static class InitTask extends AsyncTask<Void, Integer, Integer> {

         ...

		@Override
		protected Integer doInBackground (Void... arg0) {
			WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
			WifiConfiguration conf = new WifiConfiguration();

			conf.SSID = "\"" + site.getWifiSSID() + "\"";   // Please note the quotes. String should contain ssid in quotes
			
			// Only support WPA
			conf.preSharedKey = "\"" + site.getwifiWPAKey() + "\"";
			
			int wifiState = wifi.getWifiState();
			Boolean enabled = wifi.isWifiEnabled();
			
			if ((site.getSavedDeviceWifiState() != WifiManager.WIFI_STATE_ENABLED) &&
					(site.getSavedDeviceWifiState() != WifiManager.WIFI_STATE_ENABLING)) {
				wifi.setWifiEnabled(true);
			}
			
			wifi.addNetwork(conf);

			List<WifiConfiguration> list = wifi.getConfiguredNetworks();
			for( WifiConfiguration i : list ) {
			    if(i.SSID != null && (i.SSID.equals("\"" + site.getWifiSSID() + "\"") == false)) {
			    
			         wifi.disconnect();
			         wifi.enableNetwork(conf.networkId, true);
			         wifi.reconnect();               
			         
			         break;
		    	     }
			 }
			 String verif = site.getWifiSSID();
		}
		
                // If I put a breakpoint here, the following code runs without any problem
		int responseCode = 0; 
		try {
			int i = 0;
			url = new URL(site.getDescriptionFileUrl());

			// Création d'une connection HTTP à une URL
			URLConnection connection = url.openConnection();
			HttpURLConnection httpConnection = (HttpURLConnection)connection;

                        // Next Call immediately throws a ConnectException
			responseCode = httpConnection.getResponseCode();

			if (responseCode == HttpURLConnection.HTTP_OK) {
                            ...
                       }
		}
		catch (MalformedURLException e) {
			Log.d("2ndGuide", "Malformed URL Exception." + e);
		} 
		catch (IOException e) {
			Log.d("2ndGuide", "IO Exception." + e);
		}
		Log.d("2ndGuide", mActivity.getResources().getString(R.string.label_messageAlertCnfBadUrl));
		return -6;
	}
So the problem is that if I let the code executes normally, getResponseCode() throws a ConnectException error:
07-18 20:29:41.821: D/2ndGuide(1925): ConnectException.java.net.ConnectException: 192.168.0.50/192.168.0.50:80 - Network is unreachable

If I put a breakpoint just before then click immediately on Play, to just wait a little bit, the code runs normally.

I never seen such warning in the doc or anywhere nor a mechanism to wait for the network being ready after a call to reconnect().

What's the problem??

Regards,
Alain





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 - Bonjour without Wifi network acsteester BOOK: Professional iOS Network Programming: Connecting the Enterprise to the iPhone and iPad 5 July 31st, 2013 08:55 AM
ready vs. ready [email protected] BOOK: Beginning JavaScript and CSS Development with jQuery 1 August 14th, 2009 02:05 PM
Windows Mobile 6 to use WiFi yukijocelyn C# 2005 2 May 28th, 2008 11:48 PM
setting up WiFi Access on Fedora Core 5 crmpicco Linux 0 August 24th, 2007 05:58 PM
Check/Reconnect Missing References ErikTheProgrammer Access VBA 3 February 20th, 2005 02:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.