Skip to main content

WifiManager

Struct WifiManager 

Source
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

Source

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.

Source

pub fn with_interface(interface: impl Into<String>) -> WifiResult<Self>

Create a new WifiManager for a specific interface

§Arguments
  • interface - The name of the wireless interface (e.g., “wlan0”, “wlan1”)
§Errors

Returns an error if the interface doesn’t exist.

Source

pub fn interface(&self) -> &str

Get the wireless interface name

Source

pub fn get_mode(&self) -> WifiResult<WifiMode>

Get the current WiFi mode (Client, Hotspot, or Disconnected)

Source

pub fn status(&self) -> WifiResult<ConnectionStatus>

Get detailed status of the current connection

Source

pub fn scan(&self) -> WifiResult<Vec<NetworkInfo>>

Scan for available WiFi networks

Returns a list of networks sorted by signal strength (strongest first).

Source

pub fn connect( &self, ssid: impl AsRef<str>, password: Option<&str>, ) -> WifiResult<()>

Connect to a WiFi network as a client

§Arguments
  • ssid - The network name to connect to
  • password - Optional password for the network
§Example
// Connect to an open network
wifi.connect("OpenNetwork", None)?;

// Connect to a secured network
wifi.connect("SecuredNetwork", Some("mypassword"))?;
Source

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 to
  • password - Optional password for the network
  • timeout - Maximum time to wait for internet connectivity
Source

pub fn disconnect(&self) -> WifiResult<()>

Disconnect from the current network

Source

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 hotspot
  • password - 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"))?;
Source

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)?;
Source

pub fn stop_hotspot(&self) -> WifiResult<()>

Stop the current hotspot

Source

pub fn has_internet(&self) -> WifiResult<bool>

Check if internet is currently available

Performs a quick connectivity check to verify internet access.

Source

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);
}
Source

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 ping
  • timeout - Maximum time to wait for response
Source

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.

Source

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.

Source

pub fn enable_wifi(&self) -> WifiResult<()>

Enable WiFi if it’s currently disabled

Source

pub fn disable_wifi(&self) -> WifiResult<()>

Disable WiFi

Source

pub fn is_wifi_enabled(&self) -> WifiResult<bool>

Check if WiFi is enabled

Source

pub fn forget_network(&self, ssid: &str) -> WifiResult<()>

Delete a saved network connection profile

Trait Implementations§

Source§

impl Debug for WifiManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.