CaptiveNetwork API (iOS) 的 Android 等价物是什么?
What is the Android equivalent of the CaptiveNetwork API (iOS)?
我希望注册用户能够连接到 WiFi 网络并进行无缝身份验证。当用户是 运行 应用程序并且检测到特定的 SSID 时,应该会发生这种情况。
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.
首先你必须检查你的网络是否可用
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: Samsung 设备在4.4 及早期版本扫描SSID 时出现问题,请慎重。
P.P.S:对于身份验证,您必须使用特定配置进行 AP 连接,在我的情况下它是打开的。
问候
我希望注册用户能够连接到 WiFi 网络并进行无缝身份验证。当用户是 运行 应用程序并且检测到特定的 SSID 时,应该会发生这种情况。
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.
首先你必须检查你的网络是否可用
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: Samsung 设备在4.4 及早期版本扫描SSID 时出现问题,请慎重。 P.P.S:对于身份验证,您必须使用特定配置进行 AP 连接,在我的情况下它是打开的。 问候