javaandroidwifiandroid-13android-connectivitymanager

How to check if Android 13 app is connected to WIFI with API v33


I have an Android app built with Java, running on Android 13 and API version 33.

The app serves a webView, though I want to be able to serve up a local asset if WIFI is not connected (I do not need to worry about Cellular connections - only WIFI) as soon as the app launches.

My current setup is like so:

Permissions within:

AndroidManifest.xml:

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

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private WebView myWebView;
    private Context context;

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = findViewById(R.id.activity_main_webview);

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setGeolocationEnabled(true);

        myWebView.setWebViewClient(new MyWebViewClient());

        if (DetectInternetConnection.checkIfAppIsConnectedToWIFI(this)) {
          myWebView.loadUrl("https://remote-web-address");
        } else {
          myWebView.loadUrl("file:///android_asset/local-html-page.html");
        }

        // and so on...

DetectInternetConnection.java:

package com.mycompany.app;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;

class DetectInternetConnection {

  public static boolean checkIfAppIsConnectedToWIFI(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
        if (capabilities != null) {
            if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                return true;
            }
        }
    }

    return false;
  }
}

However my app always loads the remote webview content. As the checkIfAppIsConnectedToWIFI method always returns true.

Even if I turn off WIFI on the machine which the emulator is running on, or the device itself.


Solution

  • The method capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) checks if the current network supports transport over WiFi, not whether WiFi is currently active and connected.

    Instead, you should use capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) and capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) together to check if there is an active WiFi connection with the internet.

    public static boolean checkIfAppIsConnectedToWIFI(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            if (capabilities != null) {
                if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    return true;
                }
            }
        }
    
        return false;
    }