p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional Android 4 Application Development (http://p2p.wrox.com/forumdisplay.php?f=685)
-   -   Wifi seems to be not ready after reconnect !? (http://p2p.wrox.com/showthread.php?t=93298)

AlainBo26 July 18th, 2014 04:36 PM

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


All times are GMT -4. The time now is 04:21 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.