Captive Portal inside Android App

Viewed 4439
3 Answers

WifiPortalAutoLog is an example project that you can use

As describe in this answer: Here is an example scenario:

  1. Device connects to captive Wi-Fi portal
  2. System displays a captive portal notification
  3. User touches the notification
  4. System displays the implicit intent app chooser
  5. User selects SignInActivity
  6. MainActivity is launched

In MainActivity You may access the extras mentioned in the ConnectionManager.ACTION_CAPTIVE_PORTAL_SIGN_IN :

if (ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN.equals(intent.getAction())) {

get captivePortal from bundle to communicate with the system about the outcome of the sign in:

captivePortal = intent.getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);

Use the ConnectivityManager.EXTRA_NETWORK extra (which has type Network) to communicate with the portal (i.e. pass sign in tokens):

net = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);

Load url in WebView and also remember set intent filter in manifest:

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.net.conn.CAPTIVE_PORTAL" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

All android devices dont work the same for captive portal. You need to check as per your app requirements. Samsung uses the android captive portal mechanism by redirecting portal to their system, handles it internally and blocks other captive portal requests and shows on its own browser. Like Samsung, some other vendors like Huawei and more too use their own mechanism and only some including Oneplus, Mi, Htc and others use default mechanism and hence can be redirected to the app using the portal intent filter but not for else.

<intent-filter>
  <action android:name="android.net.conn.CAPTIVE_PORTAL"/>
  <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

One link explaining it clearly is here:

https://community.arubanetworks.com/t5/Wireless-Access/Samsung-Captive-Portal-Detection/m-p/405934#M78972

Related