How to connect to a specific wifi network in Android programmatically? -
How to connect to a specific wifi network in Android programmatically? -
i want design app shows list of wifi networks available , connect network when selected. have implemented part showing scan results. want connect particular network selected user list of scan results. can please tell me how this?
you need create wificonfiguration instance this:
string networkssid = "test"; string networkpass = "pass"; wificonfiguration conf = new wificonfiguration(); conf.ssid = "\"" + networkssid + "\""; // please note quotes. string should contain ssid in quotes
then, wep network need this:
conf.wepkeys[0] = "\"" + networkpass + "\""; conf.weptxkeyindex = 0; conf.allowedkeymanagement.set(wificonfiguration.keymgmt.none); conf.allowedgroupciphers.set(wificonfiguration.groupcipher.wep40);
for wpa network need add together passphrase this:
conf.presharedkey = "\""+ networkpass +"\"";
for open network need this:
conf.allowedkeymanagement.set(wificonfiguration.keymgmt.none);
then, need add together android wifi manager settings:
wifimanager wifimanager = (wifimanager)context.getsystemservice(context.wifi_service); wifimanager.addnetwork(conf);
and finally, might need enable it, android connects it:
list<wificonfiguration> list = wifimanager.getconfigurednetworks(); for( wificonfiguration : list ) { if(i.ssid != null && i.ssid.equals("\"" + networkssid + "\"")) { wifimanager.disconnect(); wifimanager.enablenetwork(i.networkid, true); wifimanager.reconnect(); break; } }
upd: in case of wep, if password in hex, not need surround quotes.
android android-wifi wifimanager
Comments
Post a Comment