I want to create open network in Android programatically. I am using the following code:
WifiManager mainWifiObj;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainWifiObj = (WifiManager)getSystemService(Context.WIFI_SERVICE);
addNetwork();
}
//Add Dummy Wifi
public void addNetwork(){
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = dummyTemp;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//Get Network ID from the MainWiFi Object
dummyNetworkId = mainWifiObj.addNetwork(wc);
//Success, can call mainWifiObj.enableNetwork(networkId, false)
if (dummyNetworkId != -1) { mainWifiObj.enableNetwork(wc.networkId, false); }
}
However, when I go into the Wifi menu I don't see this network added. Please help.
Edit:
I have the necessary permissions. I can view other networks I just am not able to add another network. Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pervysage.wifi" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So, weird quirk, you have to wrap the SSID in quotes for it to work:
wc.SSID = dummyTemp;
should be:
wc.SSID = "\"" + dummyTemp + "\"";
EDIT: Added Javadoc from the WifiConfiguration class:
/**
* The network's SSID. Can either be an ASCII string,
* which must be enclosed in double quotation marks
* (e.g., {@code "MyNetwork"}, or a string of
* hex digits,which are not enclosed in quotes
* (e.g., {@code 01a243f405}).
*/
public String SSID;