@pietro-fe01/nativescript-connectivity-manager-plugin 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,126 +1,126 @@
1
- # NativeScript ConnectivityManager Plugin
2
-
3
- A plugin to manage the device connectivity on Android and iOS.
4
-
5
- - [x] Android
6
- - [x] WiFi
7
- - [x] Cellular
8
- - [x] GPS
9
- - [ ] Bluetooth
10
- - [ ] Grant permissions
11
- - [x] iOS
12
- - [x] WiFi
13
- - [x] Connect to wifi
14
- - [x] Get SSID
15
- - [] Others not implemented yet
16
- - [ ] Cellular
17
- - [ ] GPS
18
- - [ ] Bluetooth
19
-
20
- ## Installation
21
-
22
- `tns plugin add nativescript-connectivity-manager-plugin`
23
-
24
- ## Demo
25
-
26
- Check out the [Angular demo app](https://github.com/1IoT/nativescript-connectivity-manager-plugin/blob/master/demo-angular/src/app/home/home.component.ts)
27
- and run it locally:
28
-
29
- ```
30
- git clone https://github.com/1IoT/nativescript-connectivity-manager-plugin
31
- cd nativescript-connectivity-manager-plugin/src
32
- npm run demo:android
33
- ```
34
-
35
- ## Usage
36
-
37
- ```
38
- import {ConnectivityManagerImpl} from 'nativescript-connectivity-manager-plugin';
39
-
40
- @Component({
41
- selector: "Home",
42
- templateUrl: "./home.component.html"
43
- })
44
- export class HomeComponent implements OnInit {
45
-
46
- private static NETWORK_SSID: string = "MY_SSID";
47
- private static NETWORK_PASSPHARSE: string = "MY_KEY";
48
- private static CONNECTION_TIMEOUT_MS: number = 30000;
49
- private static DISCONNECT_TIMEOUT_MS: number = 15000;
50
-
51
- constructor(private connectivityManager: ConnectivityManagerImpl, private httpClient: HttpClient) {
52
- }
53
-
54
- ngOnInit(): void {
55
- }
56
-
57
- public async getInfos(): Promise<void> {
58
- const ssid = await this.connectivityManager.getSSIDAsync();
59
- console.log("Wifi SSID: " + ssid);
60
- console.log("NetworkId: " + this.connectivityManager.getWifiNetworkId());
61
- console.log("Wifi enabled: " + this.connectivityManager.isWifiEnabled());
62
- console.log("Wifi connected: " + this.connectivityManager.isWifiConnected());
63
- console.log("Cellular enabled: " + this.connectivityManager.isCellularEnabled());
64
- console.log("Cellular connected: " + this.connectivityManager.isCellularConnected());
65
- console.log("GPS enabled: " + this.connectivityManager.isGpsEnabled());
66
- console.log("GPS connected: " + this.connectivityManager.isGpsConnected());
67
- }
68
-
69
- public scan(): void {
70
- console.log("Start scan...");
71
- this.connectivityManager.scanWifiNetworks().then((wifiSSIDs: string[]) => {
72
- console.log(wifiSSIDs);
73
- });
74
- }
75
-
76
- public async connect(): Promise<boolean> {
77
- console.log("Start connection...");
78
- console.log("Disconnect with the source network...");
79
- return this.connectivityManager.connectToWifiNetwork(HomeComponent.NETWORK_SSID, HomeComponent.NETWORK_PASSPHARSE, HomeComponent.CONNECTION_TIMEOUT_MS);
80
- }
81
-
82
- public async disconnect(): Promise<boolean> {
83
- return this.connectivityManager.disconnectWifiNetwork(HomeComponent.DISCONNECT_TIMEOUT_MS);
84
- }
85
- }
86
-
87
- ```
88
-
89
- ## API
90
-
91
- Requires **Android SDK**: 29
92
-
93
- **WARNING: Note that even for scanning WiFi and retrieving the SSID, location permission must be given and GPS must be enabled!**
94
-
95
- ### iOS / iPadOS SSID Notes
96
-
97
- - `getSSID()` is a legacy synchronous best-effort API and may return `null` on newer iOS/iPadOS versions when only CaptiveNetwork APIs are used.
98
- - Use `getSSIDAsync()` on iOS/iPadOS 14+ (including iOS/iPadOS 26+) because it uses `NEHotspotNetwork.fetchCurrentWithCompletionHandler`.
99
- - To retrieve the SSID on iOS, apps must include the **Access Wi-Fi Information** entitlement (`com.apple.developer.networking.wifi-info`) and satisfy at least one Apple runtime condition (for example: the app configured the current network via `NEHotspotConfiguration`, or the app has CoreLocation authorization with precise location).
100
-
101
- | Method | Return | Description |
102
- | -------------------------------------------------------------------------------- | ------------------- | ---------------------------------------------------- |
103
- | getSSID() | string | legacy synchronous best-effort SSID read |
104
- | async getSSIDAsync() | Promise\<string \| null\> | recommended SSID API, especially on iOS/iPadOS 14+ |
105
- | getWifiNetworkId() | number |
106
- | isWifiEnabled() | boolean |
107
- | isWifiConnected() | boolean |
108
- | isCellularEnabled() | boolean |
109
- | isCellularConnected() | boolean |
110
- | isGpsEnabled() | boolean |
111
- | isGpsConnected() | boolean |
112
- | hasInternet() | boolean |
113
- | async scanWifiNetworks() | Promise\<string[]\> | requires granted location permission and enabled gps |
114
- | async connectToWifiNetwork(ssid: string, password: string, milliseconds: number) | Promise\<boolean\> |
115
- | async disconnectWifiNetwork(timeoutMs: number) | Promise\<boolean\> |
116
-
117
- To grant permissions (location permissions) please use the [nativescript-advanced-permissions plugin](https://market.nativescript.org/plugins/nativescript-advanced-permissions/) or implement another mechanism. IMHO, dealing with permissions should not be done on plugin level but on application level instead.
118
-
119
- ## Tips
120
-
121
- - Docs about the [tns-platform-declarations](https://github.com/NativeScript/NativeScript/tree/master/tns-platform-declarations)
122
- - If the project cannot be build, maybe `npm run demo:reset` and `npm run build` can fix it
123
-
124
- ## License
125
-
126
- Apache License Version 2.0, January 2004
1
+ # NativeScript ConnectivityManager Plugin
2
+
3
+ A plugin to manage the device connectivity on Android and iOS.
4
+
5
+ - [x] Android
6
+ - [x] WiFi
7
+ - [x] Cellular
8
+ - [x] GPS
9
+ - [ ] Bluetooth
10
+ - [ ] Grant permissions
11
+ - [x] iOS
12
+ - [x] WiFi
13
+ - [x] Connect to wifi
14
+ - [x] Get SSID
15
+ - [] Others not implemented yet
16
+ - [ ] Cellular
17
+ - [ ] GPS
18
+ - [ ] Bluetooth
19
+
20
+ ## Installation
21
+
22
+ `tns plugin add nativescript-connectivity-manager-plugin`
23
+
24
+ ## Demo
25
+
26
+ Check out the [Angular demo app](https://github.com/1IoT/nativescript-connectivity-manager-plugin/blob/master/demo-angular/src/app/home/home.component.ts)
27
+ and run it locally:
28
+
29
+ ```
30
+ git clone https://github.com/1IoT/nativescript-connectivity-manager-plugin
31
+ cd nativescript-connectivity-manager-plugin/src
32
+ npm run demo:android
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```
38
+ import {ConnectivityManagerImpl} from 'nativescript-connectivity-manager-plugin';
39
+
40
+ @Component({
41
+ selector: "Home",
42
+ templateUrl: "./home.component.html"
43
+ })
44
+ export class HomeComponent implements OnInit {
45
+
46
+ private static NETWORK_SSID: string = "MY_SSID";
47
+ private static NETWORK_PASSPHARSE: string = "MY_KEY";
48
+ private static CONNECTION_TIMEOUT_MS: number = 30000;
49
+ private static DISCONNECT_TIMEOUT_MS: number = 15000;
50
+
51
+ constructor(private connectivityManager: ConnectivityManagerImpl, private httpClient: HttpClient) {
52
+ }
53
+
54
+ ngOnInit(): void {
55
+ }
56
+
57
+ public async getInfos(): Promise<void> {
58
+ const ssid = await this.connectivityManager.getSSIDAsync();
59
+ console.log("Wifi SSID: " + ssid);
60
+ console.log("NetworkId: " + this.connectivityManager.getWifiNetworkId());
61
+ console.log("Wifi enabled: " + this.connectivityManager.isWifiEnabled());
62
+ console.log("Wifi connected: " + this.connectivityManager.isWifiConnected());
63
+ console.log("Cellular enabled: " + this.connectivityManager.isCellularEnabled());
64
+ console.log("Cellular connected: " + this.connectivityManager.isCellularConnected());
65
+ console.log("GPS enabled: " + this.connectivityManager.isGpsEnabled());
66
+ console.log("GPS connected: " + this.connectivityManager.isGpsConnected());
67
+ }
68
+
69
+ public scan(): void {
70
+ console.log("Start scan...");
71
+ this.connectivityManager.scanWifiNetworks().then((wifiSSIDs: string[]) => {
72
+ console.log(wifiSSIDs);
73
+ });
74
+ }
75
+
76
+ public async connect(): Promise<boolean> {
77
+ console.log("Start connection...");
78
+ console.log("Disconnect with the source network...");
79
+ return this.connectivityManager.connectToWifiNetwork(HomeComponent.NETWORK_SSID, HomeComponent.NETWORK_PASSPHARSE, HomeComponent.CONNECTION_TIMEOUT_MS);
80
+ }
81
+
82
+ public async disconnect(): Promise<boolean> {
83
+ return this.connectivityManager.disconnectWifiNetwork(HomeComponent.DISCONNECT_TIMEOUT_MS);
84
+ }
85
+ }
86
+
87
+ ```
88
+
89
+ ## API
90
+
91
+ Requires **Android SDK**: 29
92
+
93
+ **WARNING: Note that even for scanning WiFi and retrieving the SSID, location permission must be given and GPS must be enabled!**
94
+
95
+ ### iOS / iPadOS SSID Notes
96
+
97
+ - `getSSID()` is a legacy synchronous best-effort API and may return `null` on newer iOS/iPadOS versions when only CaptiveNetwork APIs are used.
98
+ - Use `getSSIDAsync()` on iOS/iPadOS 14+ (including iOS/iPadOS 26+) because it uses `NEHotspotNetwork.fetchCurrentWithCompletionHandler`.
99
+ - To retrieve the SSID on iOS, apps must include the **Access Wi-Fi Information** entitlement (`com.apple.developer.networking.wifi-info`) and satisfy at least one Apple runtime condition (for example: the app configured the current network via `NEHotspotConfiguration`, or the app has CoreLocation authorization with precise location).
100
+
101
+ | Method | Return | Description |
102
+ | -------------------------------------------------------------------------------- | ------------------- | ---------------------------------------------------- |
103
+ | getSSID() | string | legacy synchronous best-effort SSID read |
104
+ | async getSSIDAsync() | Promise\<string \| null\> | recommended SSID API, especially on iOS/iPadOS 14+ |
105
+ | getWifiNetworkId() | number |
106
+ | isWifiEnabled() | boolean |
107
+ | isWifiConnected() | boolean |
108
+ | isCellularEnabled() | boolean |
109
+ | isCellularConnected() | boolean |
110
+ | isGpsEnabled() | boolean |
111
+ | isGpsConnected() | boolean |
112
+ | hasInternet() | boolean |
113
+ | async scanWifiNetworks() | Promise\<string[]\> | requires granted location permission and enabled gps |
114
+ | async connectToWifiNetwork(ssid: string, password: string, milliseconds: number) | Promise\<boolean\> |
115
+ | async disconnectWifiNetwork(timeoutMs: number) | Promise\<boolean\> |
116
+
117
+ To grant permissions (location permissions) please use the [nativescript-advanced-permissions plugin](https://market.nativescript.org/plugins/nativescript-advanced-permissions/) or implement another mechanism. IMHO, dealing with permissions should not be done on plugin level but on application level instead.
118
+
119
+ ## Tips
120
+
121
+ - Docs about the [tns-platform-declarations](https://github.com/NativeScript/NativeScript/tree/master/tns-platform-declarations)
122
+ - If the project cannot be build, maybe `npm run demo:reset` and `npm run build` can fix it
123
+
124
+ ## License
125
+
126
+ Apache License Version 2.0, January 2004
@@ -0,0 +1,36 @@
1
+ import { Common } from "./connectivity-manager-impl.common";
2
+ import { ConnectivityManagerInterface } from "./connectivity-manager-interface";
3
+ export declare class ConnectivityManagerImpl extends Common implements ConnectivityManagerInterface {
4
+ private readonly WIFI_SSID_BLACKLIST;
5
+ private readonly wifiManager;
6
+ private readonly cellularManager;
7
+ private readonly locationManager;
8
+ private readonly connectivityManager;
9
+ private forcedNetworkCallback;
10
+ private previousConnectionMetered;
11
+ private previousConnectionWiFi;
12
+ private previousSsid;
13
+ private connectResolve;
14
+ private disconnectResolve;
15
+ private safeUnregisterNetworkCallback;
16
+ private static getCapabilities;
17
+ getSSID(): string;
18
+ getSSIDAsync(): Promise<string | null>;
19
+ getWifiNetworkId(): number;
20
+ isWifiEnabled(): boolean;
21
+ isWifiConnected(): boolean;
22
+ isCellularEnabled(): boolean;
23
+ isCellularConnected(): boolean;
24
+ isGpsEnabled(): boolean;
25
+ isGpsConnected(): boolean;
26
+ scanWifiNetworks(): Promise<string[]>;
27
+ connectToWifiNetwork(ssid: string, password: string, milliseconds: number): Promise<boolean>;
28
+ hasInternet(): boolean;
29
+ private static hasInternet;
30
+ disconnectWifiNetwork(timeoutMs: number): Promise<boolean>;
31
+ private static isPreviousOrStableNetwork;
32
+ private static logConnectivityInfo;
33
+ private static getInterfaceName;
34
+ private waitUntilConnectedToWifi;
35
+ private disconnectWifiAndRemoveNetwork;
36
+ }
@@ -0,0 +1,339 @@
1
+ import { Common } from "./connectivity-manager-impl.common";
2
+ import * as application from "@nativescript/core/application";
3
+ var Context = android.content.Context;
4
+ var WifiConfiguration = android.net.wifi.WifiConfiguration;
5
+ var ConnectivityManagerService = android.net.ConnectivityManager;
6
+ var WifiManagerService = android.net.wifi.WifiManager;
7
+ var LocationManagerService = android.location.LocationManager;
8
+ var NetworkRequest = android.net.NetworkRequest;
9
+ var NetworkCapabilities = android.net.NetworkCapabilities;
10
+ var WifiNetworkSpecifier = android.net.wifi.WifiNetworkSpecifier;
11
+ let IS_Q_VERSION = false;
12
+ try {
13
+ IS_Q_VERSION =
14
+ android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q;
15
+ }
16
+ catch (_a) {
17
+ IS_Q_VERSION = false;
18
+ }
19
+ export class ConnectivityManagerImpl extends Common {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.WIFI_SSID_BLACKLIST = ["", " "];
23
+ this.wifiManager = application.android.context.getSystemService(Context.WIFI_SERVICE);
24
+ this.cellularManager = application.android.context.getSystemService(Context.TELEPHONY_SERVICE);
25
+ this.locationManager = application.android.context.getSystemService(Context.LOCATION_SERVICE);
26
+ this.connectivityManager = application.android.context.getSystemService(Context.CONNECTIVITY_SERVICE);
27
+ this.previousConnectionMetered = false;
28
+ this.previousConnectionWiFi = false;
29
+ this.previousSsid = undefined;
30
+ this.connectResolve = null;
31
+ this.disconnectResolve = null;
32
+ }
33
+ safeUnregisterNetworkCallback(callback) {
34
+ if (!callback) {
35
+ return;
36
+ }
37
+ try {
38
+ this.connectivityManager.unregisterNetworkCallback(callback);
39
+ }
40
+ catch (_) {
41
+ }
42
+ }
43
+ static getCapabilities(connectivityManager, network) {
44
+ if (!network) {
45
+ return null;
46
+ }
47
+ return connectivityManager.getNetworkCapabilities(network);
48
+ }
49
+ getSSID() {
50
+ return this.wifiManager.getConnectionInfo().getSSID();
51
+ }
52
+ getSSIDAsync() {
53
+ return Promise.resolve(this.getSSID());
54
+ }
55
+ getWifiNetworkId() {
56
+ return this.wifiManager.getConnectionInfo().getNetworkId();
57
+ }
58
+ isWifiEnabled() {
59
+ return this.wifiManager.isWifiEnabled();
60
+ }
61
+ isWifiConnected() {
62
+ if (!this.isWifiEnabled()) {
63
+ throw new Error("Wifi is not enabled.");
64
+ }
65
+ if (IS_Q_VERSION) {
66
+ return (this.connectivityManager.getNetworkCapabilities(this.connectivityManager.getActiveNetwork()) &&
67
+ this.connectivityManager
68
+ .getNetworkCapabilities(this.connectivityManager.getActiveNetwork())
69
+ .hasTransport(NetworkCapabilities.TRANSPORT_WIFI));
70
+ }
71
+ else {
72
+ return this.connectivityManager
73
+ .getNetworkInfo(ConnectivityManagerService.TYPE_WIFI)
74
+ .isConnected();
75
+ }
76
+ }
77
+ isCellularEnabled() {
78
+ return this.cellularManager.isDataEnabled();
79
+ }
80
+ isCellularConnected() {
81
+ if (!this.isCellularEnabled()) {
82
+ throw new Error("Cellular is not enabled.");
83
+ }
84
+ const capabilities = ConnectivityManagerImpl.getCapabilities(this.connectivityManager, this.connectivityManager.getActiveNetwork());
85
+ return capabilities
86
+ ? capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
87
+ : false;
88
+ }
89
+ isGpsEnabled() {
90
+ return this.locationManager.isProviderEnabled(LocationManagerService.GPS_PROVIDER);
91
+ }
92
+ isGpsConnected() {
93
+ return this.locationManager.isProviderEnabled(LocationManagerService.NETWORK_PROVIDER);
94
+ }
95
+ async scanWifiNetworks() {
96
+ return new Promise((resolve) => {
97
+ application.android.registerBroadcastReceiver(WifiManagerService.SCAN_RESULTS_AVAILABLE_ACTION, () => {
98
+ application.android.unregisterBroadcastReceiver(WifiManagerService.SCAN_RESULTS_AVAILABLE_ACTION);
99
+ let wifiSsidList = [];
100
+ let wifiScanResult = this.wifiManager.getScanResults();
101
+ for (let i = 0; i < wifiScanResult.size(); i++) {
102
+ let wifi = wifiScanResult.get(i);
103
+ if (wifiSsidList.indexOf(wifi.SSID) == -1 &&
104
+ this.WIFI_SSID_BLACKLIST.indexOf(wifi.SSID) == -1) {
105
+ wifiSsidList.push(wifi.SSID);
106
+ }
107
+ }
108
+ resolve(wifiSsidList);
109
+ });
110
+ this.wifiManager.startScan();
111
+ });
112
+ }
113
+ async connectToWifiNetwork(ssid, password, milliseconds) {
114
+ return new Promise((resolve) => {
115
+ const safeTimeoutMs = Math.max(0, Number(milliseconds) || 0);
116
+ let completed = false;
117
+ let connectTimeout = null;
118
+ const complete = (result) => {
119
+ if (completed) {
120
+ return;
121
+ }
122
+ completed = true;
123
+ if (connectTimeout) {
124
+ clearTimeout(connectTimeout);
125
+ connectTimeout = null;
126
+ }
127
+ resolve(result);
128
+ };
129
+ if (!this.isWifiEnabled()) {
130
+ complete(false);
131
+ return;
132
+ }
133
+ try {
134
+ const that = this;
135
+ this.previousConnectionMetered = this.connectivityManager.isActiveNetworkMetered();
136
+ if (this.isWifiConnected()) {
137
+ this.previousConnectionWiFi = true;
138
+ this.previousSsid = this.getSSID();
139
+ }
140
+ else {
141
+ this.previousConnectionWiFi = false;
142
+ this.previousSsid = undefined;
143
+ }
144
+ this.connectResolve = complete;
145
+ connectTimeout = setTimeout(() => {
146
+ this.safeUnregisterNetworkCallback(this.forcedNetworkCallback);
147
+ complete(false);
148
+ }, safeTimeoutMs);
149
+ if (IS_Q_VERSION) {
150
+ let wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
151
+ .setSsid(ssid)
152
+ .setWpa2Passphrase(password)
153
+ .build();
154
+ let networkRequest = new NetworkRequest.Builder()
155
+ .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
156
+ .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
157
+ .setNetworkSpecifier(wifiNetworkSpecifier)
158
+ .build();
159
+ this.forcedNetworkCallback = new (ConnectivityManagerService.NetworkCallback.extend({
160
+ onAvailable: function (network) {
161
+ console.log("Connected to the network");
162
+ that.connectivityManager.bindProcessToNetwork(network);
163
+ that.connectResolve(true);
164
+ },
165
+ onUnavailable: function () {
166
+ this.super.onUnavailable();
167
+ that.connectResolve(false);
168
+ that.safeUnregisterNetworkCallback(that.forcedNetworkCallback);
169
+ },
170
+ }))();
171
+ console.log("Connecting to the network...");
172
+ this.connectivityManager.requestNetwork(networkRequest, this.forcedNetworkCallback, milliseconds);
173
+ }
174
+ else {
175
+ const ssidFormatted = `"${ssid}"`;
176
+ let conf = new WifiConfiguration();
177
+ conf.SSID = ssidFormatted;
178
+ if (password) {
179
+ conf.preSharedKey = '"' + password + '"';
180
+ conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
181
+ }
182
+ else {
183
+ conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
184
+ }
185
+ let networkRequest = new NetworkRequest.Builder()
186
+ .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
187
+ .build();
188
+ this.forcedNetworkCallback = new (ConnectivityManagerService.NetworkCallback.extend({
189
+ onAvailable: function (network) {
190
+ if (that.getSSID() == ssidFormatted) {
191
+ console.log("Connected to the network");
192
+ that.connectivityManager.bindProcessToNetwork(network);
193
+ that.connectivityManager.unregisterNetworkCallback(this);
194
+ that.connectResolve(true);
195
+ }
196
+ },
197
+ onUnavailable: function () {
198
+ this.super.onUnavailable();
199
+ },
200
+ }))();
201
+ this.connectivityManager.registerNetworkCallback(networkRequest, this.forcedNetworkCallback);
202
+ const list = this.wifiManager.getConfiguredNetworks();
203
+ let netId = -1;
204
+ for (let i = 0; i < list.size(); i++) {
205
+ const network = list.get(i);
206
+ if (network.SSID === ssidFormatted) {
207
+ netId = network.networkId;
208
+ break;
209
+ }
210
+ }
211
+ if (netId == -1) {
212
+ netId = this.wifiManager.addNetwork(conf);
213
+ }
214
+ this.wifiManager.enableNetwork(netId, true);
215
+ }
216
+ }
217
+ catch (error) {
218
+ console.log("Something went wrong while connecting to the Wi-Fi. " + error);
219
+ complete(false);
220
+ }
221
+ });
222
+ }
223
+ hasInternet() {
224
+ return ConnectivityManagerImpl.hasInternet(this.connectivityManager, this.connectivityManager.getActiveNetwork());
225
+ }
226
+ static hasInternet(connectivityManager, network) {
227
+ const capabilities = ConnectivityManagerImpl.getCapabilities(connectivityManager, network);
228
+ return capabilities
229
+ ? capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
230
+ : false;
231
+ }
232
+ async disconnectWifiNetwork(timeoutMs) {
233
+ return new Promise((resolve) => {
234
+ let that = this;
235
+ let networkConnectivity = null;
236
+ let completed = false;
237
+ const complete = (result) => {
238
+ if (completed) {
239
+ return;
240
+ }
241
+ completed = true;
242
+ clearTimeout(promiseTimeout);
243
+ that.safeUnregisterNetworkCallback(networkConnectivity);
244
+ resolve(result);
245
+ };
246
+ this.disconnectResolve = complete;
247
+ let promiseTimeout = setTimeout(() => {
248
+ console.log("Ran into timeout when disconnecting and fetching new connection.");
249
+ complete(false);
250
+ }, timeoutMs);
251
+ if (IS_Q_VERSION) {
252
+ networkConnectivity = new (ConnectivityManagerService.NetworkCallback.extend({
253
+ onAvailable: function (network) {
254
+ ConnectivityManagerImpl.logConnectivityInfo(that.wifiManager, that.connectivityManager, network);
255
+ if (ConnectivityManagerImpl.isPreviousOrStableNetwork(that.wifiManager, that.connectivityManager, network, that.previousConnectionMetered, that.previousConnectionWiFi, that.previousSsid)) {
256
+ that.connectivityManager.bindProcessToNetwork(network);
257
+ that.disconnectResolve(true);
258
+ }
259
+ },
260
+ onLost: function (network) {
261
+ console.log("Disconnected.");
262
+ },
263
+ }))();
264
+ this.connectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(), networkConnectivity);
265
+ this.safeUnregisterNetworkCallback(this.forcedNetworkCallback);
266
+ }
267
+ else {
268
+ this.wifiManager.disableNetwork(this.wifiManager.getConnectionInfo().getNetworkId());
269
+ complete(true);
270
+ }
271
+ });
272
+ }
273
+ static isPreviousOrStableNetwork(wifiManager, connectivityManager, network, previousNetworkMetered, previousNetworkWiFi, previousNetworkSsid) {
274
+ const networkCapabilities = ConnectivityManagerImpl.getCapabilities(connectivityManager, network);
275
+ let isWifi = networkCapabilities
276
+ ? networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
277
+ : false;
278
+ if (previousNetworkWiFi && isWifi) {
279
+ let ssid = wifiManager.getConnectionInfo().getSSID();
280
+ return ssid == previousNetworkSsid;
281
+ }
282
+ else if (previousNetworkWiFi) {
283
+ return false;
284
+ }
285
+ if (!networkCapabilities) {
286
+ return false;
287
+ }
288
+ let meteredNow = !networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
289
+ let hasInternet = ConnectivityManagerImpl.hasInternet(connectivityManager, network);
290
+ return meteredNow == previousNetworkMetered && hasInternet;
291
+ }
292
+ static logConnectivityInfo(wifiManager, connectivityManager, network) {
293
+ let activeNetwork = connectivityManager.getActiveNetwork();
294
+ let defaultActive = connectivityManager.isDefaultNetworkActive();
295
+ let boundNetwork = connectivityManager.getBoundNetworkForProcess();
296
+ let allNetworks = connectivityManager.getAllNetworks();
297
+ let ssid = wifiManager.getConnectionInfo().getSSID();
298
+ console.log("Connected via " +
299
+ ConnectivityManagerImpl.getInterfaceName(connectivityManager, network));
300
+ console.log("New network: " + network);
301
+ console.log("Active network: " + activeNetwork);
302
+ console.log("Active network adapter: " +
303
+ ConnectivityManagerImpl.getInterfaceName(connectivityManager, activeNetwork));
304
+ console.log("Default is active?: " + defaultActive);
305
+ console.log("Current bound network: " + boundNetwork);
306
+ console.log("Current bound network adapter: " +
307
+ ConnectivityManagerImpl.getInterfaceName(connectivityManager, boundNetwork));
308
+ console.log("All networks: " + allNetworks);
309
+ console.log("Current SSID:" + ssid);
310
+ }
311
+ static getInterfaceName(connectivityManager, activeNetwork) {
312
+ let linkProperties = connectivityManager.getLinkProperties(activeNetwork);
313
+ if (linkProperties == null) {
314
+ return "";
315
+ }
316
+ return linkProperties.getInterfaceName();
317
+ }
318
+ async waitUntilConnectedToWifi(milliseconds) {
319
+ return new Promise((resolve) => {
320
+ let intervalTimer = setInterval(() => {
321
+ if (this.isWifiConnected()) {
322
+ clearInterval(intervalTimer);
323
+ clearTimeout(timeout);
324
+ resolve(true);
325
+ }
326
+ }, 500);
327
+ let timeout = setTimeout(() => {
328
+ clearInterval(intervalTimer);
329
+ this.disconnectWifiAndRemoveNetwork();
330
+ throw new Error("Could not connect in the allowed time.");
331
+ }, milliseconds);
332
+ });
333
+ }
334
+ disconnectWifiAndRemoveNetwork() {
335
+ this.wifiManager.removeNetwork(this.getWifiNetworkId());
336
+ this.wifiManager.disconnect();
337
+ }
338
+ }
339
+ //# sourceMappingURL=connectivity-manager-impl.android.js.map
@@ -0,0 +1,4 @@
1
+ import { Observable } from "@nativescript/core/data/observable";
2
+ export declare class Common extends Observable {
3
+ constructor();
4
+ }
@@ -0,0 +1,7 @@
1
+ import { Observable } from "@nativescript/core/data/observable";
2
+ export class Common extends Observable {
3
+ constructor() {
4
+ super();
5
+ }
6
+ }
7
+ //# sourceMappingURL=connectivity-manager-impl.common.js.map