@@ -4,7 +4,7 @@ a code-behind file. The code-behind is a great place to place your view
4
4
logic, and to set up your page’s data binding.
5
5
*/
6
6
7
- import { NavigatedData , Page , Application } from "@nativescript/core" ;
7
+ import { Application , NavigatedData , Page } from "@nativescript/core" ;
8
8
9
9
import { HomeViewModel } from "./home-view-model" ;
10
10
@@ -13,47 +13,76 @@ import { Resolution } from "nativescript-context-apis/activity-recognition";
13
13
14
14
import { GeolocationProvider } from "nativescript-context-apis/geolocation" ;
15
15
import { of , Subscription } from "rxjs" ;
16
+ import {
17
+ FingerprintGrouping ,
18
+ WifiScanProvider ,
19
+ } from "nativescript-context-apis/internal/wifi" ;
16
20
17
21
const activityRecognizers = [ Resolution . LOW , Resolution . MEDIUM ] ;
18
22
23
+ let locationSubscription : Subscription ;
24
+ let wifiScanSubscription : Subscription ;
19
25
export function onNavigatingTo ( args : NavigatedData ) {
20
26
const page = < Page > args . object ;
21
27
22
28
page . bindingContext = new HomeViewModel ( ) ;
23
29
24
30
let preparing = true ;
25
- let locationSubscription : Subscription ;
26
31
Application . on ( Application . resumeEvent , ( ) => {
27
32
if ( ! preparing ) {
28
- printCurrentLocation ( ) . catch ( ( err ) => {
29
- console . error ( `Could not print current location: ${ err } ` ) ;
30
- } ) . then ( ( ) => printLocationUpdates ( )
31
- . then ( ( subscription ) => ( locationSubscription = subscription ) )
32
- . catch (
33
- ( err ) =>
34
- `An error occurred while getting location updates: ${ err } ` )
35
- ) . then ( ( ) => listenToActivityChanges ( ) ) ;
33
+ showUpdates ( ) . catch ( ( err ) =>
34
+ console . error ( "Could not show updates. Reason: " , err )
35
+ ) ;
36
36
}
37
37
} ) ;
38
38
39
39
Application . on ( Application . suspendEvent , ( ) => {
40
40
if ( ! preparing ) {
41
- if ( locationSubscription ) {
42
- locationSubscription . unsubscribe ( ) ;
43
- }
41
+ locationSubscription ?. unsubscribe ( ) ;
42
+ wifiScanSubscription ?. unsubscribe ( ) ;
44
43
stopListeningToChanges ( ) ;
45
44
}
46
45
} ) ;
47
46
48
- printCurrentLocation ( ) . catch ( ( err ) => {
49
- console . error ( `Could not print current location: ${ err } ` ) ;
50
- } ) . then ( ( ) => printLocationUpdates ( )
51
- . then ( ( subscription ) => ( locationSubscription = subscription ) )
52
- . catch (
53
- ( err ) => `An error occurred while getting location updates: ${ err } `
54
- )
55
- ) . then ( ( ) => listenToActivityChanges ( true ) )
56
- . then ( ( ) => preparing = false ) ;
47
+ showUpdates ( ) . then ( ( ) => ( preparing = false ) ) ;
48
+ }
49
+
50
+ async function showUpdates ( addListeners = false ) : Promise < void > {
51
+ const steps : Array < ( ) => Promise < any > > = [
52
+ ( ) => listenToActivityChanges ( addListeners ) ,
53
+ ( ) =>
54
+ printCurrentLocation ( ) . catch ( ( err ) => {
55
+ console . error ( "Could not print current location. Reason:" , err ) ;
56
+ } ) ,
57
+ ( ) =>
58
+ printWifiScanResult ( ) . catch ( ( err ) => {
59
+ console . error (
60
+ "Could not print current nearby wifi scan. Reason:" ,
61
+ err
62
+ ) ;
63
+ } ) ,
64
+ ( ) =>
65
+ printLocationUpdates ( )
66
+ . then ( ( subscription ) => ( locationSubscription = subscription ) )
67
+ . catch ( ( err ) =>
68
+ console . error (
69
+ "An error occurred while getting location updates. Reason:" ,
70
+ err
71
+ )
72
+ ) ,
73
+ ( ) =>
74
+ printWifiScanUpdates ( )
75
+ . then ( ( subscription ) => ( wifiScanSubscription = subscription ) )
76
+ . catch ( ( err ) =>
77
+ console . error (
78
+ "An error occurred while getting wifi scan updates. Reason:" ,
79
+ err
80
+ )
81
+ ) ,
82
+ ] ;
83
+ for ( const step of steps ) {
84
+ await step ( ) ;
85
+ }
57
86
}
58
87
59
88
async function printCurrentLocation ( ) {
@@ -68,6 +97,15 @@ async function printCurrentLocation() {
68
97
}
69
98
}
70
99
100
+ async function printWifiScanResult ( ) {
101
+ const provider = contextApis . wifiScanProvider ;
102
+ const ok = await prepareWifiScanProvider ( provider ) ;
103
+ if ( ok ) {
104
+ const fingerprint = await provider . acquireWifiFingerprint ( true ) ;
105
+ console . log ( `Last wifi scan result: ${ JSON . stringify ( fingerprint ) } ` ) ;
106
+ }
107
+ }
108
+
71
109
async function printLocationUpdates ( ) : Promise < Subscription > {
72
110
const provider = contextApis . geolocationProvider ;
73
111
const ok = await prepareGeolocationProvider ( provider ) ;
@@ -86,7 +124,31 @@ async function printLocationUpdates(): Promise<Subscription> {
86
124
next : ( location ) =>
87
125
console . log ( `New location acquired!: ${ JSON . stringify ( location ) } ` ) ,
88
126
error : ( error ) =>
89
- console . error ( `Location updates could not be acquired: ${ error } ` )
127
+ console . error ( `Location updates could not be acquired: ${ error } ` ) ,
128
+ } ) ;
129
+ }
130
+
131
+ async function printWifiScanUpdates ( ) : Promise < Subscription > {
132
+ const provider = contextApis . wifiScanProvider ;
133
+ const ok = await prepareWifiScanProvider ( provider ) ;
134
+
135
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 30000 ) ) ;
136
+
137
+ const stream = ok
138
+ ? provider . wifiFingerprintStream ( {
139
+ ensureAlwaysNew : true ,
140
+ grouping : FingerprintGrouping . NONE ,
141
+ continueOnFailure : true ,
142
+ } )
143
+ : of ( null ) ;
144
+
145
+ return stream . subscribe ( {
146
+ next : ( fingerprint ) =>
147
+ console . log (
148
+ `New wifi scan result!: ${ JSON . stringify ( fingerprint ) } `
149
+ ) ,
150
+ error : ( error ) =>
151
+ console . error ( `Wifi scan result could not be acquired: ${ error } ` ) ,
90
152
} ) ;
91
153
}
92
154
@@ -96,7 +158,9 @@ export async function listenToActivityChanges(addListener = false) {
96
158
await listenToActivityChangesFor ( recognizerType , addListener ) ;
97
159
} catch ( err ) {
98
160
console . error (
99
- `An error occurred while listening to ${ recognizerType } res activity changes: ${ JSON . stringify ( err ) } `
161
+ `An error occurred while listening to ${ recognizerType } res activity changes: ${ JSON . stringify (
162
+ err
163
+ ) } `
100
164
) ;
101
165
}
102
166
}
@@ -144,7 +208,7 @@ async function listenToActivityChangesFor(
144
208
) ;
145
209
}
146
210
147
- let _preparing : Promise < any > ;
211
+ let _preparingGeoProv : Promise < any > ;
148
212
async function prepareGeolocationProvider (
149
213
provider : GeolocationProvider
150
214
) : Promise < boolean > {
@@ -154,15 +218,42 @@ async function prepareGeolocationProvider(
154
218
}
155
219
156
220
try {
157
- if ( ! _preparing ) {
158
- _preparing = provider . prepare ( ) ;
221
+ if ( ! _preparingGeoProv ) {
222
+ _preparingGeoProv = provider . prepare ( ) ;
159
223
}
160
- await _preparing ;
224
+ await _preparingGeoProv ;
161
225
return true ;
162
226
} catch ( e ) {
163
- console . error ( `GeolocationProvider couldn't be prepared: ${ JSON . stringify ( e ) } ` ) ;
227
+ console . error (
228
+ `GeolocationProvider couldn't be prepared: ${ JSON . stringify ( e ) } `
229
+ ) ;
230
+ return false ;
231
+ } finally {
232
+ _preparingGeoProv = null ;
233
+ }
234
+ }
235
+
236
+ let _preparingWifiProv : Promise < any > ;
237
+ async function prepareWifiScanProvider (
238
+ provider : WifiScanProvider
239
+ ) : Promise < boolean > {
240
+ const isReady = await provider . isReady ( ) ;
241
+ if ( isReady ) {
242
+ return true ;
243
+ }
244
+
245
+ try {
246
+ if ( ! _preparingWifiProv ) {
247
+ _preparingWifiProv = provider . prepare ( ) ;
248
+ }
249
+ await _preparingWifiProv ;
250
+ return true ;
251
+ } catch ( e ) {
252
+ console . error (
253
+ `WifiScanProvider couldn't be prepared: ${ JSON . stringify ( e ) } `
254
+ ) ;
164
255
return false ;
165
256
} finally {
166
- _preparing = null ;
257
+ _preparingWifiProv = null ;
167
258
}
168
259
}
0 commit comments