How to Detect When Wifi Connection Has Been Established in Android

Android WIFI How To Detect When Specific WIFI Connection is Available

You can use BroadcastReceiver to find out that wifi network has changed:

BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver();

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
context.registerReceiver(broadcastReceiver, intentFilter);

The BroadcastReceiver may look like this.
And to check for specific MAC address see the checkConnectedToDesiredWifi() method bellow.

public class WifiBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) {
SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (SupplicantState.isValidState(state)
&& state == SupplicantState.COMPLETED) {

boolean connected = checkConnectedToDesiredWifi();
}
}
}

/** Detect you are connected to a specific network. */
private boolean checkConnectedToDesiredWifi() {
boolean connected = false;

String desiredMacAddress = "router mac address";

WifiManager wifiManager =
(WifiManager) context.getSystemService(Context.WIFI_SERVICE);

WifiInfo wifi = wifiManager.getConnectionInfo();
if (wifi != null) {
// get current router Mac address
String bssid = wifi.getBSSID();
connected = desiredMacAddress.equals(bssid);
}

return connected;
}
}

Detect whether there is an Internet connection available on Android

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available or not.

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

You will also need:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your android manifest.

Edit:

Note that having an active network interface doesn't guarantee that a particular networked service is available. Network issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server. For instance you can't tell for sure if your app can reach Twitter until you receive a valid response from the Twitter service.

How do I see if Wi-Fi is connected on Android?

You should be able to use the ConnectivityManager to get the state of the Wi-Fi adapter. From there you can check if it is connected or even available.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
// Do whatever
}

NOTE: It should be noted (for us n00bies here) that you need to add

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

to your

AndroidManifest.xml for this to work.

NOTE2: public NetworkInfo getNetworkInfo (int networkType) is now deprecated:

This method was deprecated in API level 23. This method does not
support multiple connected networks of the same type. Use
getAllNetworks() and getNetworkInfo(android.net.Network) instead.

NOTE3: public static final int TYPE_WIFI is now deprecated:

This constant was deprecated in API level 28.
Applications should instead use NetworkCapabilities.hasTransport(int) or requestNetwork(NetworkRequest, NetworkCallback) to request an appropriate network. for supported transports.

Android Get Status of Wifi Connection

Ok, I got the solution:

val isWifiOn = with(getConnectivityManager()) {
getNetworkCapabilities(activeNetwork)?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
}

Demo: https://youtu.be/OHFrtXVW4x4

Detect when connected/disconntected to Wi-Fi

nothing happens. What am I doing wrong?

As far as I can see you have not declared the BroadcastReceiver, adding permission alone would not trigger your broadcast receiver.

My solution is based on declaring it via manifest, but that said your problem is I can not see you have declared broadcaster class in your code, so you can chose declare it in manifest or in your code.

So try to declare it and it should works. I have tested your code with both declaration and it works:

<receiver android:name=".WifiScanReceiver">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>

Or declare it programmatically new WifiScanReceiver() in onCreate method:

final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(new WifiScanReceiver(), filters);

You will need this also (reference) in your WifiScanReceiver:

public void onReceive(Context context, Intent intent) {

ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

if (isWiFi) {
Log.i("TAG", "trigged");
}
}


Related Topics



Leave a reply



Submit