[Android] The Wifi manager

In this lesson, you will learn to use the Wifi manager.

For this, you will use the Wifi manager WifiManager.

Use getSystemService() to access to the Wifi manager.

WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

To start the scan of networks, call the method startScan().

wifiManager.startScan();

To control the status active or inactive, use the methods isWifiEnabled() and setWifiEnabled().

if (wifiManager.isWifiEnabled()) {
  wifiManager.setWifiEnabled(false);
} else {
  wifiManager.setWifiEnabled(true);
}

To get notified when the search is finished, register a BroadcastReceiver.

Receiver receiver = new Receiver();
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

This receiver receives the status in the action (SCAN_RESULTS_AVAILABLE_ACTION). Then the SSIDs can be rad with the method getScanResults() of the WifiManager.

public class Receiver extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
      List<ScanResult> results = wifiManager.getScanResults();
      for(int i=0;i<results.size();i++){
        String ssid = results.get(i).SSID;
      }
    }
  }
}

Layout main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button
    android:id="@+id/btnScan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Scan" />
    <Button
        android:id="@+id/btnActivate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Activate / disactivate" />
    <EditText
        android:id="@+id/et_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SSID" />
    <EditText
        android:id="@+id/et_ssid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10" />
</LinearLayout>

File Main.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnScan = (Button)findViewById (R.id.btnScan);
    btnScan.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
      wifiManager.startScan();
      }
    });
    Button btnEnable = (Button)this.findViewById(R.id.btnActivate);
    btnEnable.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        if(wifiManager.isWifiEnabled()) {
          wifiManager.setWifiEnabled(false);
          et_ssid.setText("Disabled");
        } else {
          wifiManager.setWifiEnabled(true);
          et_ssid.setText("Enabled");
        }
      }
    });
      et_ssid = (TextView)findViewById (R.id.et_ssid);
      et_status = (TextView)findViewById (R.id.et_status);
      wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
      Receiver receiver = new Receiver();
      registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
      registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  }
  public class Receiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
      if (intent.getAction().equals (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
        List<ScanResult> results = wifiManager.getScanResults();
        String ssid = "";
        for (int i = 0; i < results.size(); i++) {
          ssid += results.get(i).SSID + ", ";
        }
        et_ssid.setText(ssid);
      }
    }
  }
}

File AndroidManifest.xml

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

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×