Skip to content

Commit 28cfda9

Browse files
Merge pull request #12 from bitcoin3us/docs/wifi-busy-none-returns
WifiService: document None return values while WiFi is busy
2 parents 6b8d018 + 61bf2ef commit 28cfda9

1 file changed

Lines changed: 58 additions & 10 deletions

File tree

‎docs/frameworks/wifi-service.md‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ else:
3636
from mpos import WifiService
3737

3838
networks = WifiService.scan_networks()
39-
for ssid in networks:
40-
print(f"Found network: {ssid}")
39+
if networks is None:
40+
print("WiFi is busy, try again later")
41+
else:
42+
for ssid in networks:
43+
print(f"Found network: {ssid}")
4144
```
4245

46+
`scan_networks()` returns `None`, not an empty list, while another WiFi operation is running (see [Busy Flag](#busy-flag)).
47+
4348
### Connecting to a Network
4449

4550
```python
@@ -48,12 +53,16 @@ from mpos import WifiService
4853
WifiService.save_network("MyNetwork", "password123")
4954

5055
success = WifiService.attempt_connecting("MyNetwork", "password123")
51-
if success:
56+
if success is None:
57+
print("WiFi is busy, try again later")
58+
elif success:
5259
print("Connected!")
5360
else:
5461
print("Connection failed")
5562
```
5663

64+
`attempt_connecting()` returns `None` without trying when another WiFi operation is running. `None` is falsy, so code that only checks `if success:` treats it as a failed attempt.
65+
5766
### Managing Saved Networks
5867

5968
```python
@@ -135,12 +144,12 @@ class WifiSettingsActivity(Activity):
135144
self.status.set_text("Not connected")
136145

137146
def on_scan(self, event):
138-
if WifiService.is_busy():
139-
print("WiFi is busy")
147+
networks = WifiService.scan_networks()
148+
if networks is None:
149+
print("WiFi is busy") # keep the current list
140150
return
141151

142152
self.network_list.clean()
143-
networks = WifiService.scan_networks()
144153
saved = WifiService.get_saved_networks()
145154

146155
for ssid in networks:
@@ -204,6 +213,24 @@ print(WifiService.get_ipv4_netmask())
204213
print(WifiService.get_ipv4_gateway())
205214
```
206215

216+
## Busy Flag
217+
218+
WifiService runs one radio operation at a time. `auto_connect()`, `attempt_connecting()`, `scan_networks()` and `temporarily_disable()` each hold the class-level `wifi_busy` flag while they run. While another operation holds it:
219+
220+
| Method | While busy |
221+
|--------|------------|
222+
| `attempt_connecting()` | Returns `None` without trying |
223+
| `scan_networks()` | Returns `None` (a scan that finds nothing returns `[]`) |
224+
| `auto_connect()` | Returns without connecting |
225+
| `temporarily_disable()` | Raises `RuntimeError` |
226+
| `enable_hotspot()` | Returns `False` |
227+
| `is_connected()` | Returns `False` |
228+
| `get_ipv4_address()`, `get_ipv4_netmask()`, `get_ipv4_gateway()` | Return `None` |
229+
230+
ConnectivityManager's periodic reconnect is skipped while the flag is held, so it can't interrupt a scan or a connect started by an app.
231+
232+
`is_busy()` tells you whether an operation is running, but the flag can change between that check and your call. Check the `None` return values instead of relying on `is_busy()` alone.
233+
207234
## API Reference
208235

209236
### Connection Functions
@@ -223,7 +250,7 @@ Scan for available networks and connect to the first saved network found.
223250

224251
#### `WifiService.attempt_connecting(ssid, password, network_module=None, time_module=None)`
225252

226-
Attempt to connect to a specific WiFi network.
253+
Attempt to connect to a specific WiFi network. Holds the [busy flag](#busy-flag) while it runs.
227254

228255
**Parameters:**
229256
- `ssid` (str) - Network SSID to connect to
@@ -233,6 +260,7 @@ Attempt to connect to a specific WiFi network.
233260

234261
**Returns:**
235262
- `bool` - `True` if successfully connected, `False` otherwise
263+
- `None` - another WiFi operation is running; nothing was attempted
236264

237265
---
238266

@@ -256,6 +284,26 @@ Disconnect from current WiFi network and disable WiFi (also disables hotspot).
256284

257285
Check if WiFi is currently connected. Returns `True` for hotspot mode when AP is active.
258286

287+
### Scanning Functions
288+
289+
#### `WifiService.scan_networks(network_module=None, time_module=None)`
290+
291+
Scan for available WiFi networks. Holds the [busy flag](#busy-flag) while it runs.
292+
293+
**Parameters:**
294+
- `network_module` - Network module for dependency injection (testing)
295+
- `time_module` - Time module for dependency injection (testing)
296+
297+
**Returns:**
298+
- `list` - SSIDs found (empty if none were found)
299+
- `None` - another WiFi operation is running; nothing was scanned
300+
301+
---
302+
303+
#### `WifiService.is_busy()`
304+
305+
Return `True` while a WiFi operation holds the [busy flag](#busy-flag).
306+
259307
### Hotspot Functions
260308

261309
#### `WifiService.enable_hotspot(network_module=None)`
@@ -300,9 +348,9 @@ On desktop (Linux/macOS), WifiService provides simulated behavior for testing:
300348

301349
| Method | Desktop Behavior |
302350
|--------|------------------|
303-
| `is_connected()` | Always returns `True` |
304-
| `scan_networks()` | Returns mock SSIDs |
305-
| `attempt_connecting()` | Simulates 2-second connection delay, always succeeds |
351+
| `is_connected()` | Returns `True` unless WiFi is busy |
352+
| `scan_networks()` | Returns mock SSIDs (ignores the busy flag) |
353+
| `attempt_connecting()` | Simulates 2-second connection delay, succeeds unless WiFi is busy |
306354
| `get_current_ssid()` | Returns simulated connected SSID |
307355
| `disconnect()` | Prints message, no-op |
308356
| `enable_hotspot()` | Simulated hotspot state |

0 commit comments

Comments
 (0)