|
27 | 27 | from mobly.controllers import android_device |
28 | 28 | from mobly.controllers.android_device_lib import adb |
29 | 29 | from mobly.controllers.android_device_lib import apk_utils |
| 30 | +from mobly.snippet import errors |
30 | 31 |
|
31 | 32 | from betocq.gms import hermetic_overrides_partner |
32 | 33 | from betocq import gms_auto_updates_util |
|
48 | 49 |
|
49 | 50 |
|
50 | 51 | WIFI_SCAN_WAIT_TIME_SEC = 5 |
| 52 | +_WIFI_CONNECT_INTERVAL_SEC = 2 |
| 53 | +_WIFI_CONNECT_RETRY_TIMES = 3 |
51 | 54 |
|
52 | 55 | MAX_SSID_THRESHOLD = 10 |
53 | 56 |
|
@@ -267,22 +270,40 @@ def connect_to_wifi_sta_till_success( |
267 | 270 | wifi_connect_start = datetime.datetime.now() |
268 | 271 | if not wifi_password: |
269 | 272 | wifi_password = None |
270 | | - connect_to_wifi(ad, wifi_ssid, wifi_password) |
| 273 | + connect_to_wifi( |
| 274 | + ad, wifi_ssid, wifi_password, num_retries=_WIFI_CONNECT_RETRY_TIMES |
| 275 | + ) |
271 | 276 | return datetime.datetime.now() - wifi_connect_start |
272 | 277 |
|
273 | 278 |
|
274 | 279 | def connect_to_wifi( |
275 | 280 | ad: android_device.AndroidDevice, |
276 | 281 | ssid: str, |
277 | 282 | password: str | None = None, |
| 283 | + num_retries: int = 1, |
278 | 284 | ) -> None: |
279 | 285 | """Connects to the specified wifi AP and raise exception if failed.""" |
280 | 286 | if not ad.nearby.wifiIsEnabled(): |
281 | 287 | ad.nearby.wifiEnable() |
282 | 288 | # return until the wifi is connected. |
283 | 289 | password = password or None |
284 | 290 | ad.log.info('Connect to wifi: ssid: %s, password: %s', ssid, password) |
285 | | - ad.nearby.wifiConnectSimple(ssid, password) |
| 291 | + for i in range(num_retries): |
| 292 | + try: |
| 293 | + ad.nearby.wifiConnectSimple(ssid, password) |
| 294 | + return |
| 295 | + except errors.ApiError: |
| 296 | + ad.log.warning( |
| 297 | + f'Failed to connect to wifi {ssid}, retry attempt {i + 1}' |
| 298 | + ) |
| 299 | + if i < num_retries - 1: |
| 300 | + time.sleep(_WIFI_CONNECT_INTERVAL_SEC) |
| 301 | + else: |
| 302 | + ad.log.error( |
| 303 | + f'Still failed to connect to wifi {ssid} after' |
| 304 | + f' {num_retries} attempts.', exc_info=True |
| 305 | + ) |
| 306 | + raise |
286 | 307 |
|
287 | 308 |
|
288 | 309 | def remove_current_connected_wifi_network( |
|
0 commit comments