Android WiFi: Android - LeaVe my baThRoom at-least !
WiFi is a technology for wireless local area networking with devices based on the IEEE 802.11 standards. Devices that can use Wi-Fi technology include personal computers, video-game consoles, smartphones, digital cameras, tablet computers, digital audio players and modern printers. Wi-Fi compatible devices can connect to the Internet via a WLAN and a wireless access point. Such an access point (or hotspot) has a range of about 20 meters (66 feet) indoors and a greater range outdoors. Hotspot coverage can be as small as a single room with walls that block radio waves, or as large as many square kilometers achieved by using multiple overlapping access points.
Android allows applications to access to view the access the state of the wireless connections at very low level. Android provides WiFi API through which applications can communicate with the lower-level wireless stack that provides WiFi network access. Almost all information from the device supplicant is available, including the connected network's link speed, IP address, negotiation state, and more, plus information about other networks that are available. Some other API features include the ability to scan, add, save, terminate and initiate WiFi connections.
WifiManager is the primary API for managing all aspects of WiFi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It's Syntax is given below:-
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiManager class provides different methods to control WiFi activities:-
- int addNetwork(WifiConfiguration config): Add a new network description to the set of configured networks.
- WifiManager.MulticastLock createMulticastLock(String tag): Create a new MulticastLock
- WifiManager.WifiLock createWifiLock(String tag): This method creates a new WifiLock.
- boolean disconnect(): This method disassociate from the currently active access point.
- boolean enableNetwork(int netId, boolean disableOthers): This method allow a previously configured network to be associated with.
- int getWifiState(): This method gets the Wi-Fi enabled state
- boolean isWifiEnabled(): This method return whether Wi-Fi is enabled or disabled.
- boolean setWifiEnabled(boolean enabled): This method enable or disable Wi-Fi.
- int updateNetwork(WifiConfiguration config): This method update the network description of an existing configured network.
- boolean disableNetwork (int netId): Disable a configured network.
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 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
String data = wifiScanList.get(0).toString();
Example
Let's see the simple example of wifi to enable and disable the wifi service.
To run this example you need actual Android device.
- You will use Android studio to create an Android application under a package net.suven.android.androidwifi.
- Modify src/MainActivity.java file to add necessary code.
- Modify the res/layout/activity_main to add respective XML components.
- Modify the AndroidManifest.xml 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 src/MainActivity.java
Following is the content of activity_main.xml
Following is the content of AndroidManifest.xml
Following is the output of Application
Comments
Post a Comment