diff options
Diffstat (limited to 'src/main/java/com/google')
-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() |