2014-02-11 62 views
2

我正在構建我自己的AOSP(Android開源項目)分支。AOSP - 添加默認Wifi SSID

我想弄清楚如何將WIFI配置添加到構建。所以一旦我閃到已經擁有SSID/WEP密鑰集的設備上。

我看了在默認配置在這裏:

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

但是我找不到任何與Wifi SSID/Key有關的東西。

有什麼建議嗎?

謝謝!

+1

wifi配置轉換爲'/ data/misc/wifi/wpa_supplicant.conf'中的條目。當我弄清楚如何正確地預加載該文件時,我會在這裏發佈答案。 – Harvey

回答

1

沒有找到一種方法來直接包括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; 
    } 
0

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

只是供參考。如果您正在執行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 
}