AOSP - 添加默认Wifi SSID

时间:2014-02-11 14:54:12

标签: android android-source

我正在构建自己的AOSP分支(Android开源项目)。

我想弄清楚如何在构建中添加Wifi配置。因此,一旦我闪存到已经具有SSID / WEP密钥集的设备上。

我在这里查看了默认配置:

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

但我找不到与Wifi SSID / Key相关的任何内容。

有什么建议吗?

谢谢!

4 个答案:

答案 0 :(得分:2)

从未找到直接包含Wifi配置的方法,但我创建了一个单独的应用程序,调用

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;
    }

答案 1 :(得分:1)

仅供参考。如果您正在执行AOSP图像并想要添加预先安装的访问点。 搜索wpa_supplicant-overlay.conf文件(通常在/ data / misc / wifi /下)并在那里添加网络信息。然后推出图像。用棉花糖测试。

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
}

答案 2 :(得分:0)

基于@ Carsten解决方案的WPA解决方案:

<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();
    }
}

答案 3 :(得分:0)

您可以在内部定义网络:

system/etc/wifi/wpa_supplicant_overlay.conf

可以在AOSP路径中的以下位置找到

device/.../wpa_supplicant_overlay.conf

在此文件中,您可以进行网络配置,例如:

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

重启设备后,如果配置有效,它将使用更多网络配置将其复制到:

/data/misc/wifi/wpa_supplicant.conf

另一种方法是以编程方式添加网络。