diff --git a/README.md b/README.md index 65b1075d..c3e8d1ec 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ The `details` value depends on the `type` value. | `ssid` | Android, iOS (not tvOS), Windows | `string` | The SSID of the network. May not be present, `null`, or an empty string if it cannot be determined. **On iOS, your app must meet at least one of the [following requirements](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo?language=objc#discussion) and you must set the `shouldFetchWiFiSSID` configuration option or no attempt will be made to fetch the SSID. On Android, you need to have the `ACCESS_FINE_LOCATION` permission in your `AndroidManifest.xml` and accepted by the user**. | | `bssid` | Android, iOS (not tvOS), Windows* | `string` | The BSSID of the network. May not be present, `null`, or an empty string if it cannot be determined. **On iOS, make sure your app meets at least one of the [following requirements](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo?language=objc#discussion). On Android, you need to have the `ACCESS_FINE_LOCATION` permission in your `AndroidManifest.xml` and accepted by the user**. | | `strength` | Android, Windows | `number` | An integer number from `0` to `100` for the signal strength. May not be present if the signal strength cannot be determined. | -| `ipAddress` | Android, iOS, macOS | `string` | The external IP address. Can be in IPv4 or IPv6 format. May not be present if it cannot be determined. | +| `ipAddress` | Android, iOS, macOS, Windows | `string` | The external IP address. Can be in IPv4 or IPv6 format. May not be present if it cannot be determined. | | `subnet` | Android, iOS, macOS | `string` | The subnet mask in IPv4 format. May not be present if it cannot be determined. | | `frequency` | Android, Windows* | `number` | Network frequency. Example: For 2.4 GHz networks, the method will return 2457. May not be present if it cannot be determined. | diff --git a/windows/RNCNetInfoCPP/RNCNetInfo.cpp b/windows/RNCNetInfoCPP/RNCNetInfo.cpp index 6b9ba32b..53463b2c 100644 --- a/windows/RNCNetInfoCPP/RNCNetInfo.cpp +++ b/windows/RNCNetInfoCPP/RNCNetInfo.cpp @@ -105,6 +105,30 @@ namespace winrt::ReactNativeNetInfo::implementation { co_return nullptr; } + std::string getIpAddressSync() noexcept + { + auto icp = Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile(); + if (!icp || !icp.NetworkAdapter()) + { + return "unknown"; + } else + { + auto hostnames = Windows::Networking::Connectivity::NetworkInformation::GetHostNames(); + for (auto const& hostname : hostnames) + { + if ( + hostname.Type() == Windows::Networking::HostNameType::Ipv4 && + hostname.IPInformation() && + hostname.IPInformation().NetworkAdapter() && + hostname.IPInformation().NetworkAdapter().NetworkAdapterId() == icp.NetworkAdapter().NetworkAdapterId()) + { + return winrt::to_string(hostname.CanonicalName()); + } + } + return "unknown"; + } + } + IAsyncAction ChainGetNetworkStatus(IAsyncAction previousRequest, std::future currentRequest, std::function onComplete) { auto state = co_await currentRequest; if (previousRequest) { @@ -164,6 +188,7 @@ namespace winrt::ReactNativeNetInfo::implementation { if (signal) { details.strength = winrt::unbox_value(signal) * 20; // Signal strength is 0-5 but we want 0-100. } + state.ipAddress = getIpAddressSync(); if (isWifiConnection) { if (!profile.IsWlanConnectionProfile()) { throw (std::runtime_error("Wifi profile is not available")); diff --git a/windows/RNCNetInfoCPP/RNCNetInfo.h b/windows/RNCNetInfoCPP/RNCNetInfo.h index dc662981..f0080f58 100644 --- a/windows/RNCNetInfoCPP/RNCNetInfo.h +++ b/windows/RNCNetInfoCPP/RNCNetInfo.h @@ -49,6 +49,13 @@ namespace winrt::ReactNativeNetInfo::implementation { REACT_FIELD(isConnected); bool isConnected; + /// + /// IP Address of the current connection if available + /// + /// + REACT_FIELD(ipAddress); + std::string ipAddress; + /// /// Is the internet reachable with the active network ///