wake_on_lan 4.0.0 wake_on_lan: ^4.0.0 copied to clipboard
Send wake on LAN magic packets to devices on your local network.
Wake-on-LAN #
Dart library package to easily send Wake-on-LAN magic packets to devices on your local network.
Getting Started #
wake_on_lan
has three core classes for functionality, IPAddress
, MACAddress
, and WakeOnLAN
. All classes are exported in the main file, to import:
import 'package:wake_on_lan/wake_on_lan.dart';
Create an IP Address
IPAddress
has a static function, validate(address, { type })
which allows for easy validation that an IP address string is correctly formatted. An IPv6 address can be used, but type
must be set to InternetAddressType.IPv6
during validation of the address and construction of the class.
Create an IPAddress
instance by using IPAddress(address, { type })
where address
is a string representation of the broadcast address (IPv4) or multicast address (IPv6) of the network and type
is the internet address type (defaults to InternetAddressType.IPv4
). The constructor will call the validation function mentioned above, but will throw the exception on a poorly constructed address string, so it is recommended to validate it first.
String ipv4 = '192.168.1.1';
final validation = IPAddress.validate(ipv4);
if(validation.state) {
IPAddress ip = IPAddress(ipv4);
// Continue execution
} else {
// Handle invalid the address case - access the error that was thrown during validation via `validation.error`.
}
You can also optionally create an IPAddress
instance using the fromHost(host, { type, hostPredicate })
static method which will lookup the host and use the associated IP address. By default the function will select and return the first address returned within the lookup. hostPredicate
can be set to choose which host address is selected and returned.
Create MAC Address
MACAddress
has a static function, validate(String address, { delimiter })
which allows easy validation that a MAC address string is correctly formatted.
Create a MACAddress
instance by using MACAddress(address, { delimiter })
where address
is a string representation of the address. The constructor will call the validation function mentioned above, but will throw the exception on a poorly constructed address string, so it is recommended to validate it first.
String address = 'AA:BB:CC:DD:EE:FF';
final validation = MACAddress.validate(address);
if(validation.state) {
MACAddress mac = MACAddress(address);
//Continue execution
} else {
// Handle invalid address case
}
You can optionally pass in a custom delimiter
when the octets are not separated by colons (:). Ensure you pass the custom delimiter
to both the validate
function and the factory constructor when instantiating a MACAddress
in this scenario.
String delimiter = '#';
String address = 'AA#BB#CC#DD#EE#FF';
if(MACAddress.validate(address, delimiter: delimiter)) {
MACAddress mac = MACAddress(address, delimiter: delimiter);
//Continue execution
} else {
// Handle invalid the address case - access the error that was thrown during validation via `validation.error`.
}
Sending the Wake-on-LAN Packet
Create a WakeOnLAN
instance by using WakeOnLAN(ip, mac, { port })
where ip
is an IPAddress
instance, mac
is a MACAddress
instance, and port
is an optional integer parameter for which port the packet should be sent over (defaulted to the specification standard port, 9).
Once created, call the function wake({ repeat })
on the WakeOnLAN
object to send the packet. You may optionally set the repeat
integer parameter to repeatedly send the Wake on LAN packet (with a 100ms delay between repeats) before closing the socket connection.
String mac = 'AA:BB:CC:DD:EE:FF';
String ipv6 = '2001:0DB8:3333:4444:5555:6666:7777:8888';
final macValidation = MACAddress.validate(mac);
final ipValidation = IPAddress.validate(ipv6, type: InternetAddressType.IPv6);
if(macValidation.state && ipValidation.state) {
MACAddress macAddress = MACAddress(mac);
IPAddress ipAddress = IPAddress(ipv6, type: InternetAddressType.IPv6);
WakeOnLAN wakeOnLan = WakeOnLAN(ipAddress, macAddress);
await wakeOnLan.wake().then(() => print('sent'));
}
Web Support #
Wake on LAN functionality utilizes the User Datagram Protocol (UDP) which is not available in the browser because of security constraints.
Notes #
Because wake-on-LAN packets are sent over UDP, beyond the successful creation of a datagram socket and sending the data over the network, there is no way to confirm that the machine has been awoken beyond pinging the machine after waking it (This functionality is not implemented in this package). This is because of the nature of UDP sockets which do not need to establish the connection for the data to be sent.