Sunday, January 23, 2011

Turn Wifi On/Off using WifiManager.setWifiEnabled()

Last exercise show how to "Detect Wifi ON/OFF state", this exercise show how to Turn Wifi On and Off.

Turn Wifi On/Off using WifiManager.setWifiEnabled()

To Turn Wifi On/Off, WifiManager.setWifiEnabled() can be used.

First of off, modify main.xml to add to button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/wifistate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/onwifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Turn Wifi On"
/>
<Button
android:id="@+id/offwifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Turn Wifi Off"
/>
</LinearLayout>


Modify the code to call WifiManager.setWifiEnabled().
package com.exercise.AndroidWifiStateChangedDetect;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidWifiStateChangedDetect extends Activity {

TextView WifiState;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WifiState = (TextView)findViewById(R.id.wifistate);
Button OnWifi = (Button)findViewById(R.id.onwifi);
Button OffWifi = (Button)findViewById(R.id.offwifi);

this.registerReceiver(this.WifiStateChangedReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));

OnWifi.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}});

OffWifi.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}});
}

private BroadcastReceiver WifiStateChangedReceiver
= new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
WifiManager.WIFI_STATE_UNKNOWN);

switch(extraWifiState){
case WifiManager.WIFI_STATE_DISABLED:
WifiState.setText("WIFI STATE DISABLED");
break;
case WifiManager.WIFI_STATE_DISABLING:
WifiState.setText("WIFI STATE DISABLING");
break;
case WifiManager.WIFI_STATE_ENABLED:
WifiState.setText("WIFI STATE ENABLED");
break;
case WifiManager.WIFI_STATE_ENABLING:
WifiState.setText("WIFI STATE ENABLING");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
WifiState.setText("WIFI STATE UNKNOWN");
break;
}

}};
}


In order to change Wifi state, we have to grant permission of "android.permission.CHANGE_WIFI_STATE".
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidWifiStateChangedDetect"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidWifiStateChangedDetect"
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>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</manifest>


Download the files.

8 comments:

duang said...

I am interested in your code. I have some question.
-How do i know which wifi that i connect.
-Do you have the method for connect 1 wifi? such as i want to connect A-wifi only how is method for simply connect that?

thank you very much

Nibilulus said...

Hi.
I copied your code and I got an exception message:

java.lang.SecurityException: WifiService: Neither user 10081 nor current process has android.permission.ACCESS_WIFI_STATE.

any suggestions?

Nibilulus said...

I added to my manifest:






AND it works!

Unknown said...

hey my app force closes on using the wifi enable diable button ??
what do i do?

Anonymous said...

could you teach me how to code android to setup automatic wireless with it ip address and give the information about which wifi is being connected? hope you will help me this. btway thanks for this coding which i implemented, well it works! great. thanks a lot!

Unknown said...

Falta un permiso en el Manifest
agreguen este

y listo correra de maravilla ^^

Unknown said...

Agreguen un permiso mas al manifest..
falta este


agregenlo y listo correra de maravilla

Anonymous said...

i hope this book can helpŝ too
http://andbrain.com/blog/wp-content/uploads/2014/09/AndBrain.Wifi-_-andBrain.pdf