Hi,

I've been trying in different ways to create and acquire a Wifi lock
in WIFI_MODE_SCAN_ONLY mode. The final aim is to develop a wifi
connection manager app, which should allow a user to scan for wifi
networks, connect to a given SSID, or disconnect from it.

Here's an excerpt of the code i'm using at the moment, which seems to
be creating a wifi lock correctly, but still, if i disconnect from a
SSID, 3 seconds later a scan is automatically initiated and the wifi
connects to the first known (remembered) SSID.

private TrustiveWifi(Context context) {
        Log.d(TAG, "Created TrustiveWifi");
        this.context = context;
        wm = (WifiManager) context.getSystemService
(context.WIFI_SERVICE);

        ......................

        // create a wifi lock to prevent the wifi from shutting down
        // the mode will be WIFI_MODE_SCAN_ONLY until a connection is
explicitely required
        createWifiLock(false);

        ......................

    }

    public static TrustiveWifi getInstance(Context context) { //
singleton
        if (INSTANCE == null) {
            INSTANCE = new TrustiveWifi(context);
        }
        return INSTANCE;
    }

    public void finalize() throws Throwable {
        Log.d(TAG, "Finalizing");
        try {
            releaseWifiLock();

            ......................

        } catch(Exception e) {
        } finally {
            super.finalize();
        }
    }

    public void createWifiLock(boolean wifi_mode_full) {
        releaseWifiLock();
        Log.d(TAG, "createWifiLock with full mode=" + wifi_mode_full);
        wifiLock = wm.createWifiLock(wifi_mode_full ?
wm.WIFI_MODE_FULL : wm.WIFI_MODE_SCAN_ONLY, "TrustiveWifi wifilock");
        wifiLock.setReferenceCounted(false);
        wifiLock.acquire();
        Log.d(TAG, "created a wifi lock: " + wifiLock.toString());
    }

    public void releaseWifiLock() {
        if (wifiLock != null && wifiLock.isHeld()) {
            Log.d(TAG, "Release wifi lock");
            wifiLock.release();
        }
    }

    public void disconnect() {
        Log.d(TAG, "disconnecting");
        createWifiLock(false);
        wm.disconnect();
    }

    /** returns false if the network isn't configured yet. Create a
new wifi lock with mode WIFI_MODE_FULL */
    public boolean connect(int selectedHotspot) {
        String selectedSSID = wifiNetworks.get
(selectedHotspot).scanResult.SSID;

        for (WifiConfiguration wc: wm.getConfiguredNetworks()) {
            String ssid = wc.SSID.replaceAll("\"", "");
            if (ssid.equals(selectedSSID)) {
                Log.d(TAG, "Connecting to hotspot: " + wifiNetworks.get
(selectedHotspot));
                createWifiLock(true);
                wm.enableNetwork(wc.networkId, true);
                return true;
            }
        }
        Log.d(TAG, "Connection requested, but hotspot not configured
yet");
        return false; // hotspot not found in the configured networks
    }


I have no clue how to move forward on this issue, so any help and/or
advice will be greatly appreciated!

Mathieu

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to