Android allows applications to access the state of the wireless connections at a very low level. Application can access almost all the information of a wifi connection. The information that an application can access includes the connected network’s link speed,IP address, negotiation state, and other network information. Applications can also scan, add, save, terminate and initiate Wi-Fi connections.
Android provides WifiManager API to manage all aspects of WIFI connectivity. We can instantiate this class by calling getSystemService method. Its syntax is given below −
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
In order to scan a list of wireless networks, you also need to register your BroadcastReceiver. It can be registered using registerReceiver method with the argument of your receiver class object. Its syntax is given below −
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
The wifi scan can be started by calling the startScan method of the WifiManager class. This method returns a list of ScanResult objects. You can access any object by calling the get method of list. Its syntax is given below −
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();
Sr.No | Method & Description |
1 | addNetwork(WifiConfiguration config)This method adds a new network description to the set of configured networks. |
2 | createWifiLock(String tag)This method creates a new WifiLock. |
3 | disconnect()This method disassociates from the currently active access point. |
4 | enableNetwork(int netId, boolean disableOthers)This method allows a previously configured network to be associated with. |
5 | getWifiState()This method gets the Wi-Fi enabled state |
6 | isWifiEnabled()This method returns whether Wi-Fi is enabled or disabled. |
7 | setWifiEnabled(boolean enabled)This method enables or disables Wi-Fi. |
8 | updateNetwork(WifiConfiguration config)This method updates the network description of an existing configured network. |