0

My app needs to work on workplaces/airports that have a free public wifi network.

I would like to inform the user when he need to approve the wifi term of use through a web browser.

I tried to extract information, But didn't find any attribute / info to isolate this case

public static void haveNetworkConnection(Context mContext) {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();


    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi
            Toast.makeText(mContext, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
            printWifiInformation(activeNetwork);
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile provider's data plan
            Toast.makeText(mContext, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(mContext, "Not connected", Toast.LENGTH_SHORT).show();
    }
}

public static void printWifiInformation(NetworkInfo activeNetwork){

    System.out.println("************************************************************************************");
    System.out.println("1 . describeContents         " + activeNetwork.describeContents());
    System.out.println("2 . getDetailedState         " + activeNetwork.getDetailedState());
    System.out.println("3 . getExtraInfo             " + activeNetwork.getExtraInfo());
    System.out.println("4 . getReason                " + activeNetwork.getReason());
    System.out.println("5 . getState                 " + activeNetwork.getState());
    System.out.println("6 . getSubtype               " + activeNetwork.getSubtype());
    System.out.println("7 . getSubtypeName           " + activeNetwork.getSubtypeName());
    System.out.println("8 . getSubtypeName           " + activeNetwork.getSubtypeName());
    System.out.println("9 . getType                  " + activeNetwork.getType());
    System.out.println("10. getTypeName              " + activeNetwork.getTypeName());
    System.out.println("11. isAvailable              " + activeNetwork.isAvailable());
    System.out.println("12. isConnected              " + activeNetwork.isConnected());
    System.out.println("13. isConnectedOrConnecting  " + activeNetwork.isConnectedOrConnecting());
    System.out.println("14. isFailover               " + activeNetwork.isFailover());
    System.out.println("15. isRoaming                " + activeNetwork.isRoaming());
    System.out.println("************************************************************************************");

}

I got the same result when I checked before and after clicking the web browser. Please advise, Thanks, Luther

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
koko
  • 59
  • 1
  • 3

1 Answers1

0

You should start by doing a connectivity check to verify that you are inside a Captive Portal walled garden.

There is a pretty good explanation of that here on SO.

Still, this does not give you info on the captive portal itself.

So you will have to manually handle the HttpUrlConnection response headers and look for a redirect

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     if (!url.getHost().equals(urlConnection.getURL().getHost())) {

      // we were redirected! Kick the user out to the browser to sign on?
     }
     ...
   } finally {
     urlConnection.disconnect();
   }

The official documentation is generous enough to provide a series of examples which include Handling Network Sign-On, Http Authentication as well as handling cookies. You should give it a look. Hope this helps!

Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39