棉花糖中的 WifiManager addNetwork return -1
WifiManager addNetwork return -1 in Marshmallow
在我的应用程序中,我想连接到特定的 wifi 连接。我的应用程序在版本 19 之前工作正常。当安装应用程序 android 6.0 时,addNetwork 总是 return "-1".
下面是我正在使用的代码。
private int configureNetworkAndReturnId() {
WifiConfiguration config = new WifiConfiguration();
config.BSSID = "xx:xx:xx:xx:xx:xx";
config.SSID = "\"" + "Name" + "\"";
config.priority = 1;
config.status = WifiConfiguration.Status.ENABLED;
// Set connection configuration to require encryption
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.preSharedKey = "xxxx";
final int newId = wifiManager.addNetwork(config);
Log.d(TAG, "Created new network configuration, ID: " + newId + ", Config: " + config);
return newId;
}
由于上面的方法是 returning -1,当我调用 boolean isEnabled = wifiManager.enableNetwork(id, true);
应用程序时,应用程序被触发。
有人遇到同样的问题吗?
您尝试连接的 Wifi 网络是否由其他应用配置?您可以尝试从“设置”中忘记 Wifi 网络,然后尝试。
查看 Marshmallow 发行说明。这里有一个变化,如果你的App没有配置Wifi,那么就不能修改。
如果网络id为-1,则从已配置的网络中读取网络id。如果它在网络列表中可用,它 return id。
int newId = wifiManager.addNetwork(config);
if (newId == -1) {
// Get existed network id if it is already added to WiFi network
newId = getExistingNetworkId(SSID);
Log.d(TAG, "getExistingNetworkId: " + newId);
}
private int getExistingNetworkId(String SSID) {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
if (configuredNetworks != null) {
for (WifiConfiguration existingConfig : configuredNetworks) {
if (SSID.equalsIgnoreCase(existingConfig.SSID)) {
return existingConfig.networkId;
}
}
}
return -1;
}
在我的应用程序中,我想连接到特定的 wifi 连接。我的应用程序在版本 19 之前工作正常。当安装应用程序 android 6.0 时,addNetwork 总是 return "-1".
下面是我正在使用的代码。
private int configureNetworkAndReturnId() {
WifiConfiguration config = new WifiConfiguration();
config.BSSID = "xx:xx:xx:xx:xx:xx";
config.SSID = "\"" + "Name" + "\"";
config.priority = 1;
config.status = WifiConfiguration.Status.ENABLED;
// Set connection configuration to require encryption
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.preSharedKey = "xxxx";
final int newId = wifiManager.addNetwork(config);
Log.d(TAG, "Created new network configuration, ID: " + newId + ", Config: " + config);
return newId;
}
由于上面的方法是 returning -1,当我调用 boolean isEnabled = wifiManager.enableNetwork(id, true);
应用程序时,应用程序被触发。
有人遇到同样的问题吗?
您尝试连接的 Wifi 网络是否由其他应用配置?您可以尝试从“设置”中忘记 Wifi 网络,然后尝试。
查看 Marshmallow 发行说明。这里有一个变化,如果你的App没有配置Wifi,那么就不能修改。
如果网络id为-1,则从已配置的网络中读取网络id。如果它在网络列表中可用,它 return id。
int newId = wifiManager.addNetwork(config);
if (newId == -1) {
// Get existed network id if it is already added to WiFi network
newId = getExistingNetworkId(SSID);
Log.d(TAG, "getExistingNetworkId: " + newId);
}
private int getExistingNetworkId(String SSID) {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
if (configuredNetworks != null) {
for (WifiConfiguration existingConfig : configuredNetworks) {
if (SSID.equalsIgnoreCase(existingConfig.SSID)) {
return existingConfig.networkId;
}
}
}
return -1;
}