Android WifiLock WIFI_MODE_SCAN_ONLY not working
Asked Answered
S

2

8

I'm trying to block the wifi connections. I want my application to turn on the wifi, but does not connect to any network that is already stored on the smartphone. But even after I use the SCAN_ONLY mode, he continues to connect to networks that already "know".

    .....
    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    .....
    wifiManager.setWifiEnabled(true);
    WifiLock scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");      
scanOnly.acquire(); 

Already in despair i tried to disconnect after to make sure that the state is WIFI_STATE_ENABLED wifi. That the app can not connect for a few seconds, but after some time it connects to a network in the same ...

    wifiManager.setWifiEnabled(true);
    ....
    WHEN (WIFI STATE == WIFI_STATE_ENABLED)
    {wifiManager.disconnect();
scanOnly.acquire();} 

Can someone help me? Tks

Saltigrade answered 27/10, 2011 at 11:17 Comment(0)
P
1

WifiLock not for this. Only for "Allows an application to keep the Wi-Fi radio awake". This does not forbid anyone to connect to wifi. Only says "I want to be able to do at least this in sleep mode". You have to do what you said Peanut above: get the list of all the stored networks and then disable wifi in BroadcastReceiver.

@Override
public void onReceive(Context c, Intent intent) {
    saveWifiList(mainWifi.getScanResults());

    unregisterReceiver(this);
    mainWifi.setWifiEnabled(false);

    showWifiList();     
}
Pedestal answered 3/9, 2013 at 22:51 Comment(0)
S
0

One way would be to get the list of all the stored networks and then disable them. You might want to store their state somewhere so you can restore the initial state after you're done.

Stealage answered 21/1, 2012 at 14:34 Comment(1)
i forgot to tell: you can enable and disable networks using the functions enableNetwork(int netId) and disableNetwork(int netID)Stealage

© 2022 - 2024 — McMap. All rights reserved.