diff options
author | Ang Li <angli@google.com> | 2017-07-18 11:59:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-18 11:59:21 -0700 |
commit | 3b48f2df00a2204e9ce3cba7ec538d4097e42018 (patch) | |
tree | de871cde1638d665fe27748b3dda5d1f8bd83b53 /src/main | |
parent | 5d45a95261c0152059dfdb7105307aebe2e2f10c (diff) | |
download | mobly-bundled-snippets-3b48f2df00a2204e9ce3cba7ec538d4097e42018.tar.gz |
Do not attempt to modify a network in wifiConnect. (#68)
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java index 32f6a8d..6614904 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java @@ -193,6 +193,7 @@ public class WifiManagerSnippet implements Snippet { throws InterruptedException, JSONException, WifiManagerSnippetException { Log.d("Got network config: " + wifiNetworkConfig); WifiConfiguration wifiConfig = JsonDeserializer.jsonToWifiConfig(wifiNetworkConfig); + String SSID = wifiConfig.SSID; // Return directly if network is already connected. WifiInfo connectionInfo = mWifiManager.getConnectionInfo(); if (connectionInfo.getNetworkId() != -1 && connectionInfo.getSSID().equals(wifiConfig.SSID)) @@ -200,13 +201,24 @@ public class WifiManagerSnippet implements Snippet { Log.d("Network " + connectionInfo.getSSID() + " is already connected."); return; } - // If the network is already added but not connected, update the configuration first. + int networkId; + // If this is a network with a known SSID, connect with the existing config. + // We have to do this because in N+, network configs can only be modified by the UID that + // created the network. So any attempt to modify a network config that does not belong to us + // would result in error. WifiConfiguration existingConfig = getExistingConfiguredNetwork(wifiConfig.SSID); if (existingConfig != null) { - Log.d("Update the configuration of network " + existingConfig.SSID + "."); - mWifiManager.removeNetwork(existingConfig.networkId); + Log.w( + "Connecting to network \"" + + existingConfig.SSID + + "\" with its existing configuration: " + + existingConfig.toString()); + wifiConfig = existingConfig; + networkId = wifiConfig.networkId; + } else { + // If this is a network with a new SSID, add the network. + networkId = mWifiManager.addNetwork(wifiConfig); } - int networkId = mWifiManager.addNetwork(wifiConfig); mWifiManager.disconnect(); if (!mWifiManager.enableNetwork(networkId, true)) { throw new WifiManagerSnippetException( @@ -216,8 +228,7 @@ public class WifiManagerSnippet implements Snippet { throw new WifiManagerSnippetException( "Failed to reconnect to Wi-Fi network of ID: " + networkId); } - if (!Utils.waitUntil( - () -> mWifiManager.getConnectionInfo().getSSID().equals(wifiConfig.SSID), 90)) { + if (!Utils.waitUntil(() -> mWifiManager.getConnectionInfo().getSSID().equals(SSID), 90)) { throw new WifiManagerSnippetException( "Failed to connect to Wi-Fi network " + wifiNetworkConfig.toString() |