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

  1. Setup mdns for whatever service you're running.

  2. 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}');
  }
});
Share:
8,500
Magnus Wang
Author by

Magnus Wang

Updated on December 06, 2022

Comments

  • Magnus Wang
    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?