删除密码并连接新密码wifi android

时间:2015-08-03 07:12:56

标签: wifi android-wifi

我想问你的帮助,这是我的代码:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==REQUEST_CODE_INPUT){
        switch (resultCode){
            case RESULT_CODE_PASS:
                ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                WifiManager wifiManager= (WifiManager) getSystemService(Context.WIFI_SERVICE);
                pass=data.getStringExtra("passWord");
                nameWifi=data.getStringExtra("nameWifi");
                WifiConfiguration conf=new WifiConfiguration();
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                List<ScanResult> networkList=wifiManager.getScanResults();
                if(networkList !=null){
                    for(ScanResult network : networkList){
                        if(network.SSID.startsWith("\"")){
                            network.SSID=network.SSID.substring(1, network.SSID.length() - 1);
                        }
                        if(nameWifi.equals(network.SSID)){
                            String Capabilities=network.capabilities;
                            if(Capabilities.contains("WPA2")){
                                conf.preSharedKey="\""+pass+"\"";
                            }else if(Capabilities.contains("WEP")){
                                conf.wepKeys[0]="\""+pass+"\"";
                                conf.wepTxKeyIndex=0;
                                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                            }
                            break;
                        }
                    }
                }

                wifiManager.addNetwork(conf);
                List<WifiConfiguration> list=wifiManager.getConfiguredNetworks();
                for(WifiConfiguration i: list){
                    if(i.SSID!=null && i.SSID.equals("\""+nameWifi+"\"")){
                        wifiManager.disconnect();
                        showWaiting();
                        wifiManager.enableNetwork(i.networkId, true);
                        wifiManager.reconnect();
                        if(networkInfo.isConnected()){
                            dismissWaiting();
                        }else{
                            AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
                            alert.setTitle("Wrong Password")
                                    .setMessage("Please try again")
                                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });
                            alert.create().show();
                        }
                    }
                }
                break;
        }
    }
}

我有2个活动(MainActivity和ShareWifi),我有目的创建Sharewifi:用户输入wifi SSID和密码然后按Enter,它们都将发送到MainActivity - &gt;禁用当前wifi并重新连接新密码,但它仍然使用旧密码重新连接。我写这段代码请点击此链接:How do I connect to a specific Wi-Fi network in Android programmatically?
请帮我解决这个问题。非常感谢你。

1 个答案:

答案 0 :(得分:0)

已解决:添加saveConfiguration,它可以正常工作

                wifiManager.addNetwork(conf);
                wifiManager.saveConfiguration();