View Single Post
  #1 (permalink)  
Old July 18th, 2014, 04:36 PM
AlainBo26 AlainBo26 is offline
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote