dbus_wifi 0.0.5
dbus_wifi: ^0.0.5 copied to clipboard
A simple library to connect to Wi-Fi networks using D-Bus on Linux. This library is intended for use with the dbus package in Dart and Flutter.
DBus Wi-Fi #
A native implementation for managing Wi-Fi networks using D-Bus on Linux. This library provides a simple interface to scan for networks, connect to them, check connection status, and disconnect.
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
dbus_wifi: ^0.0.5
Then run:
dart pub get
Features #
- Scan for available Wi-Fi networks
- Connect to Wi-Fi networks with password authentication
- Check current connection status
- Disconnect from networks
- View saved networks
- Forget (delete) saved networks
- Command-line interface for interactive usage
Usage #
Basic Example #
import 'package:dbus_wifi/dbus_wifi.dart';
void main() async {
final wifi = DbusWifi();
// Check if Wi-Fi device is available
if (await wifi.hasWifiDevice) {
// Search for Wi-Fi networks
final results = await wifi.search(timeout: Duration(seconds: 7));
print('Found ${results.length} networks');
// Connect to a network
if (results.isNotEmpty) {
try {
await wifi.connect(results.first, 'your_password_here');
print('Connected to ${results.first.ssid}');
} catch (e) {
print('Failed to connect: $e');
}
}
}
// Always close the connection when done
await wifi.close();
}
Checking Connection Status #
final status = await wifi.getConnectionStatus();
if (status['status'] == ConnectionStatus.connected) {
final network = status['network'];
if (network != null) {
print('Connected to: ${network.ssid}');
print('Signal strength: ${network.strength}%');
}
}
Disconnecting from a Network #
final disconnected = await wifi.disconnect();
if (disconnected) {
print('Successfully disconnected');
} else {
print('Failed to disconnect');
}
Viewing Saved Networks #
final savedNetworks = await wifi.getSavedNetworks();
for (final network in savedNetworks) {
print('Network: ${network['id']}, UUID: ${network['uuid']}');
}
Forgetting a Network #
// Forget by UUID
final forgotten = await wifi.forgetNetwork(uuid: 'network-uuid-here');
// Or forget by SSID
final forgotten = await wifi.forgetNetwork(ssid: 'network-name-here');
if (forgotten) {
print('Network has been forgotten');
} else {
print('Failed to forget network');
}
Command-Line Interface #
The package includes a CLI application that can be used to manage Wi-Fi networks. You can run it with:
dart run bin/dbus_wifi.dart
Or install it globally:
dart pub global activate dbus_wifi
dbus-wifi
API Documentation #
DbusWifi #
The main class for interacting with Wi-Fi networks.
Methods
Future<bool> get hasWifiDevice
- Checks if a Wi-Fi device is availableFuture<List<WifiNetwork>> search({Duration timeout})
- Scans for nearby Wi-Fi networksFuture<List<DBusValue>> connect(WifiNetwork network, String password)
- Connects to a Wi-Fi networkFuture<bool> disconnect()
- Disconnects from the current Wi-Fi networkFuture<Map<String, dynamic>> getConnectionStatus()
- Gets the current connection statusFuture<List<Map<String, dynamic>>> getSavedNetworks()
- Gets a list of saved Wi-Fi networksFuture<bool> forgetNetwork({String? uuid, String? ssid})
- Forgets (deletes) a saved Wi-Fi networkFuture<void> close()
- Closes the D-Bus client connection
WifiNetwork #
A class representing a Wi-Fi network.
Properties
String ssid
- The network nameString mac
- The MAC address of the access pointint strength
- The signal strength (0-100)DBusObjectPath path
- The D-Bus object pathString security
- The security type (e.g., 'wpa-psk', 'none')String mode
- The network mode (e.g., 'infrastructure', 'adhoc')
ConnectionStatus #
An enum representing the connection status.
disconnected
- Not connected to any networkconnected
- Connected to a networkconnecting
- In the process of connectingfailed
- Connection failed
More Examples #
For more detailed examples, see the examples directory.
Requirements #
- Linux operating system
- NetworkManager
- D-Bus
License #
This project is licensed under the MIT License - see the LICENSE file for details.