22
22
import android .os .Build ;
23
23
24
24
public class WifiConfigUtil {
25
+ private static final int INVALID_NETWORK_ID = -1 ;
25
26
26
27
/**
27
28
* Save or replace the WiFi configuration.
@@ -33,23 +34,37 @@ public class WifiConfigUtil {
33
34
public static boolean saveWifiConfiguration (
34
35
Context context , WifiConfiguration wifiConfiguration ) {
35
36
WifiManager wifiManager = (WifiManager ) context .getSystemService (Context .WIFI_SERVICE );
36
- return wifiConfiguration .networkId == -1
37
- ? addWifiNetwork (wifiManager , wifiConfiguration )
38
- : updateWifiNetwork (wifiManager , wifiConfiguration );
37
+ final int networkId ;
38
+ if (wifiConfiguration .networkId == INVALID_NETWORK_ID ) {
39
+ networkId = addWifiNetwork (wifiManager , wifiConfiguration );
40
+ } else {
41
+ networkId = updateWifiNetwork (wifiManager , wifiConfiguration );
42
+ }
43
+ if (networkId == INVALID_NETWORK_ID ) {
44
+ return false ;
45
+ }
46
+ wifiManager .enableNetwork (networkId , /* disableOthers= */ false );
47
+ return true ;
39
48
}
40
49
41
- private static boolean addWifiNetwork (
50
+ /**
51
+ * Adds a new Wifi configuration, returning the configuration's networkId, or
52
+ * {@link #INVALID_NETWORK_ID} if the operation fails.
53
+ */
54
+ private static int addWifiNetwork (
42
55
WifiManager wifiManager , WifiConfiguration wifiConfiguration ) {
43
56
// WifiManager APIs are marked as deprecated but still explicitly supported for DPCs.
44
57
int networkId = wifiManager .addNetwork (wifiConfiguration );
45
- if (networkId == - 1 ) {
46
- return false ;
58
+ if (networkId == INVALID_NETWORK_ID ) {
59
+ return INVALID_NETWORK_ID ;
47
60
}
48
61
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
49
62
// Saving the configuration is required pre-O.
50
- return saveAddedWifiConfiguration (wifiManager , networkId );
63
+ if (!saveAddedWifiConfiguration (wifiManager , networkId )) {
64
+ return INVALID_NETWORK_ID ;
65
+ }
51
66
}
52
- return true ;
67
+ return networkId ;
53
68
}
54
69
55
70
private static boolean saveAddedWifiConfiguration (WifiManager wifiManager , int networkId ) {
@@ -61,18 +76,24 @@ private static boolean saveAddedWifiConfiguration(WifiManager wifiManager, int n
61
76
return true ;
62
77
}
63
78
64
- private static boolean updateWifiNetwork (
79
+ /**
80
+ * Saves an existing Wifi configuration, returning the configuration's networkId, or
81
+ * {@link #INVALID_NETWORK_ID} if the operation fails.
82
+ */
83
+ private static int updateWifiNetwork (
65
84
WifiManager wifiManager , WifiConfiguration wifiConfiguration ) {
66
85
// WifiManager APIs are marked as deprecated but still explicitly supported for DPCs.
67
86
int networkId = wifiManager .updateNetwork (wifiConfiguration );
68
- if (networkId == - 1 ) {
69
- return false ;
87
+ if (networkId == INVALID_NETWORK_ID ) {
88
+ return INVALID_NETWORK_ID ;
70
89
}
71
90
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
72
91
// Saving the configuration is required pre-O.
73
- return saveUpdatedWifiConfiguration (wifiManager );
92
+ if (!saveUpdatedWifiConfiguration (wifiManager )) {
93
+ return INVALID_NETWORK_ID ;
94
+ }
74
95
}
75
- return true ;
96
+ return networkId ;
76
97
}
77
98
78
99
private static boolean saveUpdatedWifiConfiguration (WifiManager wifiManager ) {
0 commit comments