java - Reconnecting to a WiFi Network after disconnecting from it Programatically -
i got disconnected wifi network programatically using
wifimanager wifi = (wifimanager) getsystemservice(context.wifi_service); wifi.disconnect(); disconnectwifi discon = new disconnectwifi(); registerreceiver(discon, new intentfilter(wifimanager.supplicant_state_changed_action)); public class disconnectwifi extends broadcastreceiver { @override public void onreceive(context c, intent intent) { wifimanager wifi = (wifimanager) c.getsystemservice(context.wifi_service); if(!intent.getparcelableextra(wifimanager.extra_new_state).tostring().equals(supplicantstate.scanning)) wifi.disconnect(); } }
but not able reconnect same network again. tried reconnecting using:
wifimanager wifi = (wifimanager) getsystemservice(context.wifi_service); wifi.reconnect();
but not able connect. how can reconnect wifi network?
thanks,
so complete, simplified solution this:
wificonfiguration wificonfig = new wificonfiguration(); wificonfig.ssid = string.format("\"%s\"", ssid); wificonfig.presharedkey = string.format("\"%s\"", key); wifimanager wifimanager = (wifimanager)getsystemservice(wifi_service); //remember id int netid = wifimanager.addnetwork(wificonfig); wifimanager.disconnect(); wifimanager.enablenetwork(netid, true); wifimanager.reconnect();
hope helps you!!
Comments
Post a Comment