Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WifiLock WIFI_MODE_SCAN_ONLY not working

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

like image 909
Vasco Fernandes Avatar asked Oct 27 '11 11:10

Vasco Fernandes


1 Answers

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();     
}
like image 191
ahtartam Avatar answered Nov 04 '22 03:11

ahtartam