Skip to content

Commit

Permalink
feat(windows, ip-address): add ipAddress info to windows details (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed Mar 2, 2022
1 parent 6b01fee commit 11f3e3b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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. |

Expand Down
25 changes: 25 additions & 0 deletions windows/RNCNetInfoCPP/RNCNetInfo.cpp
Expand Up @@ -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<NetInfoState> currentRequest, std::function<void(NetInfoState)> onComplete) {
auto state = co_await currentRequest;
if (previousRequest) {
Expand Down Expand Up @@ -164,6 +188,7 @@ namespace winrt::ReactNativeNetInfo::implementation {
if (signal) {
details.strength = winrt::unbox_value<uint8_t>(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"));
Expand Down
7 changes: 7 additions & 0 deletions windows/RNCNetInfoCPP/RNCNetInfo.h
Expand Up @@ -49,6 +49,13 @@ namespace winrt::ReactNativeNetInfo::implementation {
REACT_FIELD(isConnected);
bool isConnected;

/// <summary>
/// IP Address of the current connection if available
/// </summary>
/// <param name=""></param>
REACT_FIELD(ipAddress);
std::string ipAddress;

/// <summary>
/// Is the internet reachable with the active network
/// </summary>
Expand Down

0 comments on commit 11f3e3b

Please sign in to comment.