Pervy Sage
Pervy Sage

Reputation: 871

How do I create a open Wifi network in android programatically?

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>

Upvotes: 2

Views: 677

Answers (2)

Naveed Ahmad
Naveed Ahmad

Reputation: 6737

You need the following permissions in your manifest file:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

Then you can use the following in your activity class:

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);

Use the following to check if it's enabled or not

boolean wifiEnabled = wifiManager.isWifiEnabled()

You'll find a nice tutorial on the subject on this site.

Upvotes: 0

Kevin Coppock
Kevin Coppock

Reputation: 134704

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;

Upvotes: 1

Related Questions