![](/img/trans.png)
[英]What is the android equivalent to an iOS uitableviewstylegrouped
[英]What is the Android equivalent of the CaptiveNetwork API (iOS)?
我希望注册用户连接到WiFi网络并进行无缝身份验证。 当用户正在运行应用程序并且检测到特定的SSID时,应该会发生这种情况。
CaptiveNetwork编程接口允许应用程序与Captive Network Support交互,Captive Network Support是负责在提供Internet访问之前检测需要用户交互的网络的系统组件。 这些网络是机场和酒店等公共场所中最常见的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上添加权限
PS:三星设备在4.4和早期版本的SSID扫描中存在一些问题,请小心。 PPS:对于身份验证,您必须对AP连接使用特定的配置(以我为例)。 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.