@@ -36,7 +36,7 @@ class WifiService:
3636 """
3737
3838 # Class-level lock to prevent concurrent WiFi operations
39- # Used by WiFi app when scanning to avoid conflicts with connection attempts
39+ # Use is_busy() to check state; operations like scan_networks() manage this automatically
4040 wifi_busy = False
4141
4242 # Dictionary of saved access points {ssid: {password: "..."}}
@@ -312,6 +312,19 @@ def disconnect(network_module=None):
312312 #print(f"WifiService: Error disconnecting: {e}") # probably "Wifi Not Started" so harmless
313313 pass
314314
315+ @staticmethod
316+ def is_busy ():
317+ """
318+ Check if WiFi operations are currently in progress.
319+
320+ Use this to check if scanning or other WiFi operations can be started.
321+ Operations like scan_networks() manage the busy flag automatically.
322+
323+ Returns:
324+ bool: True if WiFi is busy, False if available
325+ """
326+ return WifiService .wifi_busy
327+
315328 @staticmethod
316329 def get_saved_networks ():
317330 """
@@ -356,22 +369,35 @@ def _scan_networks_raw(network_module=None):
356369 def scan_networks (network_module = None ):
357370 """
358371 Scan for available WiFi networks.
372+
373+ This method manages the wifi_busy flag internally. If WiFi is already busy,
374+ returns an empty list. The busy flag is automatically cleared when scanning
375+ completes (even on error).
359376
360377 Args:
361378 network_module: Network module for dependency injection (testing)
362379
363380 Returns:
364- list: List of SSIDs found, or mock data on desktop
381+ list: List of SSIDs found, empty list if busy, or mock data on desktop
365382 """
383+ # Desktop mode - return mock SSIDs (no busy flag needed)
366384 if not HAS_NETWORK_MODULE and network_module is None :
367- # Desktop mode - return mock SSIDs
368385 time .sleep (1 )
369386 return ["Home WiFi" , "Pretty Fly for a Wi Fi" , "Winternet is coming" , "The Promised LAN" ]
370387
371- networks = WifiService ._scan_networks_raw (network_module )
372- # Return unique SSIDs, filtering out empty ones and invalid lengths
373- ssids = list (set (n [0 ].decode () for n in networks if n [0 ]))
374- return [s for s in ssids if 0 < len (s ) <= 32 ]
388+ # Check if already busy
389+ if WifiService .wifi_busy :
390+ print ("WifiService: scan_networks() - WiFi is busy, returning empty list" )
391+ return []
392+
393+ WifiService .wifi_busy = True
394+ try :
395+ networks = WifiService ._scan_networks_raw (network_module )
396+ # Return unique SSIDs, filtering out empty ones and invalid lengths
397+ ssids = list (set (n [0 ].decode () for n in networks if n [0 ]))
398+ return [s for s in ssids if 0 < len (s ) <= 32 ]
399+ finally :
400+ WifiService .wifi_busy = False
375401
376402 @staticmethod
377403 def get_current_ssid (network_module = None ):
0 commit comments