Sunday, 27 January 2013

Scanning Wifi networks and Finding Strongest network in Android

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/Scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scan" >
    </Button>

    <ScrollView
        android:id="@+id/Scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/Status"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="WiFiDemo" />
    </ScrollView>

</LinearLayout>

WiFiScanReceiver.java:
package com.wifiapp1.inspire.ram;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class WiFiScanReceiver extends BroadcastReceiver {
  WifiApp1Activity wifiDemo;

  public WiFiScanReceiver(WifiApp1Activity wifiDemo) {
    super();
    this.wifiDemo = wifiDemo;
  }

  @Override
  public void onReceive(Context c, Intent intent) {
    List<ScanResult> results = wifiDemo.wifi.getScanResults();
    ScanResult bestSignal = null;
    for (ScanResult result : results) {
      if (bestSignal == null
          || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
        bestSignal = result;
    }

    String message = String.format("%s networks found. %s is the strongest.",
        results.size(), bestSignal.SSID);
    Toast.makeText(wifiDemo, message, Toast.LENGTH_LONG).show();

   
  }

}

WifiApp1Activity.java:
package com.wifiapp1.inspire.ram;

import java.util.List;

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

public class WifiApp1Activity extends Activity implements OnClickListener {

WifiManager wifi;
BroadcastReceiver receiver;

TextView textStatus;
Button buttonScan;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Setup UI
textStatus = (TextView) findViewById(R.id.Status);
buttonScan = (Button) findViewById(R.id.Scan);
buttonScan.setOnClickListener(this);

// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());

// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus.append("\n\n" + config.toString());
}

// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);

registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

public void onClick(View view) {
Toast.makeText(this, "Scanning.......",
Toast.LENGTH_LONG).show();

 
wifi.startScan();
}

}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wifiapp1.inspire.ram"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WifiApp1Activity"
            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>