Android detect wifi


If you want to detect Wifi networks in your application, this Android tip is useful to you. In this Android tip, I am going to show you how to detect nearby Wifi and display the results in a ListView.
Again, Android provides WifiManager class that is able to manage all aspects of Wifi connectivity. To detect Wifi networks, you need to use the startScan() method of a WifiManager instance. By reading turn on and off wifi, you already know how to get an instance of the WifiManager. The startScan() method returns a list of ScanResult. Each ScanResult contains information (e.g. capacities, level, and frequency) about a detected Wifi network. To get the list of ScanResult, you need a broadcast receiver that is registered to receive an intent that matches the SCAN_RESULTS_AVAILABLE_ACTION action. In onReceive() method of the broadcast receiver, you can gather detected Wifi once the scan results available using getScanResults() method of the WifiManager.

Now to have example application on detecting nearby Wifi, you create an Android project. In the activity_main.xml, you add a Button and a ListView. The button is clicked to detect nearby wifi networks and the ListView displayed the detected results.




<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/list"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

</LinearLayout>


In the res/layout folder, you create a layout file called list_item.xml for the ListView as shown below.

<?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>


In the MainActivity class, you write code to get an instance of the WifiManager, scan Wifi networks, and display the results in the ListView.

package com.example.scanwifi;
import java.util.ArrayList;
import java.util.List;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity{

WifiManager wifi;
List<ScanResult> wifiList;
WifiReceiver wifiReceiver;
ListView lstView;
ArrayAdapter<String> adapter;
ArrayList<String> wifiResults;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     lstView=(ListView)findViewById(R.id.list);
     wifiResults=new ArrayList<String>();
     adapter=new ArrayAdapter<String>(this, R.layout.list_item,R.id.txt,wifiResults);
     lstView.setAdapter(adapter);
     // Get WifiManager instance
     wifi = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
     // Create WifiReceiver instance
     wifiReceiver = new WifiReceiver();
    
   }

   public void scanWifi(View view){
   // Scan 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();
   // Unregister the wifi receiver
     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();
     }
   }
}


Before you run the example application, don't forget to add CHANGE_WIFI_STATE and ACCESS_WIFI_STATE permissions in the AndroidManifest.xml file of your project.

Android-detect-wifi

Posted by: Dara | post date: 12-15-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.