Flutter/Dart: List devices on local network
8,500
Solution 1
Right now this library only works on Android, but...
https://pub.dartlang.org/packages/mdns
-
Setup mdns for whatever service you're running.
-
Discover the service as such
startMdnsDiscovery(String serviceType){ Mdns mdns = new Mdns(discoveryCallbacks: discoveryCallbacks); mdns.startDiscovery(serviceType); }
UPDATE Mar 26, 2020 - This no longer works with the newest versions of flutter, see below for an updated example.
Updated example: here
Also, what is MDNS.
Solution 2
To discover your local network and list connected computers, printers and other network devices use ping_discover_network package.
Get local ip and discover network devices:
import 'package:wifi/wifi.dart';
import 'package:ping_discover_network/ping_discover_network.dart';
final String ip = await Wifi.ip;
final String subnet = ip.substring(0, ip.lastIndexOf('.'));
final int port = 80;
final stream = NetworkAnalyzer.discover(subnet, port);
stream.listen((NetworkAddress addr) {
if (addr.exists) {
print('Found device: ${addr.ip}');
}
});
Author by
Magnus Wang
Updated on December 06, 2022Comments
-
Magnus Wang over 1 year
I would like to connect to a computer on the local network from a flutter app. Is there any way to get all machines connected to the local network? Like pinging broadcast address?