0

i would like to ask your help, this is my code :

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

i have 2 Activities (MainActivity and ShareWifi), i create Sharewifi with purpose: users input wifi SSID and password then press Enter, both of them will send to MainActivity--> disable current wifi and reconnect with new password but it still uses old password to reconnect. I write this code follow this link : How do I connect to a specific Wi-Fi network in Android programmatically?
Please help me to resolve this problem. Thank you very much.

Community
  • 1
  • 1
DuongKa
  • 3
  • 2

1 Answers1

0

Resolved: add saveConfiguration and it works

                wifiManager.addNetwork(conf);
                wifiManager.saveConfiguration();
DuongKa
  • 3
  • 2
  • Actually it will work only in situation when old password was wrong and new one is correct, but in situation when old password is correct and new one is not correct - then it will use old password in anyway, I tested. – Stepan Maksymov Apr 30 '18 at 10:08