This document provides instructions on how to use the WifiManager in Android to control WiFi settings and scan for available networks. It explains how to get the WifiManager instance, start a scan, register a broadcast receiver to detect when scanning is complete, and retrieve the list of scanned networks. It also demonstrates how to check and change the active WiFi state. The code examples show implementing these features to build a simple app with buttons to scan and enable/disable WiFi and display the results.
1. Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : The Wifi manager
2. The Wifi manager
• In this lesson, you will learn to use the Wifi manager.
• For this, you will use the Wifi manager WifiManager.
3. The Wifi manager
• 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);
}
4. The Wifi manager
• 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));
5. The Wifi manager
• 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;
}
}
}
}
12. Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb