Android add wifi profile for static programmatically

First of all, the Full Android project for download.

WifiSetting

Simply desc the code.  MainActivity.saveWepConfig(String ssid, String password) adding the Wifi Profile

This version is for WPA, and setting the password to wc.preSharedKey, for WEP, password will store at wc.wepKeys[0], and setting the wc.wepTxKeyIndex=0.

The allowXXX.set, can accept multiply set, just set all the choice is OK.

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = ssid; //IMP! This should be in Quotes!!
wc.hiddenSSID = false;
wc.status = WifiConfiguration.Status.ENABLED;
wc.priority = 1;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
// wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
// wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.preSharedKey = password ;

Following is the part for setting the Static IP, just ignore it for case DHCP, it just calling the Helper class, since the Helper class involve reflect, for setting the (private?) properties.

// set STATIC
try{
WifiHelper.setIpAssignment(“STATIC”, wc); //or “DHCP” for dynamic setting
WifiHelper.setIpAddress(InetAddress.getByName(“192.168.0.211″), 24, wc);
WifiHelper.setGateway(InetAddress.getByName(“192.168.0.254″), wc);
WifiHelper.setDNS(InetAddress.getByName(“192.168.0.254″), wc);
//wifiManag.updateNetwork(wifiConf); //apply the setting
}catch(Exception ex){
Log.e(TAG, “Exception when set the Static IP:”, ex) ;
}

The WifiHelper, is helper class for setting the Static Network info for the Android (3.0 & 4.0). (ref : http://stackoverflow.com/questions/10278461/how-to-configue-static-ip-netmask-gateway-programmatically-on-android-3-x-or-4), for detail please download the project.

44 comments to Android add wifi profile for static programmatically

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>