So as the title suggests, I am trying to gather MAC Addresses from connected devices on my local network. Any idea on how I can achieve this would be helpful!
This is what I have made so far to find connected devices MAC Addresses :
Future<String> getMacAddress(String ip) async {
// Use the ping command to check if the device is reachable
final ProcessResult pingResult = await Process.run(
'ping',
['-c', '1', '-w', '1', ip],
);
if (pingResult.exitCode == 0) {
// Use the netcfg command to get the MAC address of the device
final ProcessResult netcfgResult = await Process.run(
'netcfg',
['wlan0'],
);
if (netcfgResult.exitCode == 0) {
final String output = netcfgResult.stdout;
final List<String> lines = output.split('\n');
for (final line in lines) {
final List<String> parts = line.split(' ');
if (parts.length >= 3 && parts[2] == ip) {
return parts[3]; // Return the MAC address as a string
}
}
}
}
return ''; // Return an empty string if the MAC address is not found
}
CodePudding user response:
import 'dart:io';
Future<String> getMacAddress() async {
var interfaces = await NetworkInterface.list();
var macAddress = '';
for (var interface in interfaces) {
// Convert the hardwareAddress list to a string
macAddress = interface.hardwareAddress.join(':');
break;
}
return macAddress;
}
CodePudding user response:
To get the MAC address of devices on the local network in Flutter/Dart, you can use the dart:io library and the NetworkInterface class.
import 'dart:io';
Future<List<String>> getLocalMacAddresses() async {
final interfaces = await NetworkInterface.list();
final macAddresses = <String>[];
for (final interface in interfaces) {
if (interface.name.startsWith('en')) {
macAddresses.add(interface.macAddress);
}
}
return macAddresses;
}