Remove password and connect with new password wifi android -
i ask help, 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 , sharewifi), create sharewifi purpose: users input wifi ssid , password press enter, both of them send mainactivity--> disable current wifi , reconnect new password still uses old password reconnect. write code follow link : how connect specific wi-fi network in android programmatically?
please me resolve problem. thank much.
resolved: add saveconfiguration , works
wifimanager.addnetwork(conf); wifimanager.saveconfiguration();
Comments
Post a Comment