Always getting -1 in adding new wifiConfiguration to the wifiiManager

空扰寡人 提交于 2020-01-13 11:24:07

问题


I am creating a list of in-range wifis and show it to the user. I want to the user can select each of the items in the list and insert the password in order to connect to the selected SSID.

I wrote this method for wifi connection:

    private WifiConfiguration wifiConf;             /* WifiConfiguration object */
    private WifiManager wifiMgr;                            /* WifiManager object */
    private WifiInfo wifiInfo;                              /* WifiInfo object */


        public boolean connectToSelectedNetwork(String networkSSID, String networkPassword, String securityType) {

    int networkId;
    int SecurityProtocol;

    if (securityType.contains("WEP")) {
      SecurityProtocol = 1;
      Log.i(TAG, "Security:  WEP");
    } else if (securityType.contains("WPA2")) {
      Log.i(TAG, "Security:  WPA2");
      SecurityProtocol = 2;
    } else if (securityType.contains("WPA")) {
      Log.i(TAG, "Security:  WPA");
      SecurityProtocol = 3;
    } else {
      Log.i(TAG, "Security:  OPEN");
      SecurityProtocol = 4;
    }


    // Clear wifi configuration variable
    clearWifiConfig();

    // Sets network SSID name on wifiConf
    wifiConf.SSID = "\"" + networkSSID + "\"";
    Log.i(TAG, "SSID Received: " + wifiConf.SSID);

    switch (SecurityProtocol) {

      // WEP "security".
      case WEP:
        wifiConf.wepKeys[0] = "\"" + networkPassword + "\"";
        wifiConf.wepTxKeyIndex = 0;
        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        break;

      // WAP security. We have to set preSharedKey.
      case WPA2:
        wifiConf.preSharedKey = "\"" + networkPassword + "\"";
        wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

        break;

      case WPA:
        wifiConf.preSharedKey = "\"" + networkPassword + "\"";

      // Network without security.
      case OPEN_NETWORK:
        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        break;
    }

    // Add WiFi configuration to list of recognizable networks
    if ((networkId = wifiMgr.addNetwork(wifiConf)) == -1) {
      Log.i("TAG", "Failed to add network configuration!");
      return false;
    }

    // Disconnect from current WiFi connection
    if (!disconnectFromWifi()) {
      Log.i("TAG", "Failed to disconnect from network!");
      return false;
    }

    // Enable network to be connected
    if (!wifiMgr.enableNetwork(networkId, true)) {
      Log.i("TAG", "Failed to enable network!");
      return false;
    }

    // Connect to network
    if (!wifiMgr.reconnect()) {
      Log.i("TAG", "Failed to connect!");
      return false;
    }

    return true;
  }

But when I call this function in order to connect to the selected wifi I always get false! I debugged it a lot of times and it goes into first if in this method And doesn't connect to the wifi.

Please help me with your answers. Thank you.


回答1:


addNetwork() returns -1 on failure. One common reason for it to fail is when the network was previously configured by the user (through the wifi setting screen) or programmatically, via an app with different Unix User ID (i.e. UID). Recall that every app is associated with a UID.

If the same UID calls addNetwork() for the second time, it will be allowed to override the configuration and the function will succeed.

A similar related behavior is with regards to removing a configuration via the removeNetwork() API. Only the app with the UID that added the configuration can remove it.

This behavior makes sense since you wouldn't want a malicious (or just buggy) app, to override or remove configurations done by either the user or other apps.

So far so good, but there's a complication - when an app is reinstalled (read: uninstalled and then installed again), the OS will provide it with a different UID. Now the same app which previously added the configuration, following the reinstall, can no longer remove or update it. Ouch!

It would have been nice to allow the same application ID to be able to update/remove a network. Oh well.



来源:https://stackoverflow.com/questions/47968837/always-getting-1-in-adding-new-wificonfiguration-to-the-wifiimanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!