Connecting to specific wifi sometimes fails on android -


i creating app, can list out available wifi's in listview. if select 1 of wifi's in list, cached before in list<wificonfiguration> list = wifimanager.getconfigurednetworks(); should connect it. if wificonfiguration list doesn't contain selected wifi, nothing happens. problem is, select wifi list (which know sure in wificonfiguration list), doesn't connects it. instead connects connected wifi. after attempts (selecting again , again same wifi) connects finally. doesn't happen always, sometimes. can problem? here code snippet:

// go through cached wifis , check if selected gopro cached before (wificonfiguration config : configurations) {     // if cached connect , that's     if (config.ssid != null && config.ssid.equals("\"" + mdrawerlistview.getadapter().getitem(position) + "\"")) {         // log         log.i("onreceive", "connecting to: " + config.ssid);         mwifimanager.disconnect();         mwifimanager.enablenetwork(config.networkid, true);         mwifimanager.reconnect();         break;      } } 

this what's going on. basically, can tell os disable network, , can tell os enable network, it's not possible tell os network connect to.

if there multiple wifi access points in range both configured on device (and both in enabled state), os decide 1 connect to.

the way force os connect 1 of networks in range instead of other 1 call disablenetwork() on network in range don't want connect to.

let's go through code line line:

mwifimanager.disconnect(); 

the line above tells os disconnect connected wifi access point.

mwifimanager.enablenetwork(config.networkid, true); 

the line above tells device set network enabled state if in disabled state.

mwifimanager.reconnect();  

from the documentation:

reconnect active access point, if disconnected. may result in asynchronous delivery of state change events.

so, when instead connects connected wifi., it's working expected, os re-connecting considers currently active access point.

if want disable other network os connect 1 clicked on, this:

// go through cached wifis , check if selected gopro cached before  wifiinfo info = mwifimanager.getconnectioninfo(); //get wifiinfo int id = info.getnetworkid(); //get id of connected network  (wificonfiguration config : configurations) {     // if cached connect , that's     if (config.ssid != null && config.ssid.equals("\"" + mdrawerlistview.getadapter().getitem(position) + "\"")) {         // log         log.i("onreceive", "connecting to: " + config.ssid);          mwifimanager.disconnect();          mwifimanager.disablenetwork(id); //disable current network          mwifimanager.enablenetwork(config.networkid, true);         mwifimanager.reconnect();         break;      } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -