Skip to content

Commit 174175e

Browse files
committed
Internal Change.
PiperOrigin-RevId: 854258644
1 parent 0703a0e commit 174175e

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

betocq/setup_utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from mobly.controllers import android_device
2828
from mobly.controllers.android_device_lib import adb
2929
from mobly.controllers.android_device_lib import apk_utils
30+
from mobly.snippet import errors
3031

3132
from betocq.gms import hermetic_overrides_partner
3233
from betocq import gms_auto_updates_util
@@ -48,6 +49,8 @@
4849

4950

5051
WIFI_SCAN_WAIT_TIME_SEC = 5
52+
_WIFI_CONNECT_INTERVAL_SEC = 2
53+
_WIFI_CONNECT_RETRY_TIMES = 3
5154

5255
MAX_SSID_THRESHOLD = 10
5356

@@ -267,22 +270,40 @@ def connect_to_wifi_sta_till_success(
267270
wifi_connect_start = datetime.datetime.now()
268271
if not wifi_password:
269272
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+
)
271276
return datetime.datetime.now() - wifi_connect_start
272277

273278

274279
def connect_to_wifi(
275280
ad: android_device.AndroidDevice,
276281
ssid: str,
277282
password: str | None = None,
283+
num_retries: int = 1,
278284
) -> None:
279285
"""Connects to the specified wifi AP and raise exception if failed."""
280286
if not ad.nearby.wifiIsEnabled():
281287
ad.nearby.wifiEnable()
282288
# return until the wifi is connected.
283289
password = password or None
284290
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
286307

287308

288309
def remove_current_connected_wifi_network(

0 commit comments

Comments
 (0)