以编程方式修改或删除现有的wifi配置

时间:2011-05-04 18:02:50

标签: android

我想删除或修改现有的wifi配置。有人能告诉我如何或指向正确的地方吗?

由于

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

答案 2 :(得分:0)

要删除/忘记wifi网络,请尝试以下两个链接,Android - Cant Remove Wifi Network Programatically- The method removeNetwork(int) in the type WifiManager is not applicable for the arguments (String)How to forget a wireless network in android programmatically?。对于修改特定网络,我认为您应该先删除它然后再询问新密码并设置如下:

if(scanResultSecurity.equals(Constants.WEP)){
                                        wifiConfiguration.SSID = "\"" + networkSSID + "\""; 
                                        wifiConfiguration.wepKeys[0] = "\"" + password + "\"";
                                        wifiConfiguration.wepTxKeyIndex = 0;
                                        wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                                        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                                        //Adds to the list of network and returns the network id which can be used to enable it later.
                                        networkId = wifiManager.addNetwork(wifiConfiguration);
                                        if(networkId != -1){
                                            connectToWifi(wifiConfiguration);
                                        }

                                    }
                                    else if(scanResultSecurity.equals(Constants.PSK)){
                                        wifiConfiguration.SSID = "\"" + networkSSID + "\""; 
                                        wifiConfiguration.preSharedKey = "\"" + password + "\"";
                                        wifiConfiguration.hiddenSSID = true;
                                        wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
                                        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                                        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                                        wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                                        wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                                        wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                                        wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                                        wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                                      //Adds to the list of network and returns the network id which can be used to enable it later.
                                        networkId = wifiManager.addNetwork(wifiConfiguration); 
                                        if(networkId != -1){
                                            connectToWifi(wifiConfiguration);
                                        }
                                    }

private void connectToWifi(WifiConfiguration wifiConfiguration){
        wifiManager.disconnect();
        wifiManager.setWifiEnabled(true);
        boolean enableNetworkBoolean = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
        Log.i(Constants.LOG_TAG, "networkId "+wifiConfiguration.networkId);
        Log.i(Constants.LOG_TAG, "SSID "+wifiConfiguration.SSID);
        Log.i(Constants.LOG_TAG, enableNetworkBoolean+" enableNetworkBoolean");
        boolean reconnectBoolean = wifiManager.reconnect();
        Log.i(Constants.LOG_TAG, reconnectBoolean+" reconnectBoolean");
        boolean changeHappen = wifiManager.saveConfiguration();
        Log.i(Constants.LOG_TAG, changeHappen+" changeHappen");
        if(enableNetworkBoolean && reconnectBoolean && changeHappen){
            Log.i(Constants.LOG_TAG, "Change Happen" );     
//          Utility.showToast(MainActivity.this, Constants.CONNECTED);
            wifiListViewAdapter.notifyDataSetChanged();
        }
        else{
            Log.i(Constants.LOG_TAG, "Change not Happen" );
            Utility.showToast(MainActivity.this, Constants.CONNECTING_ERROR);
        }                   
    }