From 81f588fdc84c6e0c0b6708b359c1af4d896f5571 Mon Sep 17 00:00:00 2001 From: Johannes Stahl Date: Wed, 31 Jan 2024 11:19:57 -0800 Subject: [PATCH] fix issue #22 for Windows and Android --- android/build.gradle | 15 ++++++++++----- .../nimroddayan/flutternsd/FlutterNsdPlugin.kt | 9 +++++++++ lib/flutter_nsd.dart | 10 ++++++++-- windows/flutter_nsd_plugin.cpp | 4 ++++ windows/mdns_impl.c | 4 ++-- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 13b1713..06d38cf 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -19,14 +19,14 @@ group 'com.nimroddayan.flutternsd' version '1.0-SNAPSHOT' buildscript { - ext.kotlin_version = '1.4.0' + ext.kotlin_version = '1.7.10' repositories { google() - jcenter() + mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:7.3.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -34,7 +34,7 @@ buildscript { rootProject.allprojects { repositories { google() - jcenter() + mavenCentral() } } @@ -42,7 +42,12 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 29 + // Conditional for compatibility with AGP <4.2. + if (project.android.hasProperty("namespace")) { + namespace 'com.nimroddayan.flutternsd' + } + + compileSdkVersion 34 sourceSets { main.java.srcDirs += 'src/main/kotlin' diff --git a/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt b/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt index 8a7c889..3e05682 100644 --- a/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt +++ b/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt @@ -19,6 +19,8 @@ package com.nimroddayan.flutternsd import android.net.nsd.NsdManager import android.net.nsd.NsdServiceInfo +import android.os.Build +import android.os.Build.VERSION_CODES import android.os.Handler import android.os.Looper import androidx.annotation.NonNull @@ -241,10 +243,17 @@ private fun NsdServiceInfo?.toMap(): Map { val port = this?.port val name = this?.serviceName val txt = this?.attributes + val hostAddresses = if(Build.VERSION.SDK_INT > VERSION_CODES.UPSIDE_DOWN_CAKE) { + this?.hostAddresses?.map { it.hostAddress } + } else { + listOf(this?.host?.hostAddress) + } + Timber.v("Resolved service: $name-$hostname:$port $txt") return mapOf( "hostname" to hostname, + "hostAddresses" to hostAddresses, "port" to port, "name" to name, "txt" to txt diff --git a/lib/flutter_nsd.dart b/lib/flutter_nsd.dart index 5266a7d..f7f51ac 100644 --- a/lib/flutter_nsd.dart +++ b/lib/flutter_nsd.dart @@ -91,7 +91,12 @@ class FlutterNsd { final int port = call.arguments['port']; final String name = call.arguments['name']; final Map txt = Map.from(call.arguments['txt']); - var nsdServiceInfo = NsdServiceInfo(hostname, port, name, txt); + List? hostAddresses; + List? rawAddresses = call.arguments['hostAddresses']; + if (rawAddresses != null) { + hostAddresses = rawAddresses.where((e) => e != null).map((e) => e.toString()).toList(); + } + var nsdServiceInfo = NsdServiceInfo(hostname, port, name, txt, hostAddresses: hostAddresses); return nsdServiceInfo; } @@ -104,11 +109,12 @@ class FlutterNsd { /// Info class for holding discovered service class NsdServiceInfo { final String? hostname; + final List? hostAddresses; final int? port; final String? name; final Map? txt; - NsdServiceInfo(this.hostname, this.port, this.name, this.txt); + NsdServiceInfo(this.hostname, this.port, this.name, this.txt, {this.hostAddresses}); } /// List of possible error codes of NsdError diff --git a/windows/flutter_nsd_plugin.cpp b/windows/flutter_nsd_plugin.cpp index 39eea95..8fd15cf 100644 --- a/windows/flutter_nsd_plugin.cpp +++ b/windows/flutter_nsd_plugin.cpp @@ -37,6 +37,7 @@ namespace { std::string servicename; std::string ipv4address; std::string ipv6address; + flutter::EncodableList addressList; int port; std::map txt; }; @@ -109,6 +110,7 @@ namespace { {flutter::EncodableValue("hostname"), flutter::EncodableValue(packet.hostname)}, {flutter::EncodableValue("ipv4address"), flutter::EncodableValue(packet.ipv4address)}, {flutter::EncodableValue("ipv6address"), flutter::EncodableValue(packet.ipv6address)}, + {flutter::EncodableValue("hostAddresses"), packet.addressList}, {flutter::EncodableValue("port"), flutter::EncodableValue(packet.port)}, {flutter::EncodableValue("name"), flutter::EncodableValue(name)}, {flutter::EncodableValue("txt"), flutter::EncodableValue(packet.txt)} @@ -158,6 +160,7 @@ namespace { MdnsResult& packet = packets[base]; packet.dnsname = _hostname; packet.ipv4address = _address; + packet.addressList.push_back(flutter::EncodableValue(_address)); process(base, last); } @@ -167,6 +170,7 @@ namespace { MdnsResult& packet = packets[base]; packet.dnsname = _hostname; packet.ipv6address = _address; + packet.addressList.push_back(flutter::EncodableValue(_address)); process(base, last); } void MdnsRequest::callbackTXT(const void* base, boolean last, STRING_ARG_DECL(key), STRING_ARG_DECL(value)) { diff --git a/windows/mdns_impl.c b/windows/mdns_impl.c index 8a3648f..e2f8406 100644 --- a/windows/mdns_impl.c +++ b/windows/mdns_impl.c @@ -7,9 +7,9 @@ #include #define sleep(x) Sleep(x * 1000) -#if (_DEBUG == 0) +//#if (_DEBUG == 0) #define printf -#endif +//#endif #include "mdns.h"