Android Wi-Fi Tutorial

Android allows applications to access to view the access the state of the wirless connections at very low level. Application can access almost all the information of a wifi connection.
The information that an application can access includes connected network's link speed,IP address, negotiation state, other networks information. Applications can also scan, add, save, terminate and initiate Wi-Fi connections.
Android provides WifiManager API to manage all aspects of WIFI connectivity. We can instantiate this class by calling getSystemService method. Its syntax is given below:
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
In order to scan a list of wireless networks, you also need to register your BroadcastReceiver. It can be registered using registerReceiver method with argument of your reciever class object. Its sytanx is given below:
class WifiScanReceiver extends BroadcastReceiver {
   public void onReceive(Context c, Intent intent) {
   }
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));  
The wifi scan can be start by calling the startScan method of the WifiManager class. This method returns a list of ScanResult objects. You can access any object by calling the get method of list. Its syntax is given below:
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();
Apart from just Scanning , you can have more control over your WIFI by using the methods defined in WifiManager class. They are listed as follows:
Sr.NoMethod & Description
1addNetwork(WifiConfiguration config)
This method add a new network description to the set of configured networks.
2createWifiLock(String tag)
This method creates a new WifiLock.
3disconnect()
This method disassociate from the currently active access point.
4enableNetwork(int netId, boolean disableOthers)
This method allow a previously configured network to be associated with.
5getWifiState()
This method gets the Wi-Fi enabled state
6isWifiEnabled()
This method return whether Wi-Fi is enabled or disabled.
7setWifiEnabled(boolean enabled)
This method enable or disable Wi-Fi.
8updateNetwork(WifiConfiguration config)
This method update the network description of an existing configured network.

Example

Here is an example demonstrating the use of WIFI. It creates a basic application that scans a list of wirless networks and populate them in a list view.
To experiment with this example , you need to run this on an actual device on which wifi is turned on.
StepsDescription
1You will use Eclipse IDE to create an Android application and name it as WIFI under a package com.example.wifi. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add WebView code.
3Modify the res/layout/activity_main to add respective XML components
4Modify the AndroidManifest.xml to add the necessary permissions
5Run the application and choose a running android device and install the application on it and verify the results.
Following is the content of the modifed main activity file src/com.example.wifi/MainActivity.java.
package com.example.wifi;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

   WifiManager mainWifiObj;
   WifiScanReceiver wifiReciever;
   ListView list;
   String wifis[];
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      list = (ListView)findViewById(R.id.listView1);
      mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      wifiReciever = new WifiScanReceiver();
      mainWifiObj.startScan();
   }


   protected void onPause() {
      unregisterReceiver(wifiReciever);
      super.onPause();
   }

   protected void onResume() {
      registerReceiver(wifiReciever, new IntentFilter(
      WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
      super.onResume();
   }

   class WifiScanReceiver extends BroadcastReceiver {
      @SuppressLint("UseValueOf")
      public void onReceive(Context c, Intent intent) {
         List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
         wifis = new String[wifiScanList.size()];
         for(int i = 0; i < wifiScanList.size(); i++){
            wifis[i] = ((wifiScanList.get(i)).toString());
         }

         list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
         android.R.layout.simple_list_item_1,wifis));
      }
   }

}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <ListView
      android:id="@+id/listView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:drawSelectorOnTop="false"
      android:background="@android:color/background_dark"
      android:listSelector="@android:color/darker_gray" >

   </ListView>
</RelativeLayout>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.wifi"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="14"
      android:targetSdkVersion="17" />

   <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="com.example.wifi.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>
    <activity
         android:name="com.example.wifi.ListWifiActivity"
         android:label="@string/title_activity_list_wifi" >
      </activity>
   </application>
</manifest>

Let's try to run your WIFI application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Anroid Wi-Fi Tutorial Select your mobile device as an option and then check your mobile device which will display your mobile screen filled with wireless networks around you. It is shown below:
Anroid Wi-Fi Tutorial Note the information that has been returned to you. It contains much information about each of the wireless network detected.