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