Skip to content

Navideck/wifi_desktop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WiFi Desktop for Dart

A cross-platform WiFi connectivity library for Dart that provides WiFi connection management, hotspot creation, and network interface control. This library is a port of the popular Rust wifi-rs library to Dart.

Features

  • Cross-platform support: macOS, Linux, and Windows
  • WiFi connection management: Connect to and disconnect from WiFi networks
  • Network scanning: Discover available WiFi networks
  • Interface control: Turn WiFi on/off and get status information
  • Platform-specific implementations: Optimized for each operating system

Installation

Add this to your pubspec.yaml:

dependencies:
  wifi_desktop: ^0.2.4

Then run:

dart pub get

Quick Start

import 'package:wifi_desktop/wifi_desktop.dart';

void main() async {
  // Create a WiFi instance
  final wifi = WiFi();

  // Check if WiFi is enabled
  final isEnabled = await wifi.isWifiEnabled();
  print('WiFi Enabled: $isEnabled');

  // Connect to a network
  final connected = await wifi.connect('MyNetwork', 'MyPassword');
  if (connected) {
    print('Successfully connected!');
  }

  // Get available networks
  final networks = await wifi.getAvailableNetworks();
  print('Found ${networks.length} networks');
}

Basic Usage

Creating a WiFi Instance

// Use default configuration
final wifi = WiFi();

// Or specify a custom interface
final config = WifiConfig(interface: 'wlan0');
final wifi = WiFi(config);

Checking WiFi Status

// Check if WiFi is enabled
final isEnabled = await wifi.isWifiEnabled();

// Get current WiFi status
final status = await wifi.getWifiStatus();

// Get current connection info
final connection = await wifi.getCurrentConnection();

Connecting to Networks

// Connect to a WiFi network
final connected = await wifi.connect('NetworkSSID', 'NetworkPassword');

if (connected) {
  print('Connected successfully!');
  print('Current connection: ${wifi.connection?.ssid}');
} else {
  print('Failed to connect');
}

Disconnecting

// Disconnect from current network
final disconnected = await wifi.disconnect();

if (disconnected) {
  print('Disconnected successfully');
} else {
  print('Failed to disconnect');
}

Scanning for Networks

// Get all available networks
final networks = await WiFi.getAvailableNetworks();

for (final network in networks) {
  print('SSID: ${network['ssid']}');
  print('Security: ${network['security']}');
  print('Signal Strength: ${network['signalStrength']} dBm');
  print('Frequency: ${network['frequency']} MHz');
  print('Channel: ${network['channel']}');
  print('---');
}

WiFi Interface Control

// Turn WiFi on/off
await wifi.turnOn();
await wifi.turnOff();

// Check if WiFi is enabled
final enabled = await wifi.isWifiEnabled();

Advanced Usage

Custom WiFi Manager

class WifiManager {
  final WiFi _wifi;

  WifiManager([WifiConfig? config]) : _wifi = WiFi(config);

  Future<bool> connectToNetwork(String ssid, String password) async {
    try {
      return await _wifi.connect(ssid, password);
    } catch (e) {
      print('Connection error: $e');
      return false;
    }
  }

  Future<List<Map<String, dynamic>>> scanNetworks() async {
    try {
      return await WiFi.getAvailableNetworks();
    } catch (e) {
      print('Scan error: $e');
      return [];
    }
  }


}

// Usage
final manager = WifiManager();
await manager.connectToNetwork('MyNetwork', 'MyPassword');

Error Handling

try {
  final connected = await wifi.connect('Network', 'Password');
  if (connected) {
    print('Connected successfully!');
  }
} on WifiDisabledException {
  print('WiFi is disabled. Please enable WiFi first.');
} on FailedToConnectException catch (e) {
  print('Failed to connect: ${e.message}');
} on WifiException catch (e) {
  print('WiFi error: ${e.message}');
} catch (e) {
  print('Unexpected error: $e');
}

Platform Support

macOS

  • Uses networksetup command-line tool
  • Default interface: en0
  • Supports WiFi connection, disconnection, and hotspot creation

Linux

  • Uses iwconfig, iwlist, and wpa_supplicant
  • Default interface: wlan0
  • Supports WiFi connection and disconnection

Windows

  • Uses Windows Native WiFi API
  • Default interface: Wi-Fi
  • Supports WiFi connection and disconnection

Data Models

WifiConfig

class WifiConfig {
  final String? interface;

  const WifiConfig({this.interface});
}

WifiConnection

class WifiConnection {
  final String ssid;
  final int? signalStrength;
  final String? security;
  final int? frequency;
  final int? channel;
}

WifiNetwork

class WifiNetwork {
  final String ssid;
  final int signalStrength;
  final String security;
  final int frequency;
  final int channel;
  final bool isConnected;
}

Error Types

The library provides specific exception types for different error scenarios:

  • WifiException - Base exception class
  • WifiDisabledException - WiFi interface is disabled
  • WifiInterfaceFailedToOnException - Failed to turn on WiFi
  • WifiIoException - IO error occurred
  • AddNetworkProfileFailedException - Failed to add network profile
  • FailedToConnectException - Failed to connect to network
  • FailedToDisconnectException - Failed to disconnect from network

Dependencies

  • flutter - Flutter framework
  • ffi - Foreign Function Interface for native code integration
  • path - Path manipulation utilities
  • process_run - Process execution utilities

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This library is a port of the excellent wifi-rs Rust library by Tochukwu Nkemdilim.

Example Projects

Check out the example/ directory for complete working examples of how to use the library.

Support

If you encounter any issues or have questions, please:

  1. Check the existing issues on GitHub
  2. Create a new issue with a detailed description of the problem
  3. Include your platform (macOS/Linux/Windows) and Dart/Flutter version

About

A cross-platform WiFi connectivity library for Dart that provides WiFi connection management.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages