Programmatically Set Wi-Fi Profile
One question that is asked of our team fairly often is if there's a way to programmatically set a wifi profile on the Cius. The quick answer to this question is "yes", and below I will explain how to create an Android wifi profile, called a "WifiConfiguration", and add it to the Cius' (or any Android device really) list of configured networks.
The first step in creating a wifi profile is to create an instance of a WifiConfiguration, which is what stores the profile's info, and an instance of WifiManager, which is a system service that deals with, you guessed it, managing the wifi and its profiles on the device. The code for doing both looks like this:
3WifiConfiguration WC=new WifiConfiguration();
That last line of code sets the new WifiConfiguration's SSID (network name) to the name of the network you are configuring. The next step is to add the network's password (if one is needed). This could be either a WEP or WPA password, or the network could be open. No matter what, you should know ahead of time what kind of network security the wifi network has (WEP, WPA-PSK, open, etc) and configure the WifiConfiguration accordingly as below:
For WEP password...
For WPA password...
and finally for an open network...
If there are more advanced options for the profile you are trying to configure, such as the network being hidden or settings a BSSID, those are also fields in the WifiConfiguration that can be set also. Once you have defined all the relevant fields for the WifiConfiguration, you can go ahead and add it to the device's configured list and enable it as seen below:
After completing the above steps, your wifi profile has been added to the device's configured list of networks and is enabled, meaning it is considered when the device is looking to connect to a wifi network. This does not mean, however, that the device will connect to the new network immediately. If a wifi network is already connected, the above code just adds your network to the list of other configured networks. However, if you want to now force your network to be connected to, even if another network is already connected, then use the following code:
That extra statement "WM.enableNetwork()" connects the device's wifi to your newly added network (based on it's profile ID) and disables (disconnects) all other wifi networks, meaning yours is the only one left for the device to connect to.
--by James Catalano
Subscribe to our Cius Developer Blog RSS feed to get the latest info and sample code.