Expand description
A Rust library for managing Wi-Fi connections via NetworkManager.
This crate provides a high-level async API for common Wi-Fi operations:
- Listing network devices and visible networks
- Connecting to open, WPA-PSK, and WPA-EAP networks
- Managing saved connection profiles
- Enabling/disabling Wi-Fi
§Example
use nmrs::{NetworkManager, WifiSecurity};
let nm = NetworkManager::new().await?;
// List visible networks
let networks = nm.list_networks().await?;
for net in &networks {
println!("{} ({}%)", net.ssid, net.strength.unwrap_or(0));
}
// Connect to a network
nm.connect("MyNetwork", WifiSecurity::WpaPsk {
psk: "password123".into()
}).await?;§Error Handling
All operations return Result<T, ConnectionError>. The error type provides
specific variants for common failures like authentication errors, timeouts,
and missing devices.
§Signal-Based State Monitoring
This crate uses D-Bus signals for efficient state monitoring instead of polling.
When connecting to a network, the library subscribes to NetworkManager’s
StateChanged signals to detect connection success or failure immediately,
rather than polling device state in a loop. This provides:
- Faster response times (immediate notification vs polling delay)
- Lower CPU usage (no spinning loops)
- Better error messages with specific failure reasons
§Logging
This crate uses the log facade for logging. To see
log output, add a logging implementation like env_logger. For example:
ⓘ
env_logger::init();
// ...Re-exports§
pub use models::ActiveConnectionState;pub use models::ConnectionError;pub use models::ConnectionOptions;pub use models::ConnectionStateReason;pub use models::Device;pub use models::DeviceState;pub use models::DeviceType;pub use models::EapMethod;pub use models::EapOptions;pub use models::Network;pub use models::NetworkInfo;pub use models::Phase2;pub use models::StateReason;pub use models::WifiSecurity;pub use models::connection_state_reason_to_error;pub use models::reason_to_error;pub use network_manager::NetworkManager;
Modules§
- models
- network_
manager - wifi_
builders - NetworkManager connection settings builder.
Macros§
- try_log
- Macro to convert Result to Option with error logging.
Usage:
try_log!(result, "context message")?
Type Aliases§
- Result
- A specialized
Resulttype for network operations.