mdns_plugin 1.1.1
mdns_plugin #
Yet another package which discovers network services on the local network. Presently, this package is in development, and should work for iOS targets. Android targets work with one caveat mentioned below.
Please file any issues or feature requests on github, thanks.
Using the plugin #
You use the plugin by calling startDiscovery
and stopDiscovery
on an instance of MDNSPlugin
, which
you need to create by passing a delegate instance. The MDNSPlugin
class provides two methods and a constructor:
class MDNSPlugin {
/// Constructor
MDNSPlugin(MDNSPluginDelegate)
/// platformVersion returns the underlying platform version of the
/// running plugin
static Future<String> get platformVersion async;
/// startDiscovery is invoked to start discovery of services on
/// the local network, for a serviceType, which should be, for
/// example "_googlecast._tcp" or similar. When the optional
/// enableUpdating flag is set to true, resolved services
/// respond to updates to the TXT record for the service
Future<void> startDiscovery(String serviceType,{ bool enableUpdating = false }) async;
/// stopDiscovery should be invoked to shutdown the discovery
/// of services on your local network
Future<void> stopDiscovery() async;
}
One limitation is that on Android, the enableUpdating
flag is currently ignored. Future work will correct this to bring the platforms in parity. The delegate instance will receive messages from the plugin. Your delegate
class should implement the following methods:
abstract class MDNSPluginDelegate {
/// onDiscoveryStarted is called when discovery on the local
/// network has started
void onDiscoveryStarted();
/// onDiscoveryStopped is called when discovery on the local
/// network has stopped
void onDiscoveryStopped();
/// onServiceFound is called when a service on the local network
/// has been discovered. Your function implementation should
/// return true if you want the service to be resolved
bool onServiceFound(MDNSService service);
/// onServiceResolved is called when resolution has occurred,
/// filling in the details (hostname, addresses, etc) of the
/// service
void onServiceResolved(MDNSService service);
/// onServiceUpdated is called when a found service has updated
/// the TXT record
void onServiceUpdated(MDNSService service);
/// onServiceRemoved is called when a found service has been
/// removed from the local network
void onServiceRemoved(MDNSService service);
}
Example #
Here's a basic example which implements a delegate, that can respond to mDNS services being discovered and updated:
import 'dart:async';
import 'package:mdns_plugin/mdns_plugin.dart';
void main() async {
MDNSPlugin mdns = new MDNSPlugin(Delegate());
await mdns.startDiscovery("_googlecast._tcp",enableUpdating: true);
sleep();
await mdns.stopDiscovery();
sleep();
}
Future sleep() {
return new Future.delayed(const Duration(seconds: 5), () => "5");
}
class Delegate implements MDNSPluginDelegate {
void onDiscoveryStarted() {
print("Discovery started");
}
void onDiscoveryStopped() {
print("Discovery stopped");
}
bool onServiceFound(MDNSService service) {
print("Found: $service");
// Always returns true which begins service resolution
return true;
}
void onServiceResolved(MDNSService service) {
print("Resolved: $service");
}
void onServiceUpdated(MDNSService service) {
print("Updated: $service");
}
void onServiceRemoved(MDNSService service) {
print("Removed: $service");
}
}
For a fuller example please see the application in the example
folder.
Publishing this plugin #
(Some notes for me) In order to publish the plugin, edit
the pubspec.yaml
and CHANGELOG.md
files in the root directory and
the example
directory to bump the version number $TAG
and then run the following commands:
bash% TAG=v`sed -n 's/version\: \(.*\)/\1/p' pubspec.yaml`
bash% echo $TAG && flutter format .
bash% git commit -a -m "Updated version to $TAG"
bash% git push && git tag $TAG && git push --tags
bash% flutter pub pub publish
You should then merge v1
into the master
branch
0.0.1 #
Initial Release
0.0.2 #
Added delegate implementation for iOS and Dart. There is no Android implementation in this version. Remaining items to do:
- Implement
domain
argument when callingstartDiscovery
- Implment the
addresses
property onMDNSService
- Implement the Android version
0.0.4 #
Now allows domain
argument when calling startDiscovery
and
implements the addresses
property on MDNSService
. Remaining items to do:
- Implement the Android version
0.0.5 #
Updated the example code.
0.0.6 #
Formatting changes
0.0.7 #
Started to implement the android code, just enough for the plugin to work correctly, but not yet return any services.
0.0.8 #
Further work on the Android code, but quite a bit to do
0.0.9 #
Further work on the Android code, the remaining issues are:
- Pass through the serviceType onStartDiscovery
- Remove the domain to ensure parity between the iOS and Android implementations
- Extract the addresses from the ServiceInfo
- Implement some sort up update method on Android for when TXT records are updated?
- Android: Hot reloading doesn't work since the discoverylistener is still allocated
0.0.10 #
Further work on the Android code, the remaining issues are:
- Remove the domain to ensure parity between the iOS and Android implementations
- Implement some sort up update method on Android for when TXT records are updated
- On Android, Lazy resolve service info instead of all at once (queue)
1.0.0 #
First v1 release includes the iOS and Android, the remaining issue is that when TXT records are updated, the onServiceUpdated callback is not called on the Android platform, since the in-built libraries don't have this functionality available.
1.1.0 #
The plugin no longer resolves found services automatically but will only do so
after "true" is returned from the delegate onServiceFound
. In addition, the
startDiscovery
method now has an optional argument which is enableUpdating
which when set to true will call the onServiceUpdated
delegate method when
TXT records are updated on the iOS platform. However, this doesn't yet work
on Android.
1.1.1 #
Fixed a problem with the Android manifest for the release version
mdns_plugin_example #
This example application demonstrates how to use the mdns_plugin plugin. It generates a list of services which are presented as a ListView:

See the code in the example/lib
folder:
- The
bloc
folder contains the business logic of the application; - The
models
folder contains the model for the list of services; - The
pages
folder contains the StatelessWidget which renders the page; - The
widgets
folder contains the widget to render a service as a list item;
The list is rebuilt based on delegate messages from the MDNSPlugin
object, which
you can see in the bloc/app.dart
business logic.
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies:
mdns_plugin: ^1.1.1
2. Install it
You can install packages from the command line:
with Flutter:
$ flutter pub get
Alternatively, your editor might support flutter pub get
.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
import 'package:mdns_plugin/mdns_plugin.dart';
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
59
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
80
|
We analyzed this package on Dec 7, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.6.1
- pana: 0.12.21
- Flutter: 1.9.1+hotfix.6
Platforms
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Dependencies
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.1.0 <3.0.0 | ||
flutter | 0.0.0 | ||
Transitive dependencies | |||
collection | 1.14.11 | 1.14.12 | |
meta | 1.1.7 | 1.1.8 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |