WIFI in Android
Working with Wi-Fi in an Android app involves managing Wi-Fi connectivity, scanning for available Wi-Fi networks, and interacting with Wi-Fi configurations. Here’s a guide on how to perform common Wi-Fi operations in Android:
1. Check and Request Wi-Fi Permissions:
Before you can interact with Wi-Fi, you need to request appropriate permissions in your AndroidManifest.xml
file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
The ACCESS_WIFI_STATE
permission allows you to check the Wi-Fi status, CHANGE_WIFI_STATE
permits you to modify Wi-Fi settings, and ACCESS_FINE_LOCATION
is often required to scan for available Wi-Fi networks (particularly in Android 10 and later).
2. Check Wi-Fi Connectivity:
You can check if the device is connected to a Wi-Fi network as follows:
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isConnected()) {
// Device is connected to Wi-Fi
} else {
// Device is not connected to Wi-Fi
}
3. Enable/Disable Wi-Fi:
To programmatically enable or disable Wi-Fi, you can use the following code:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// To enable Wi-Fi
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
// To disable Wi-Fi
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}
Make sure to request the necessary permissions for changing Wi-Fi state.
4. Scan for Available Wi-Fi Networks:
To scan for available Wi-Fi networks, you can use the WifiManager
class. Note that you need the ACCESS_FINE_LOCATION
permission to scan for Wi-Fi networks in Android 10 and later. Here’s a basic example:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
List<ScanResult> scanResults = wifiManager.getScanResults();
for (ScanResult result : scanResults) {
String ssid = result.SSID;
String bssid = result.BSSID;
int signalStrength = result.level;
// Handle the scan result data
}
5. Connect to a Wi-Fi Network:
To programmatically connect to a specific Wi-Fi network, you can use the following code:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + networkSSID + "\"";
wifiConfig.preSharedKey = "\"" + networkPassword + "\"";
int networkId = wifiManager.addNetwork(wifiConfig);
if (networkId != -1) {
wifiManager.enableNetwork(networkId, true);
}
Replace networkSSID
and networkPassword
with the appropriate network credentials.
6. Disconnect from a Wi-Fi Network:
To disconnect from the currently connected Wi-Fi network, you can use:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.disconnect();
7. Check if Wi-Fi is Available:
You can check if Wi-Fi is available on the device as follows:
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isAvailable()) {
// Wi-Fi is available on the device
} else {
// Wi-Fi is not available on the device
}
8. Monitor Wi-Fi State Changes:
You can register a BroadcastReceiver
to listen for Wi-Fi state changes. For example, to listen for Wi-Fi connection changes:
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo != null && networkInfo.isConnected()) {
// Wi-Fi connection established
} else {
// Wi-Fi connection lost
}
}
}
}
Don’t forget to declare the receiver in your AndroidManifest.xml file and request the necessary permissions.
These are the basic operations for working with Wi-Fi in an Android app. Depending on your app’s requirements, you may need to implement more advanced features, such as Wi-Fi configuration, network discovery, or managing Wi-Fi hotspots.