Search code examples
androidwifi

Android: Temporarily connect to WiFi network?


when I connect to an 8open) WLAN network using this code:

WifiConfiguration wifiConfig = new WifiConfiguration(); 
wifiConfig.BSSID =result.BSSID; // BSSID of detected network
wifiConfig.priority = 1; 
wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE); 
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
wifiConfig.status=WifiConfiguration.Status.ENABLED; 

int netId = scanData.wifiManager.addNetwork(wifiConfig); 
scanData.wifiManager.enableNetwork(netId, true);

...this WiFi connection is stored in list of preferred networks. So when I try that very often I get a really huge list of connections - and that's somethin I do not want.

So my question: how can I establish a connection to a WLAN network just temporarily and let Android not store it permanently? Or is there a way to remove such a network in case it is no longer needed?

Thanks!


Solution

  • When you disconnect from the network, just remove it from the stored list. See details here: In Android, How do you clear the WI-FI list of networks in Settings via Java?