Skip to main content

lingxia_platform/traits/
wifi.rs

1use crate::error::PlatformError;
2
3#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4#[serde(rename_all = "camelCase")]
5pub struct WifiInfo {
6    /// SSID of the access point.
7    pub ssid: String,
8    /// BSSID of the access point (MAC address format: "aa:bb:cc:dd:ee:ff").
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub bssid: Option<String>,
11    /// Whether the access point requires authentication.
12    pub secure: bool,
13    /// Signal strength in the range [0, 100], where 100 is strongest.
14    pub signal_strength: u8,
15    /// Center frequency in MHz (if available).
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub frequency: Option<u32>,
18}
19
20#[derive(Debug, Clone)]
21pub struct WifiConnectRequest {
22    /// SSID of the access point.
23    pub ssid: String,
24    /// Password for the access point.
25    pub password: Option<String>,
26    /// Callback ID for success/failure.
27    pub callback_id: u64,
28}
29
30#[derive(Debug, Clone)]
31pub struct WifiGetConnectedRequest {
32    /// Callback ID for success/failure.
33    pub callback_id: u64,
34}
35
36pub trait Wifi: Send + Sync + 'static {
37    /// Initialize Wi-Fi module (wx.startWifi).
38    ///
39    /// # Platform Requirements
40    /// - Android: Requires ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions
41    /// - iOS: Requires NEHotspotConfiguration API (iOS 11+)
42    /// - HarmonyOS: Requires ohos.permission.GET_WIFI_INFO
43    fn start_wifi(&self, callback_id: u64) -> Result<(), PlatformError> {
44        let _ = callback_id;
45        Err(PlatformError::Platform(
46            "start_wifi not implemented".to_string(),
47        ))
48    }
49
50    /// Stop Wi-Fi module (wx.stopWifi).
51    fn stop_wifi(&self, callback_id: u64) -> Result<(), PlatformError> {
52        let _ = callback_id;
53        Err(PlatformError::Platform(
54            "stop_wifi not implemented".to_string(),
55        ))
56    }
57
58    /// Connect to a Wi-Fi access point (wx.connectWifi).
59    ///
60    /// # Platform Limitations
61    /// - iOS: Only works for pre-configured networks or hotspot networks
62    /// - Android: May require location permissions on Android 6.0+
63    fn connect_wifi(&self, request: WifiConnectRequest) -> Result<(), PlatformError> {
64        let _ = request;
65        Err(PlatformError::Platform(
66            "connect_wifi not implemented".to_string(),
67        ))
68    }
69
70    /// Request a Wi-Fi scan and return results via callback (wx.getWifiList).
71    ///
72    /// Unlike WeChat's event-driven approach, this directly returns scan results
73    /// via the callback for Stage 1 simplicity.
74    ///
75    /// Callback receives: JSON array of WifiInfo objects
76    /// Example: [{"ssid":"MyWiFi","bssid":"aa:bb:cc:dd:ee:ff","secure":true,"signalStrength":80,"frequency":2412}]
77    fn get_wifi_list(&self, callback_id: u64) -> Result<(), PlatformError> {
78        let _ = callback_id;
79        Err(PlatformError::Platform(
80            "get_wifi_list not implemented".to_string(),
81        ))
82    }
83
84    /// Get current connected Wi-Fi (wx.getConnectedWifi).
85    ///
86    /// Always returns full WiFi information (SSID, BSSID, secure, signalStrength).
87    fn get_connected_wifi(&self, request: WifiGetConnectedRequest) -> Result<(), PlatformError> {
88        let _ = request;
89        Err(PlatformError::Platform(
90            "get_connected_wifi not implemented".to_string(),
91        ))
92    }
93
94    /// Check if WiFi is currently enabled on the device (synchronous).
95    ///
96    /// Returns true if WiFi hardware is active and available for use.
97    ///
98    /// # Platform Notes
99    /// - Android: Checks WifiManager.isWifiEnabled()
100    /// - iOS: Always returns true (WiFi state not accessible)
101    /// - HarmonyOS: Checks OH_Wifi_IsWifiEnabled()
102    fn is_wifi_enabled(&self) -> Result<bool, PlatformError> {
103        Err(PlatformError::Platform(
104            "is_wifi_enabled not implemented".to_string(),
105        ))
106    }
107
108    /// Add a listener for WiFi connection state changes.
109    ///
110    /// Multiple listeners can be registered (supports multiple LxApp instances).
111    /// Platform should invoke the callback with connected WiFi info JSON payload
112    /// each time WiFi connection state changes.
113    ///
114    /// # Multi-LxApp Support
115    /// - Platform maintains a set of callback_ids
116    /// - First listener triggers system WiFi state monitoring
117    /// - Each new listener immediately receives current WiFi state
118    fn add_wifi_state_listener(&self, callback_id: u64) -> Result<(), PlatformError> {
119        let _ = callback_id;
120        Ok(())
121    }
122
123    /// Remove a previously registered WiFi state listener.
124    ///
125    /// # Multi-LxApp Support
126    /// - Removes the specific callback_id from the listener set
127    /// - Last listener stops system WiFi state monitoring
128    fn remove_wifi_state_listener(&self, callback_id: u64) -> Result<(), PlatformError> {
129        let _ = callback_id;
130        Ok(())
131    }
132}