• May 4, 2024

Android Wifi Communication Tutorial

Android – Wi-Fi – Tutorialspoint

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 −
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 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(AN_RESULTS_AVAILABLE_ACTION));
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 method of list. Its syntax is given below −
List wifiScanList = tScanResults();
String data = (0). toString();
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 −
Method & Description
1
addNetwork(WifiConfiguration config)
This method add a new network description to the set of configured networks.
2
createWifiLock(String tag)
This method creates a new WifiLock.
3
disconnect()
This method disassociate from the currently active access point.
4
enableNetwork(int netId, boolean disableOthers)
This method allow a previously configured network to be associated with.
5
getWifiState()
This method gets the Wi-Fi enabled state
6
isWifiEnabled()
This method return whether Wi-Fi is enabled or disabled.
7
setWifiEnabled(boolean enabled)
This method enable or disable Wi-Fi.
8
updateNetwork(WifiConfiguration config)
This method update the network description of an existing configured network.
Example
Here is an example demonstrating the use of WIFI. It creates a basic application that open your wifi and close your wifi
To experiment with this example, you need to run this on an actual device on which wifi is turned on.
Steps
Description
You will use Android studio to create an Android application under a package application.
Modify src/ file to add WebView code.
Modify the res/layout/activity_main to add respective XML components
Modify the to add the necessary permissions
Run the application and choose a running android device and install the application on it and verify the results.
Following is the content of the modified main activity file src/
package application;
import;
import ntext;
public class MainActivity extends Activity {
Button enableButton, disableButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super. onCreate(savedInstanceState);
setContentView();
enableButton=(Button)findViewById();
disableButton=(Button)findViewById();
tOnClickListener(new OnClickListener(){
public void onClick(View v){
WifiManager wifi = (WifiManager) getSystemService(Context. WIFI_SERVICE);
tWifiEnabled(true);}});
tWifiEnabled(false);}});}}
Following is the modified content of the xml res/layout/



Leave a Reply

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