Skip to content

Commit cf0fb8f

Browse files
sbecamatt-oakes
authored andcommitted
feat: Add Windows support (#103 by @sbeca)
1 parent 8350d2f commit cf0fb8f

12 files changed

+911
-13
lines changed

README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
# `@react-native-community/netinfo`
33

4-
[![CircleCI Status](https://img.shields.io/circleci/project/github/react-native-community/react-native-netinfo/master.svg)](https://circleci.com/gh/react-native-community/workflows/react-native-netinfo/tree/master) ![Supports Android and iOS](https://img.shields.io/badge/platforms-android%20|%20ios-lightgrey.svg) ![MIT License](https://img.shields.io/npm/l/@react-native-community/netinfo.svg)
4+
[![CircleCI Status](https://img.shields.io/circleci/project/github/react-native-community/react-native-netinfo/master.svg)](https://circleci.com/gh/react-native-community/workflows/react-native-netinfo/tree/master) ![Supports Android, iOS, and Windows](https://img.shields.io/badge/platforms-android%20|%20ios%20|%20windows-lightgrey.svg) ![MIT License](https://img.shields.io/npm/l/@react-native-community/netinfo.svg)
55

6-
React Native Network Info API for Android & iOS. It allows you to get information on:
6+
React Native Network Info API for Android, iOS & Windows. It allows you to get information on:
77

88
* Connection type
99
* Connection quality
@@ -79,6 +79,28 @@ protected List<ReactPackage> getPackages() {
7979
```
8080
</details>
8181

82+
<details>
83+
<summary>Manually link the library on Windows</summary>
84+
85+
* Open the solution in Visual Studio for your Windows apps
86+
* Right click in the Explorer and click Add > Existing Project...
87+
* Navigate to `./<app-name>/node_modules/@react-native-community/netinfo/windows/RNCNetInfo/` and add `RNCNetInfo.csproj`
88+
* This time right click on your React Native Windows app under your solutions directory and click Add > Reference...
89+
* Check the `RNCNetInfo` you just added and press ok
90+
* Open up `MainReactNativeHost.cs` for your app and edit the file like so:
91+
92+
```diff
93+
+ using ReactNativeCommunity.NetInfo;
94+
......
95+
protected override List<IReactPackage> Packages => new List<IReactPackage>
96+
{
97+
new MainReactPackage(),
98+
+ new RNCNetInfoPackage(),
99+
};
100+
```
101+
102+
</details>
103+
82104
## Migrating from the core `react-native` module
83105
This module was created when the NetInfo was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:
84106

@@ -170,17 +192,17 @@ The `details` value depends on the `type` value.
170192
#### `NetInfoStateType`
171193
Describes the current type of network connection. It is an enum with these possible values:
172194

173-
| Value | Platform | Description |
174-
| ----------- | ------------ | ---------------------------------------------------------- |
175-
| `none` | Android, iOS | No network connection is active |
176-
| `unknown` | Android, iOS | The network state could not or has yet to be be determined |
177-
| `cellular` | Android, iOS | Active network over cellular |
178-
| `wifi` | Android, iOS | Active network over Wifi |
179-
| `bluetooth` | Android | Active network over Bluetooth |
180-
| `ethernet` | Android | Active network over wired ethernet |
181-
| `wimax` | Android | Active network over WiMax |
182-
| `vpn` | Android | Active network over VPN |
183-
| `other` | Android, iOS | Active network over another type of network |
195+
| Value | Platform | Description |
196+
| ----------- | --------------------- | ---------------------------------------------------------- |
197+
| `none` | Android, iOS, Windows | No network connection is active |
198+
| `unknown` | Android, iOS, Windows | The network state could not or has yet to be be determined |
199+
| `cellular` | Android, iOS, Windows | Active network over cellular |
200+
| `wifi` | Android, iOS, Windows | Active network over Wifi |
201+
| `bluetooth` | Android | Active network over Bluetooth |
202+
| `ethernet` | Android, Windows | Active network over wired ethernet |
203+
| `wimax` | Android | Active network over WiMax |
204+
| `vpn` | Android | Active network over VPN |
205+
| `other` | Android, iOS, Windows | Active network over another type of network |
184206

185207
#### `NetInfoCellularGeneration`
186208
Describes the current generation of the `cellular` connection. It is an enum with these possible values:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"/android",
1111
"!/android/build",
1212
"/ios",
13+
"/windows",
1314
"/src",
1415
"/lib",
1516
"/*.podspec"

windows/.gitignore

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
*AppPackages*
2+
*BundleArtifacts*
3+
*ReactAssets*
4+
5+
# OS junk files
6+
[Tt]humbs.db
7+
*.DS_Store
8+
9+
# Visual Studio files
10+
*.[Oo]bj
11+
*.user
12+
*.aps
13+
*.pch
14+
*.vspscc
15+
*.vssscc
16+
*_i.c
17+
*_p.c
18+
*.ncb
19+
*.suo
20+
*.tlb
21+
*.tlh
22+
*.bak
23+
*.[Cc]ache
24+
*.ilk
25+
*.log
26+
*.lib
27+
*.sbr
28+
*.sdf
29+
*.opensdf
30+
*.opendb
31+
*.unsuccessfulbuild
32+
ipch/
33+
[Oo]bj/
34+
[Bb]in
35+
[Dd]ebug*/
36+
[Rr]elease*/
37+
Ankh.NoLoad
38+
39+
# MonoDevelop
40+
*.pidb
41+
*.userprefs
42+
43+
# Tooling
44+
_ReSharper*/
45+
*.resharper
46+
[Tt]est[Rr]esult*
47+
*.sass-cache
48+
49+
# Project files
50+
[Bb]uild/
51+
52+
# Subversion files
53+
.svn
54+
55+
# Office Temp Files
56+
~$*
57+
58+
# vim Temp Files
59+
*~
60+
61+
# NuGet
62+
packages/
63+
*.nupkg
64+
65+
# ncrunch
66+
*ncrunch*
67+
*crunch*.local.xml
68+
69+
# Visual Studio database projects
70+
*.dbmdl
71+
72+
# Test files
73+
*.testsettings
74+
75+
#Other files
76+
*.DotSettings
77+
.vs/
78+
*project.lock.json

windows/RNCNetInfo.sln

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RNCNetInfo", "RNCNetInfo\RNCNetInfo.csproj", "{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactNative", "..\node_modules\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj", "{C7673AD5-E3AA-468C-A5FD-FA38154E205C}"
9+
EndProject
10+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChakraBridge", "..\node_modules\react-native-windows\ReactWindows\ChakraBridge\ChakraBridge.vcxproj", "{4B72C796-16D5-4E3A-81C0-3E36F531E578}"
11+
EndProject
12+
Global
13+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
14+
..\node_modules\react-native-windows\ReactWindows\ReactNative.Shared\ReactNative.Shared.projitems*{c7673ad5-e3aa-468c-a5fd-fa38154e205c}*SharedItemsImports = 4
15+
EndGlobalSection
16+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17+
Debug|ARM = Debug|ARM
18+
Debug|x64 = Debug|x64
19+
Debug|x86 = Debug|x86
20+
Development|ARM = Development|ARM
21+
Development|x64 = Development|x64
22+
Development|x86 = Development|x86
23+
Release|ARM = Release|ARM
24+
Release|x64 = Release|x64
25+
Release|x86 = Release|x86
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|ARM.ActiveCfg = Debug|ARM
29+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|ARM.Build.0 = Debug|ARM
30+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x64.ActiveCfg = Debug|x64
31+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x64.Build.0 = Debug|x64
32+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x86.ActiveCfg = Debug|x86
33+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x86.Build.0 = Debug|x86
34+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|ARM.ActiveCfg = Development|ARM
35+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|ARM.Build.0 = Development|ARM
36+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x64.ActiveCfg = Development|x64
37+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x64.Build.0 = Development|x64
38+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x86.ActiveCfg = Development|x86
39+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x86.Build.0 = Development|x86
40+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|ARM.ActiveCfg = Release|ARM
41+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|ARM.Build.0 = Release|ARM
42+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x64.ActiveCfg = Release|x64
43+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x64.Build.0 = Release|x64
44+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x86.ActiveCfg = Release|x86
45+
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x86.Build.0 = Release|x86
46+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|ARM.ActiveCfg = Debug|ARM
47+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|ARM.Build.0 = Debug|ARM
48+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x64.ActiveCfg = Debug|x64
49+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x64.Build.0 = Debug|x64
50+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x86.ActiveCfg = Debug|x86
51+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x86.Build.0 = Debug|x86
52+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|ARM.ActiveCfg = Debug|ARM
53+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|ARM.Build.0 = Debug|ARM
54+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x64.ActiveCfg = Debug|x64
55+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x64.Build.0 = Debug|x64
56+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x86.ActiveCfg = Debug|x86
57+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x86.Build.0 = Debug|x86
58+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|ARM.ActiveCfg = Release|ARM
59+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|ARM.Build.0 = Release|ARM
60+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x64.ActiveCfg = Release|x64
61+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x64.Build.0 = Release|x64
62+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x86.ActiveCfg = Release|x86
63+
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x86.Build.0 = Release|x86
64+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|ARM.ActiveCfg = Debug|ARM
65+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|ARM.Build.0 = Debug|ARM
66+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x64.ActiveCfg = Debug|x64
67+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x64.Build.0 = Debug|x64
68+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x86.ActiveCfg = Debug|Win32
69+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x86.Build.0 = Debug|Win32
70+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|ARM.ActiveCfg = Debug|ARM
71+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|ARM.Build.0 = Debug|ARM
72+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x64.ActiveCfg = Debug|x64
73+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x64.Build.0 = Debug|x64
74+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x86.ActiveCfg = Debug|Win32
75+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x86.Build.0 = Debug|Win32
76+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|ARM.ActiveCfg = Release|ARM
77+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|ARM.Build.0 = Release|ARM
78+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x64.ActiveCfg = Release|x64
79+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x64.Build.0 = Release|x64
80+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x86.ActiveCfg = Release|Win32
81+
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x86.Build.0 = Release|Win32
82+
EndGlobalSection
83+
GlobalSection(SolutionProperties) = preSolution
84+
HideSolutionNode = FALSE
85+
EndGlobalSection
86+
EndGlobal
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Windows.Networking.Connectivity;
5+
6+
namespace ReactNativeCommunity.NetInfo
7+
{
8+
class DefaultNetworkInformation : INetworkInformation
9+
{
10+
public event NetworkStatusChangedEventHandler NetworkStatusChanged;
11+
12+
public void Start()
13+
{
14+
NetworkInformation.NetworkStatusChanged += OnNetworkStatusChanged;
15+
}
16+
17+
public void Stop()
18+
{
19+
NetworkInformation.NetworkStatusChanged -= OnNetworkStatusChanged;
20+
}
21+
22+
public IConnectionProfile GetInternetConnectionProfile()
23+
{
24+
var profile = NetworkInformation.GetInternetConnectionProfile();
25+
return profile != null
26+
? new ConnectionProfileImpl(profile)
27+
: null;
28+
}
29+
30+
private void OnNetworkStatusChanged(object sender)
31+
{
32+
NetworkStatusChanged?.Invoke(sender);
33+
}
34+
35+
class ConnectionProfileImpl : IConnectionProfile
36+
{
37+
private readonly ConnectionProfile _profile;
38+
39+
public ConnectionProfileImpl(ConnectionProfile profile)
40+
{
41+
_profile = profile;
42+
}
43+
44+
public NetworkConnectivityLevel ConnectivityLevel
45+
{
46+
get
47+
{
48+
return _profile.GetNetworkConnectivityLevel();
49+
}
50+
}
51+
52+
public NetworkConnectionType ConnectionType
53+
{
54+
get
55+
{
56+
if (_profile.IsWlanConnectionProfile)
57+
{
58+
return NetworkConnectionType.Wifi;
59+
}
60+
else if (_profile.IsWwanConnectionProfile)
61+
{
62+
return NetworkConnectionType.Cellular;
63+
}
64+
65+
var networkAdapter = _profile.NetworkAdapter;
66+
if (networkAdapter == null)
67+
{
68+
return NetworkConnectionType.Unknown;
69+
}
70+
71+
// Possible values: https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib
72+
if (networkAdapter.IanaInterfaceType == 6u)
73+
{
74+
return NetworkConnectionType.Ethernet;
75+
}
76+
else
77+
{
78+
return NetworkConnectionType.Other;
79+
}
80+
}
81+
}
82+
83+
public CellularGeneration CellularGeneration
84+
{
85+
get
86+
{
87+
if (!_profile.IsWwanConnectionProfile)
88+
{
89+
return CellularGeneration.None;
90+
}
91+
92+
var dataClass = _profile.WwanConnectionProfileDetails.GetCurrentDataClass();
93+
94+
switch (dataClass) {
95+
case WwanDataClass.None:
96+
return CellularGeneration.None;
97+
case WwanDataClass.Edge:
98+
case WwanDataClass.Gprs:
99+
return CellularGeneration.Generation2;
100+
case WwanDataClass.Cdma1xEvdo:
101+
case WwanDataClass.Cdma1xEvdoRevA:
102+
case WwanDataClass.Cdma1xEvdoRevB:
103+
case WwanDataClass.Cdma1xEvdv:
104+
case WwanDataClass.Cdma1xRtt:
105+
case WwanDataClass.Cdma3xRtt:
106+
case WwanDataClass.Hsdpa:
107+
case WwanDataClass.Hsupa:
108+
case WwanDataClass.Umts:
109+
return CellularGeneration.Generation3;
110+
case WwanDataClass.CdmaUmb:
111+
case WwanDataClass.LteAdvanced:
112+
return CellularGeneration.Generation4;
113+
case WwanDataClass.Custom:
114+
default:
115+
return CellularGeneration.Unknown;
116+
}
117+
}
118+
}
119+
120+
public NetworkCostType ConnectionCost
121+
{
122+
get
123+
{
124+
return _profile.GetConnectionCost().NetworkCostType;
125+
}
126+
}
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)