Android WIFI - Androhub

Android WIFI

Android WIFI

Android allows applications to access to view the access the state of the wireless connections at very low level. Application can access almost all the information of a wifi connection.

The information that an application can access includes connected network’s link speed, IP address, negotiation state, other networks 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 −

In order to scan a list of wireless networks, you also need to register your BroadcastReceiver. It can be registered using registerReceiver method with argument of your receiver class object. Its syntax is given below −

The wifi scan can be start 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() of list. Its syntax is given below −

Apart from just Scanning, you can have more control over your WIFI by using the methods defined in WifiManager class. They are listed as follows −

MethodDescription
addNetwork(WifiConfiguration config)This method add a new network description to the set of configured networks.
createWifiLock(String tag)This method creates a new WifiLock.
disconnect()This method disassociate from the currently active access point.
enableNetwork(int netId, boolean disableOthers)This method allow a previously configured network to be associated with.
getWifiState()This method gets the Wi-Fi enabled state.
isWifiEnabled()This method return whether Wi-Fi is enabled or disabled.
setWifiEnabled(boolean enabled)This method enable or disable Wi-Fi.
updateNetwork(WifiConfiguration config)This method update the network description of an existing configured network.

Example

In this tutorial, we are going to learn how to use WIFI by enabling and disabling it and listing scanned wifi’s in ListView.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial.

3. To work on WiFi we need to set certain permission in Manifest file so open manifest file and add this lines to it.

4. Now, create a xml layout naming activity_main.xml  for basic layout. This layout contains some buttons and one listview for displaying Wifi’s.

5. Now, come to your MainActivity.java and add the following code. In this code we used above discussed method to enable, disable wifi and list scanned wifi over ListView.

6. Finally, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *