Android WifiManager.addNetwork()返回-1

问题描述 投票:18回答:4

我正在编写一个将连接到特定WPA接入点的Android应用程序,当连接时,它将发出一个http调用。它不会保存网络配置。我已经阅读了连接到wifi网络上几乎所有关于堆栈溢出的帖子,但找不到对我有用的答案。这是我用来连接的代码..

    WifiConfiguration wc = new WifiConfiguration();
    wc.allowedAuthAlgorithms.clear();
    wc.allowedGroupCiphers.clear();
    wc.allowedPairwiseCiphers.clear();
    wc.allowedProtocols.clear();
    wc.allowedKeyManagement.clear();
    wc.SSID = "\"".concat("<ssid>").concat("\"");
    wc.preSharedKey = "\"".concat("<password>").concat("\"");
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
    wc.priority = 0;
    //wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.ENABLED;
    // connect to and enable the connection
    WifiManager wifiManager = (WifiManager) getSystemService(this.WIFI_SERVICE);
    int netId = wifiManager.addNetwork(wc);
    boolean wifiEnabled = wifiManager.enableNetwork(netId, true);
    wifiManager.setWifiEnabled(true);
    Log.d("opener", "addNetwork returned " + netId);
    if (netId > 0) {
        wifiId = netId;
    }

但是netId总是-1。我在两部不同的手机上试过它(ICS:HTC Rezound和GingerBread:摩托罗拉DroidX)。两者显示完全相同的结果。我究竟做错了什么?

编辑:我尝试使用WPA2访问点的相同代码,并得到了非常奇怪的结果。运行此代码时,第一次返回-1,但如果我第二次调用同一方法并延迟1秒,则返回有效的netId。所以问题是

  1. 为什么上面的代码没有连接到wpa?
  2. 在wpa2中,为什么我需要两次调用上面的方法来连接?编辑:我发现我必须多次连接才能连接。有时连接需要3-4次。所以现在我循环直到添加config返回> 0 id。
android wifi android-wifi wifimanager wificonfiguration
4个回答
14
投票

可能有点晚,但尝试连接到Open / WPA / WPA2 / WEP安全网络

    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = selectedNetwork.SSID();
    wifiConfig.status = WifiConfiguration.Status.DISABLED;
    wifiConfig.priority = 40;

    // Dependent on the security type of the selected network
    // we set the security settings for the configuration
    if (/*Open network*/) {
        // No security
        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);
    } else if (/*WPA*/ || /*WPA2*/) {
        //WPA/WPA2 Security
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        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);
        wifiConfig.preSharedKey = "\"".concat(password).concat("\"");
    } else if (/*WEP*/) {
        // WEP Security
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

        if (getHexKey(password)) wifiConfig.wepKeys[0] = password;
        else wifiConfig.wepKeys[0] = "\"".concat(password).concat("\"");
        wifiConfig.wepTxKeyIndex = 0;
    }

    // Finally we add the new configuration to the managed list of networks
    int networkID = wifiMan.addNetwork(wifiConfig);

显然,根据适用情况为不同的安全类型设置自己的变量。连接到WEP安全网络的关键(原谅双关语)是getHexKey方法,如下所示。

/**
 * WEP has two kinds of password, a hex value that specifies the key or
 * a character string used to generate the real hex. This checks what kind of
 * password has been supplied. The checks correspond to WEP40, WEP104 & WEP232
 * @param s
 * @return
 */
private static boolean getHexKey(String s) {
    if (s == null) {
        return false;
    }

    int len = s.length();
    if (len != 10 && len != 26 && len != 58) {
        return false;
    }

    for (int i = 0; i < len; ++i) {
        char c = s.charAt(i);
        if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
            continue;
        }
        return false;
    }
    return true;
}

9
投票

我发现如果共享密钥少于8个字符,它将返回-1。


4
投票

我有同样的问题。我发现在你打电话给addNetwork时你的wifi必须打开。


4
投票

我只是遇到了同样的问题。看似一切都很好,但后来 - 事​​实并非如此。

我发现的是:

  • 除非请求者正在运行并连接到,否则Android WifiStateMachine将不允许您添加或修改网络。这会影响在启动和服务运行时运行的服务,即使WIFI已关闭。
  • Android WifiConfigStore通过UID跟踪wifi网络的所有者。这意味着您可能无法修改由其他进程创建的网络。

添加网络的正确方法是:

  1. 检查是否启用了WIFI网络。如果没有,请致电WifiManager.setWifiEnabled(true)
  2. 等到WifiManager.pingSupplicant()返回true。
  3. 创建并填充新的WifiConfiguration,然后将其传递给WifiManager.addNetwork()。确保返回的值不是(-1)。
  4. (可选)使用addNetwork()返回的值作为WifiConfiguration.enableNetwork()调用的参数(除非它是-1)。请注意,布尔参数表示disableOthers并且应该为false,除非您有权修改所有其他网络:如果将其设置为true,则可能在内部失败。

这应该允许您以编程方式添加(和连接)到新的Wifi网络。


0
投票

问题是您正在尝试添加已存在的网络配置。你打电话的时候:

int netId = wifiManager.addNetwork(wc);

如果已存在具有相同SSID的网络配置,它将失败(返回-1)。因此,您需要做的是检查netId是否为-1,如果是,则遍历已配置的网络,搜索具有相同SSID的网络,一旦找到,返回networkId。

科特林:

var netId = wifiManager.addNetwork(conf)
if (netId == -1) netId = wifiManager.configuredNetworks?.let {
    it.firstOrNull { it.SSID.trim('"') == ssid.trim('"') }?.networkId ?: -1
}
wifiManager.enableNetwork(netId, true)
© www.soinside.com 2019 - 2024. All rights reserved.