什么是Android版的CaptiveNetwork API(iOS)



我希望注册用户能够连接到WiFi网络并进行无缝身份验证。当用户正在运行应用程序,并且检测到特定的SSID时,应该发生这种情况。

Captive Network编程接口允许应用程序与Captive Network Support交互,后者是负责在提供互联网访问之前检测需要用户交互的网络的系统组件。这些网络是机场和酒店等公共场所最常见的WiFi热点。

请参阅:https://developer.apple.com/library/ios/documentation/SystemConfiguration/Reference/CaptiveNetworkRef/index.html

首先,您必须检查网络是否可用

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();

然后,如果网络可用,您可以连接到您的SSID(这是我的配置之一):

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();
}

不要忘记在AndroidManifest 上添加权限

p.S:三星设备在4.4和早期版本中有一些SSID扫描问题,请小心。P.P.S:对于auth,您必须使用AP连接的特定配置,在我的情况下,它是打开的。问候

相关内容

  • 没有找到相关文章

最新更新