【问题标题】:Modify or Delete existing wifi configuration programmatically以编程方式修改或删除现有 wifi 配置
【发布时间】:2011-05-04 18:02:49
【问题描述】:

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

谢谢

【问题讨论】:

  • 您是尝试以编程方式执行此操作,还是这是一个一般用法问题?

标签: android


【解决方案1】:
【解决方案2】:
【解决方案3】:

要删除/忘记 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);
        }                   
    }

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 2015-01-28
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多