pub struct WifiManager { /* private fields */ }Expand description
High-level WiFi manager for Raspberry Pi
Provides an easy-to-use API for switching between client and hotspot modes, scanning networks, and checking internet connectivity.
§Example
use rpi_host::WifiManager;
let wifi = WifiManager::new()?;
// Connect to a network
wifi.connect("MyNetwork", Some("password123"))?;
// Check internet connectivity
if wifi.has_internet()? {
println!("Connected to the internet!");
}
// Start a hotspot
wifi.start_hotspot("MyHotspot", Some("hotspotpass"))?;Implementations§
Source§impl WifiManager
impl WifiManager
Sourcepub fn new() -> WifiResult<Self>
pub fn new() -> WifiResult<Self>
Create a new WifiManager using the default interface (wlan0)
§Errors
Returns an error if the interface doesn’t exist or WiFi is not available.
Sourcepub fn with_interface(interface: impl Into<String>) -> WifiResult<Self>
pub fn with_interface(interface: impl Into<String>) -> WifiResult<Self>
Sourcepub fn get_mode(&self) -> WifiResult<WifiMode>
pub fn get_mode(&self) -> WifiResult<WifiMode>
Get the current WiFi mode (Client, Hotspot, or Disconnected)
Sourcepub fn status(&self) -> WifiResult<ConnectionStatus>
pub fn status(&self) -> WifiResult<ConnectionStatus>
Get detailed status of the current connection
Sourcepub fn scan(&self) -> WifiResult<Vec<NetworkInfo>>
pub fn scan(&self) -> WifiResult<Vec<NetworkInfo>>
Scan for available WiFi networks
Returns a list of networks sorted by signal strength (strongest first).
Sourcepub fn connect_with_internet(
&self,
ssid: impl AsRef<str>,
password: Option<&str>,
timeout: Duration,
) -> WifiResult<()>
pub fn connect_with_internet( &self, ssid: impl AsRef<str>, password: Option<&str>, timeout: Duration, ) -> WifiResult<()>
Connect to a WiFi network and wait for internet connectivity
Similar to connect(), but also waits for and verifies internet connectivity.
§Arguments
ssid- The network name to connect topassword- Optional password for the networktimeout- Maximum time to wait for internet connectivity
Sourcepub fn disconnect(&self) -> WifiResult<()>
pub fn disconnect(&self) -> WifiResult<()>
Disconnect from the current network
Sourcepub fn start_hotspot(
&self,
ssid: impl Into<String>,
password: Option<&str>,
) -> WifiResult<()>
pub fn start_hotspot( &self, ssid: impl Into<String>, password: Option<&str>, ) -> WifiResult<()>
Start a WiFi hotspot with default settings
§Arguments
ssid- The name for the hotspotpassword- Optional password (must be at least 8 characters if provided)
§Example
// Create an open hotspot
wifi.start_hotspot("MyPiHotspot", None)?;
// Create a secured hotspot
wifi.start_hotspot("MyPiHotspot", Some("password123"))?;Sourcepub fn start_hotspot_with_config(&self, config: HotspotConfig) -> WifiResult<()>
pub fn start_hotspot_with_config(&self, config: HotspotConfig) -> WifiResult<()>
Start a WiFi hotspot with custom configuration
§Example
let config = HotspotConfig::new("MyHotspot")
.with_password("securepass")
.with_band(HotspotBand::A) // Use 5GHz
.with_channel(36);
wifi.start_hotspot_with_config(config)?;Sourcepub fn stop_hotspot(&self) -> WifiResult<()>
pub fn stop_hotspot(&self) -> WifiResult<()>
Stop the current hotspot
Sourcepub fn has_internet(&self) -> WifiResult<bool>
pub fn has_internet(&self) -> WifiResult<bool>
Check if internet is currently available
Performs a quick connectivity check to verify internet access.
Sourcepub fn check_connectivity(&self) -> WifiResult<ConnectivityResult>
pub fn check_connectivity(&self) -> WifiResult<ConnectivityResult>
Check internet connectivity with detailed results
§Example
let result = wifi.check_connectivity()?;
if result.is_connected {
println!("Connected! Latency: {:?}ms", result.latency_ms);
}Sourcepub fn check_connectivity_to(
&self,
target: &str,
timeout: Duration,
) -> WifiResult<ConnectivityResult>
pub fn check_connectivity_to( &self, target: &str, timeout: Duration, ) -> WifiResult<ConnectivityResult>
Check connectivity to a specific target
§Arguments
target- IP address or hostname to pingtimeout- Maximum time to wait for response
Sourcepub fn check_connectivity_multi(&self, targets: &[&str]) -> WifiResult<bool>
pub fn check_connectivity_multi(&self, targets: &[&str]) -> WifiResult<bool>
Check connectivity to multiple targets and return true if any succeed
This is more reliable than checking a single target.
Sourcepub fn wait_for_internet(&self, timeout: Duration) -> WifiResult<()>
pub fn wait_for_internet(&self, timeout: Duration) -> WifiResult<()>
Wait for internet connectivity with timeout
Useful after connecting to a network to wait for DHCP and routing to complete.
Sourcepub fn enable_wifi(&self) -> WifiResult<()>
pub fn enable_wifi(&self) -> WifiResult<()>
Enable WiFi if it’s currently disabled
Sourcepub fn disable_wifi(&self) -> WifiResult<()>
pub fn disable_wifi(&self) -> WifiResult<()>
Disable WiFi
Sourcepub fn is_wifi_enabled(&self) -> WifiResult<bool>
pub fn is_wifi_enabled(&self) -> WifiResult<bool>
Check if WiFi is enabled
Sourcepub fn forget_network(&self, ssid: &str) -> WifiResult<()>
pub fn forget_network(&self, ssid: &str) -> WifiResult<()>
Delete a saved network connection profile