androidioscaptivenetwork

What is the Android equivalent of the CaptiveNetwork API (iOS)?


I want a registered user to connect to the WiFi network and authenticate seamlessly. This should happen when the user is running the app, and an specific SSID is detected.

The CaptiveNetwork programming interface allows an application to interact with Captive Network Support, the system component responsible for detecting networks that require user interaction before providing internet access. These networks are most commonly WiFi hotspots in public locations such as airports and hotels.

See: https://developer.apple.com/library/ios/documentation/SystemConfiguration/Reference/CaptiveNetworkRef/index.html


Solution

  • First of all you must check if available your network

    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (!wifiManager.isWifiEnabled()) {
                wifiManager.setWifiEnabled(true);
            }
                wifiResultsReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(wifiResultsReceiver);
                    List<ScanResult> wifiList;
                    wifiList = wifiManager.getScanResults();
                    for (int i = 0; i < wifiList.size(); i++) {
                        String[] networkInfo = wifiList.get(i).toString().split(",");
                        if (networkInfo[0].trim().equals(YOUR_SSID)) {
                           //Check if your ssid is available &
                            connect();
                        }
                }
            };
            context.registerReceiver(wifiResultsReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    
            wifiManager.startScan();
    

    Then if network is available, you can connect to your SSID(this is one of my config):

    public static WifiConfiguration getConnectionConfig(Context context, String ssidName) {
            Validator.validateArgument(context, "Context can't be null");
            WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
            if (!wifiManager.isWifiEnabled()) wifiManager.setWifiEnabled(true);
            WifiConfiguration wifiConfig = new WifiConfiguration();
            wifiConfig.SSID = ssidName;
            List<WifiConfiguration> curentList = wifiManager.getConfiguredNetworks();
            if (curentList != null && !curentList.isEmpty()) {
                curentList.removeAll(Collections.singleton(null));
                for (int i = 0; i < curentList.size(); i++) {
                    WifiConfiguration item = curentList.get(i);
                    if (ssidName.equals(item.SSID)) {
                        wifiManager.removeNetwork(item.networkId);
                    }
                }
            }
            wifiConfig.status = WifiConfiguration.Status.DISABLED;
            wifiConfig.priority = 100;
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            wifiConfig.allowedAuthAlgorithms.clear();
            wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            return wifiConfig;
        }
    
    private void connect() {
    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int networkID = wifiManager.addNetwork(getConnectionConfig(context, YOUR_SSID));
         wifiManager.disconnect();
         wifiManager.enableNetwork(networkID, true);
         wifiManager.reconnect();
    }
    

    Don't forget add permissions on AndroidManifest

    P.S: Samsung devices has some problem with SSID scanning in 4.4 and early version, be carefully. P.P.S: for auth you must use specific configuration for AP connection in my case it open. Regards