Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mdns ios support #43

Merged
merged 8 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "example",
"cwd": "example",
"request": "launch",
"type": "dart"
}
]
}
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,27 @@ And initialize the pacakge in the main function
await configureNetworkToolsFlutter((await getApplicationDocumentsDirectory()).path);
```

From here please follow the documentation of [network_tools](https://pub.dev/packages/network_tools) as they are the same.
From here please follow the documentation of [network_tools](https://pub.dev/packages/network_tools) as they are the same.


## mDNS search

For mDNS search on android make sure your min Android API level is 21 and add the following permissiongs to the manifest file

```
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
```

And for iOS add permissions to the Info.plist (replace service type with your own):

```
<key>NSLocalNetworkUsageDescription</key>
<string>Required to discover local network devices</string>
<key>NSBonjourServices</key>
<array>
<string>_http._tcp</string>
</array>

```

2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
applicationId "org.fsociety.network_tools_flutter_example.example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
3 changes: 3 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

<application
android:label="example"
android:name="${applicationName}"
Expand Down
28 changes: 13 additions & 15 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:example/pages/mdns_search/mdns_scanner_page.dart';
import 'package:example/pages/pingable_devices.dart';
import 'package:example/pages/port_scanner_page.dart';
import 'package:flutter/material.dart';
Expand All @@ -21,21 +22,6 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
Expand Down Expand Up @@ -80,6 +66,18 @@ class MyHomePage extends StatelessWidget {
},
child: const Text('Port Scanner'),
),
const SizedBox(height: 20, width: double.infinity),
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MdnsScannerPage(),
),
);
},
child: const Text('mDNS Scanner'),
),
],
),
);
Expand Down
58 changes: 58 additions & 0 deletions example/lib/pages/mdns_search/mdns_scanner_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:example/pages/mdns_search/mdns_widget.dart';
import 'package:flutter/material.dart';
import 'package:network_tools_flutter/network_tools_flutter.dart';

class MdnsScannerPage extends StatefulWidget {
const MdnsScannerPage({super.key});

@override
State<MdnsScannerPage> createState() => _MdnsScannerPageState();
}

class _MdnsScannerPageState extends State<MdnsScannerPage> {
List<ActiveHost>? activeHosts;

@override
void initState() {
super.initState();
searchMdns();
}

searchMdns() async {
NetInterface? netInt = await NetInterface.localInterface();
if (netInt == null) {
return;
}
List<ActiveHost> hosts = await MdnsScannerService.instance
.searchMdnsDevices(forceUseOfSavedSrvRecordList: true);

setState(() {
if (activeHosts == null) {
activeHosts = hosts;
} else {
activeHosts!.addAll(hosts);
}
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('mDNS Devices'),
),
body: Center(
child: activeHosts == null
? const CircularProgressIndicator()
: activeHosts!.isEmpty
? const SizedBox()
: ListView.builder(
itemCount: activeHosts!.length,
itemBuilder: (context, index) =>
MdnsSearchWidget(activeHost: activeHosts![index]),
),
),
);
}
}
90 changes: 90 additions & 0 deletions example/lib/pages/mdns_search/mdns_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:flutter/material.dart';
import 'package:network_tools_flutter/network_tools_flutter.dart';

class MdnsSearchWidget extends StatefulWidget {
const MdnsSearchWidget({
super.key,
required this.activeHost,
});

final ActiveHost activeHost;

@override
State<MdnsSearchWidget> createState() => _MdnsSearchWidgetState();
}

class _MdnsSearchWidgetState extends State<MdnsSearchWidget> {
@override
initState() {
super.initState();
initialzeActiveHost();
}

MdnsInfo? mdnsInfo;
bool mdnsInfoFound = false;

String? hostName;
bool hostNameFound = false;

String? deviceName;
bool deviceNameFound = false;

String? macAddress;
bool macAddressFound = false;

initialzeActiveHost() {
widget.activeHost.mdnsInfo.then((value) {
setState(() {
mdnsInfo = value;
mdnsInfoFound = true;
});
});

widget.activeHost.hostName.then((value) {
setState(() {
hostName = value;
hostNameFound = true;
});
});

widget.activeHost.deviceName.then((value) {
setState(() {
deviceName = value;
deviceNameFound = true;
});
});

widget.activeHost.getMacAddress().then((value) {
setState(() {
macAddress = value;
macAddressFound = true;
});
});
}

@override
Widget build(BuildContext context) {
if (!mdnsInfoFound ||
!hostNameFound ||
!deviceNameFound ||
!macAddressFound) {
return const SizedBox();
}

return ListTile(
title: Text(widget.activeHost.weirdHostName),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('mDNS: ${mdnsInfo?.mdnsName}'),
const SizedBox(height: 5),
Text('Device: $deviceName'),
const SizedBox(height: 5),
Text('Mac: $macAddress'),
const SizedBox(height: 5),
Text('Host: $hostName '),
],
),
);
}
}
2 changes: 2 additions & 0 deletions example/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import FlutterMacOS
import Foundation

import nsd_macos
import path_provider_foundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
NsdMacosPlugin.register(with: registry.registrar(forPlugin: "NsdMacosPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
}
Loading
Loading