Tuesday, September 11, 2012

Wi-Fi device scanning in Android


Hello Friend!
I am going to share code for Wi-Fi device scanning and getting their states. Important step are given below-
1-Create an android project.
2-Create a class WiFiDemo.
3-Create another class WiFiScaner for searching Wi-Fi devices.
4- Change in main.xml file.
5-Add Wi-Fi permission in manifest file.

a)Print Screen:

b)manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manish.wifi.demo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<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:label="@string/app_name"
android:name=".WiFiDemo" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
c)WiFiDemo.java
package com.manish.wifi.demo;

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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WiFiDemo extends Activity implements OnClickListener {
private static final String TAG = "WiFiDemo";
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.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
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 WiFiScaner(this);

registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}

@Override
public void onStop() {
unregisterReceiver(receiver);
}

public void onClick(View view) {
Toast.makeText(this, "All network serched!!!", Toast.LENGTH_LONG)
.show();

if (view.getId() == R.id.buttonScan) {
Log.d(TAG, "onClick() wifi.startScan()");
wifi.startScan();
}

}

}
d)WiFiScaner.java
package com.manish.wifi.demo;

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.util.Log;
import android.widget.Toast;

public class WiFiScaner extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
WiFiDemo wifiDemo;

public WiFiScaner(WiFiDemo 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();

Log.d(TAG, "onReceive() message: " + message);
}

}
e)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/buttonScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search Network" >
</Button>

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

<TextView
android:id="@+id/textStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WiFiDemo"
android:gravity="center"/>
</ScrollView>
</LinearLayout>

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.