Skip to content

Commit c7924e7

Browse files
Add wifi QR decoding
1 parent 68d0ac5 commit c7924e7

File tree

1 file changed

+51
-3
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.wifi/assets

1 file changed

+51
-3
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,64 @@ def gotqr_result_callback(self, result):
346346
if result.get("result_code"):
347347
data = result.get("data")
348348
print(f"Setting textarea data: {data}")
349-
authentication_type, ssid, password, hidden = decode_wifi_qr_code(data)
349+
authentication_type, ssid, password, hidden = self.decode_wifi_qr_code(data)
350350
if ssid and self.ssid_ta: # not always present
351351
self.ssid_ta.set_text(ssid)
352352
if password:
353-
self.password_ta.set_text(ssid)
353+
self.password_ta.set_text(password)
354354
if hidden is True:
355355
self.hidden_cb.set_state(lv.STATE.CHECKED, True)
356356
elif hidden is False:
357357
self.hidden_cb.remove_state(lv.STATE.CHECKED)
358358

359359
@staticmethod
360360
def decode_wifi_qr_code(to_decode):
361-
print(f"decoding {todecode}")
361+
"""
362+
Decode a WiFi QR code string in the format:
363+
WIFI:T:WPA;S:SSID;P:PASSWORD;H:hidden;
364+
365+
Returns: (authentication_type, ssid, password, hidden)
366+
"""
367+
print(f"decoding {to_decode}")
368+
369+
# Initialize return values
370+
authentication_type = "WPA"
371+
ssid = None
372+
password = None
373+
hidden = False
374+
375+
try:
376+
# Remove the "WIFI:" prefix if present
377+
if to_decode.startswith("WIFI:"):
378+
to_decode = to_decode[5:]
379+
380+
# Split by semicolon to get key-value pairs
381+
pairs = to_decode.split(";")
382+
383+
for pair in pairs:
384+
if not pair: # Skip empty strings
385+
continue
386+
387+
# Split by colon to get key and value
388+
if ":" not in pair:
389+
continue
390+
391+
key, value = pair.split(":", 1)
392+
393+
if key == "T":
394+
# Authentication type (WPA, WEP, nopass, etc.)
395+
authentication_type = value
396+
elif key == "S":
397+
# SSID (network name)
398+
ssid = value
399+
elif key == "P":
400+
# Password
401+
password = value
402+
elif key == "H":
403+
# Hidden network (true/false)
404+
hidden = value.lower() in ("true", "1", "yes")
405+
406+
except Exception as e:
407+
print(f"Error decoding WiFi QR code: {e}")
408+
409+
return authentication_type, ssid, password, hidden

0 commit comments

Comments
 (0)