Search code examples
androidwireless

Android improperly forgets wireless networks


I've written an application to easily connect Android devices to our company's various wireless networks. It is intended to be run once, when the device is unboxed and set up with the rest of our application suite.

The problem is that sometimes, the device will forget the networks without being told to do so. I have not been able to reproduce this, unfortunately. I'm hoping that I've missed some API call that the Android gurus here can help me find.

The code that follows is from my run-once wireless initialization app, with the network names and keys changed. Can anyone see something I'm doing incorrectly here?

Edit: The device that I have observed this behavior on is the LG Ally. I'm hoping it's a bug with my own code, so that's where I'm starting.

@Override
public void onCreate(Bundle savedInstanceState)
{
    WifiConfiguration n1Config = new WifiConfiguration();
    n1Config.SSID = "\"networkOne\"";
    n1Config.preSharedKey = "\"...\"";
    n1Config.status = WifiConfiguration.Status.ENABLED;

    WifiConfiguration n2Config = new WifiConfiguration();
    n2Config.SSID = "\"networkTwo\"";
    n2Config.hiddenSSID = true;
    n2Config.wepKeys[0] = "...";
    n2Config.wepTxKeyIndex = 0;
    n2Config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    n2Config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    n2Config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    n2Config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    n2Config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    n2Config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    n2Config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    n2Config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    n2Config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    n2Config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    n2Config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    n2Config.status = WifiConfiguration.Status.ENABLED;

    WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    int n1ID = wifiManager.addNetwork(n1Config);
    wifiManager.enableNetwork(n1ID, false);

    int n2ID = wifiManager.addNetwork(n2Config);
    wifiManager.enableNetwork(n2ID, false);
}

Solution

  • I believe you need to call wifi.saveConfiguration(); after you have added both of your configurations to the WifiManager.