首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >什么是安卓的CaptiveNetwork API (iOS)?

什么是安卓的CaptiveNetwork API (iOS)?
EN

Stack Overflow用户
提问于 2015-11-22 19:21:20
回答 1查看 511关注 0票数 1

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-22 20:22:25

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

代码语言:javascript
复制
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(这是我的配置之一):

代码语言:javascript
复制
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连接使用特定的配置,在我的示例中,它是打开的。问候

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33859125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档