Detect Wifi Network

            Hear I am going to explain how you can detect wifi network which are in you range ,First we create two design file one is base layout for display wifi network list layout and scan button and another one is cell of wifi network , and also create one calss file which have code for scan wifi network.


           Hear we use wifiManager class for scan wifi.



Layout File : activity_main.xml //main layout

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.scanwifi.MainActivity" >
    <Button
        android:id="@+id/btcanwifi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Scan Wifi"
        android:onClick="scanWifi"
        />
    <ListView
        android:id="@+id/wifi_device_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Layout File : list_item.xml //for row of wifi list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        />
</RelativeLayout>
Class File : MainActivity.java // main java class file

package com.dhaval.demo;
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.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
    WifiManager wifi;
    List<ScanResult> wifiList;
    WifiReceiver wifiReceiver;
    ListView wifi_device_list;
    ArrayAdapter<String> adapter;
    ArrayList<String> wifiResults;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wifi_device_list=(ListView)findViewById(R.id.wifi_device_list);
        wifiResults=new ArrayList<String>();
        adapter=new ArrayAdapter<String>(this, R.layout.list_item,R.id.txt,wifiResults);
        wifi_device_list.setAdapter(adapter);
        // Get instance of wifi Manager
        wifi = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
        // Create New WifiReceiver instance
        wifiReceiver = new WifiReceiver();
    }
    public void scanWifi(View view){
        // Scaning wifi
        wifi.startScan();
    }
    protected void onResume(){
        super.onResume();
        // Register wifi receiver to get the results
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }
    protected void onPause(){
        super.onPause();
        unregisterReceiver(wifiReceiver);
    }
    class WifiReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            wifiResults.clear();
            wifiList = wifi.getScanResults();
            for(int i = 0; i < wifiList.size(); i++){
                wifiResults.add(wifiList.get(i).toString());
            }
            adapter.notifyDataSetChanged();
        }
    }
}

No comments: