@@ -44,12 +44,7 @@ public abstract class AbstractWifiFragment extends Fragment {
44
44
private final Timer mTimer ;
45
45
private WifiManager mWifiManager ;
46
46
private ConnectivityManager mConnectivityManager ;
47
- private static final String [] WIFI_PERMS = new String [] {
48
- Manifest .permission .NEARBY_WIFI_DEVICES ,
49
- Manifest .permission .ACCESS_FINE_LOCATION ,
50
- Manifest .permission .CHANGE_WIFI_STATE ,
51
- Manifest .permission .CHANGE_NETWORK_STATE };
52
-
47
+ private static String [] WIFI_PERMS = null ;
53
48
private static final int REQUEST_PERMISSION_CODE = 1 ;
54
49
55
50
// holds the state of the wifi connection attempt
@@ -85,10 +80,24 @@ public AbstractWifiFragment() {
85
80
public void onCreate (Bundle savedInstanceState ) {
86
81
super .onCreate (savedInstanceState );
87
82
83
+ if (android .os .Build .VERSION .SDK_INT <= 32 ) {
84
+ WIFI_PERMS = new String []{
85
+ Manifest .permission .ACCESS_COARSE_LOCATION ,
86
+ Manifest .permission .ACCESS_FINE_LOCATION ,
87
+ Manifest .permission .CHANGE_WIFI_STATE ,
88
+ Manifest .permission .CHANGE_NETWORK_STATE };
89
+ } else {
90
+ WIFI_PERMS = new String []{
91
+ Manifest .permission .NEARBY_WIFI_DEVICES ,
92
+ Manifest .permission .ACCESS_FINE_LOCATION ,
93
+ Manifest .permission .CHANGE_WIFI_STATE ,
94
+ Manifest .permission .CHANGE_NETWORK_STATE };
95
+ }
96
+
88
97
multiplePermissionsContract = new ActivityResultContracts .RequestMultiplePermissions ();
89
98
multiplePermissionLauncher = registerForActivityResult (multiplePermissionsContract , isGranted -> {
90
99
if (isGranted .containsValue (false )) {
91
- multiplePermissionLauncher . launch ( WIFI_PERMS );
100
+ askPermissions ( multiplePermissionLauncher );
92
101
onMissingWifiPermissions (mTargetSsid );
93
102
} else {
94
103
onWifiPermissionsGranted (mTargetSsid , mTargetBssid );
@@ -116,7 +125,7 @@ public void onDestroyView() {
116
125
super .onDestroyView ();
117
126
118
127
// Unschedule the timeout task, if any
119
- if (mTimeoutTask != null ) {
128
+ if (mTimeoutTask != null ) {
120
129
mTimeoutTask .cancel ();
121
130
mTimeoutTask = null ;
122
131
}
@@ -185,7 +194,6 @@ private boolean hasPermissions(String[] permissions) {
185
194
186
195
private void askPermissions (ActivityResultLauncher <String []> multiplePermissionLauncher ) {
187
196
if (!hasPermissions (WIFI_PERMS )) {
188
- Log .d ("PERMISSIONS" , "Launching multiple contract permission launcher for ALL required permissions" );
189
197
multiplePermissionLauncher .launch (WIFI_PERMS );
190
198
} else {
191
199
Log .d ("PERMISSIONS" , "All permissions are already granted" );
@@ -199,11 +207,12 @@ public void onResume() {
199
207
200
208
/**
201
209
* Starts the wifi connection to the specified ssid
202
- * @param targetSsid Target Wifi SSID to connect to
203
- * @param targetBssid (Optional) Target BSSID
210
+ *
211
+ * @param targetSsid Target Wifi SSID to connect to
212
+ * @param targetBssid (Optional) Target BSSID
204
213
* @param targetPassphrase (Optional) Wifi passphrase to use
205
- * @param reason A reason to show to the user that explains why Wifi is being requested.
206
- * @param timeout Number of milliseconds to wait before issuing a timeout to the wifi operation
214
+ * @param reason A reason to show to the user that explains why Wifi is being requested.
215
+ * @param timeout Number of milliseconds to wait before issuing a timeout to the wifi operation
207
216
* @throws PermissionNotGrantedException In case no enough permissions have been granted
208
217
* to the app by the user. In such case, permissions
209
218
* is automatically requested by this method.
@@ -223,7 +232,7 @@ public void startWifiConnection(String targetSsid,
223
232
if (!AndroidUtils .checkPermissions (requireContext (), WIFI_PERMS )) {
224
233
225
234
// If a reason was specified, show it here.
226
- if (reason != null ) {
235
+ if (reason != null ) {
227
236
AlertDialog .Builder builder = new AlertDialog .Builder (getActivity ());
228
237
builder .setMessage (reason ).setTitle ("Wifi Access Request" );
229
238
AlertDialog dialog = builder .create ();
@@ -232,7 +241,6 @@ public void startWifiConnection(String targetSsid,
232
241
233
242
// If the user did not yet grant Wifi and Location permissions, ask them here and abort the connection.
234
243
askPermissions (multiplePermissionLauncher );
235
- //requestPermissions(WIFI_PERMS, REQUEST_PERMISSION_CODE);
236
244
237
245
// Return false, so the caller know it has to retry the operation after the
238
246
// user's consent to use wifi
@@ -259,26 +267,28 @@ public void startWifiConnection(String targetSsid,
259
267
* Starts the connection attempt against the legacy API.
260
268
* To do so, it registers a broadcast receiver for the network connection events and
261
269
* waits for the connection to attempt to the specified network.
270
+ *
262
271
* @param ssid
263
272
* @param passphrase
264
- * @param timeout Timeout in milliseconds
273
+ * @param timeout Timeout in milliseconds
265
274
*/
266
- @ SuppressLint ("MissingPermission" ) //This piece of code is called only after permissions have bee granted
275
+ @ SuppressLint ("MissingPermission" )
276
+ //This piece of code is called only after permissions have bee granted
267
277
private void startWifiConnectionLegacy (String ssid , @ Nullable String passphrase , int timeout ) {
268
278
269
279
WifiConfiguration targetWifiConf = null ;
270
280
271
281
// Locate the existing network configuration entry.
272
282
for (android .net .wifi .WifiConfiguration conf : mWifiManager .getConfiguredNetworks ()) {
273
283
274
- if (conf .SSID .compareTo ("\" " + ssid + "\" " ) == 0 ) {
284
+ if (conf .SSID .compareTo ("\" " + ssid + "\" " ) == 0 ) {
275
285
// Found a matching configuration: make sure the network passphrase is OK
276
- if (conf .preSharedKey != null && conf .preSharedKey .length ()> 0 && Strings .isEmpty (passphrase ) || Strings .isEmpty (conf .preSharedKey ) && !Strings .isEmpty (passphrase )) {
277
- Log .i (TAG , "Network configuration (" + conf .networkId + ") ignored: passphrase mismatch." );
286
+ if (conf .preSharedKey != null && conf .preSharedKey .length () > 0 && Strings .isEmpty (passphrase ) || Strings .isEmpty (conf .preSharedKey ) && !Strings .isEmpty (passphrase )) {
287
+ Log .i (TAG , "Network configuration (" + conf .networkId + ") ignored: passphrase mismatch." );
278
288
continue ;
279
289
}
280
290
// If we reach this part, it means we found a valid, consistent wifi configuration to be used.
281
- Log .i (TAG , "Network configuration (" + conf .networkId + ") found for ssid " + ssid + "." );
291
+ Log .i (TAG , "Network configuration (" + conf .networkId + ") found for ssid " + ssid + "." );
282
292
targetWifiConf = conf ;
283
293
break ;
284
294
}
@@ -288,10 +298,10 @@ private void startWifiConnectionLegacy(String ssid, @Nullable String passphrase,
288
298
if (targetWifiConf == null || targetWifiConf .networkId == -1 ) {
289
299
Log .i (TAG , "Adding new Wifi configuration for the desired network." );
290
300
targetWifiConf = new android .net .wifi .WifiConfiguration ();
291
- targetWifiConf .SSID = "\" " + ssid + "\" " ;
301
+ targetWifiConf .SSID = "\" " + ssid + "\" " ;
292
302
293
303
if (!Strings .isEmpty (passphrase )) {
294
- targetWifiConf .preSharedKey = "\" " + passphrase + "\" " ;
304
+ targetWifiConf .preSharedKey = "\" " + passphrase + "\" " ;
295
305
targetWifiConf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
296
306
} else {
297
307
targetWifiConf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
@@ -302,7 +312,7 @@ private void startWifiConnectionLegacy(String ssid, @Nullable String passphrase,
302
312
}
303
313
304
314
// Disconnect from current network
305
- Log .i (TAG , "Forcing disconnection/reconnection to the selected wifi " + targetWifiConf );
315
+ Log .i (TAG , "Forcing disconnection/reconnection to the selected wifi " + targetWifiConf );
306
316
mWifiManager .disconnect ();
307
317
308
318
// Start the timeout countdown
@@ -322,7 +332,7 @@ private void startWifiConnectionAndroidQ(String ssid, @Nullable String bssid, @N
322
332
.setSsid (ssid );
323
333
324
334
if (bssid != null ) {
325
- specifierBuilder .setBssid ( MacAddress .fromString (bssid ));
335
+ specifierBuilder .setBssid (MacAddress .fromString (bssid ));
326
336
}
327
337
328
338
if (passphrase != null )
@@ -343,6 +353,7 @@ private void startWifiConnectionAndroidQ(String ssid, @Nullable String bssid, @N
343
353
* Registers a broadcast received that listens to WIFI_STATE changes. It also sets the value
344
354
* of the SSID to configure the broadcast receiver to intercept the desired network
345
355
* connection.
356
+ *
346
357
* @param ssid SSID value that represent the network we are looking for
347
358
*/
348
359
private void registerWifiBroadcastReceiver (String ssid ) {
@@ -392,7 +403,7 @@ private void unRegisterWifiBroadcastReceiver() {
392
403
393
404
private void notifyWifiConnected () {
394
405
mWifiConnectionAttemptInProgress = false ;
395
- if (mTimeoutTask != null ) {
406
+ if (mTimeoutTask != null ) {
396
407
mTimeoutTask .cancel ();
397
408
mTimeoutTask = null ;
398
409
}
@@ -401,7 +412,7 @@ private void notifyWifiConnected() {
401
412
402
413
private void notifyWifiUnavailable () {
403
414
mWifiConnectionAttemptInProgress = false ;
404
- if (mTimeoutTask != null ) {
415
+ if (mTimeoutTask != null ) {
405
416
mTimeoutTask .cancel ();
406
417
mTimeoutTask = null ;
407
418
}
@@ -452,7 +463,7 @@ public void run() {
452
463
@ Override
453
464
public void onReceive (Context context , Intent intent ) {
454
465
String action = intent .getAction ();
455
- if (WifiManager .NETWORK_STATE_CHANGED_ACTION .equals (action )) {
466
+ if (WifiManager .NETWORK_STATE_CHANGED_ACTION .equals (action )) {
456
467
NetworkInfo networkInfo = intent .getParcelableExtra (WifiManager .EXTRA_NETWORK_INFO );
457
468
if (networkInfo == null ) {
458
469
Log .w (TAG , "Network info is null!" );
@@ -463,7 +474,7 @@ public void onReceive(Context context, Intent intent) {
463
474
String connectedSsid = null ;
464
475
String connectedBssid = null ;
465
476
466
- if (connectionInfo != null ) {
477
+ if (connectionInfo != null ) {
467
478
connectedSsid = connectionInfo .getSSID ();
468
479
connectedBssid = connectionInfo .getBSSID ();
469
480
}
@@ -475,9 +486,9 @@ public void onReceive(Context context, Intent intent) {
475
486
Log .i (TAG , "WifiState updated. Connected: " + networkInfo .isConnected () + ", SSID: " + connectedSsid );
476
487
477
488
if (networkInfo .getType () == ConnectivityManager .TYPE_WIFI && networkInfo .isConnected ()) {
478
- Log .i (TAG , "WifiState updated. Current SSID: " + connectedSsid );
489
+ Log .i (TAG , "WifiState updated. Current SSID: " + connectedSsid );
479
490
480
- String quotedSsid = "\" " + mTargetSsid + "\" " ;
491
+ String quotedSsid = "\" " + mTargetSsid + "\" " ;
481
492
if (quotedSsid .compareTo (connectedSsid ) == 0 ) {
482
493
// Ok, we found the network we were looking for.
483
494
notifyWifiConnected ();
0 commit comments