Pregunta

I am building my fork of AOSP (Android Open Source Project).

I am trying to figure out how to add Wifi Configuration to the build. So once I flash onto a device that it already has the SSID/WEP Key set.

I've looked in the default configuration here:

/frameworks/base/packages/SettingsProvider/res/values/defaults.xml 
/frameworks/base/core/res/res/values/config.xml

But I cannot find anything related to Wifi SSID/Key.

¿Fue útil?

Solución

Just FYI. If you are doing AOSP images and wanna add preinstalled accesspoints. search wpa_supplicant-overlay.conf file (normally under /data/misc/wifi/) and add network information there. Then roll out the images. Tested with Marshmallow.

Example
network={
    ssid="test_wlan1"
    psk="test_key1"
    key_mgmt=WPA-PSK
    priority=1
}

network={
    ssid="test_wlan2"
    psk="test_key2"
    key_mgmt=WPA-PSK
    priority=2
}

Otros consejos

Never found a way to directly include Wifi Configuration, but I created a seperate application that calls

public void saveWepConfig(String SSID, String Password, boolean Hidden)
    {
        WifiConfiguration wfc = new WifiConfiguration();

        wfc.SSID = "\"".concat(SSID).concat("\"");
        wfc.status = WifiConfiguration.Status.DISABLED;
        wfc.priority = 40;
        wfc.hiddenSSID = Hidden;

        wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

        if (isHexWepKey(Password)) wfc.wepKeys[0] = Password;
        else wfc.wepKeys[0] = "\"".concat(Password).concat("\"");
        wfc.wepTxKeyIndex = 0;

        WifiManager wfMgr = (WifiManager) _context.getSystemService(Context.WIFI_SERVICE);
        int networkId = wfMgr.addNetwork(wfc);
        if (networkId != -1) {
         // success, can call wfMgr.enableNetwork(networkId, true) to connect
            wfMgr.enableNetwork(networkId, true);
        }

    }

    private boolean isHexWepKey(String s) {
        if (s == null) {
            return false;
        }

        int len = s.length();
        if (len != 10 && len != 26 && len != 58) {
            return false;
        }

        for (int i = 0; i < len; ++i) {
            char c = s.charAt(i);
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
                continue;
            }
            return false;
        }
        return true;
    }

You can define your network inside:

system/etc/wifi/wpa_supplicant_overlay.conf

Which can be found in AOSP path under:

device/.../wpa_supplicant_overlay.conf

And inside this file you can have the network configuration, for example:

network={
    ssid="my wifi name"
    psk="my password here"
    key_mgmt=WPA-PSK
    priority=42
}

After rebooting the device, if the configuration is valid, it will copy it with more network configuration into:

/data/misc/wifi/wpa_supplicant.conf

Another approach is to add the network programmatically.

WPA solution based on @Carsten's solution:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

.

public static void saveWpaConfig(Context context, String SSID, String Passphrase, boolean Hidden)
{
    WifiManager wfMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // Turn on WiFi if necessary
    if (!wfMgr.isWifiEnabled())
        wfMgr.setWifiEnabled(true);
    // Do not continue if we can't get into a good state or
    // if this device already has at least one WifiConfiguration
    List<WifiConfiguration> cfgList = wfMgr.getConfiguredNetworks();
    if (!wfMgr.isWifiEnabled() || cfgList == null || cfgList.size() > 0)
        return;

    WifiConfiguration wfc = new WifiConfiguration();
    wfc.SSID = "\"".concat(SSID).concat("\"");
    wfc.preSharedKey = "\"".concat(Passphrase).concat("\"");
    wfc.hiddenSSID = Hidden;

    int networkId = wfMgr.addNetwork(wfc);
    if (networkId != -1) {
        wfMgr.enableNetwork(networkId, true);
        // Use this to permanently save this network
        // wfMgr.saveConfiguration();
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top