@@ -16,12 +16,18 @@ import { of, Subscription } from "rxjs";
16
16
import {
17
17
FingerprintGrouping ,
18
18
WifiScanProvider ,
19
- } from "nativescript-context-apis/internal/wifi" ;
19
+ } from "nativescript-context-apis/wifi" ;
20
+ import { BleScanProvider , BleScanMode } from "nativescript-context-apis/ble" ;
21
+
22
+ const I_BEACON_UUIDS = [
23
+ // Place your iBeacon UUIDs here to just report beacon updates!
24
+ ] ;
20
25
21
26
const activityRecognizers = [ Resolution . LOW , Resolution . MEDIUM ] ;
22
27
23
28
let locationSubscription : Subscription ;
24
29
let wifiScanSubscription : Subscription ;
30
+ let bleScanSubscription : Subscription ;
25
31
export function onNavigatingTo ( args : NavigatedData ) {
26
32
const page = < Page > args . object ;
27
33
@@ -40,6 +46,7 @@ export function onNavigatingTo(args: NavigatedData) {
40
46
if ( ! preparing ) {
41
47
locationSubscription ?. unsubscribe ( ) ;
42
48
wifiScanSubscription ?. unsubscribe ( ) ;
49
+ bleScanSubscription ?. unsubscribe ( ) ;
43
50
stopListeningToChanges ( ) ;
44
51
}
45
52
} ) ;
@@ -54,10 +61,17 @@ async function showUpdates(addListeners = false): Promise<void> {
54
61
printCurrentLocation ( ) . catch ( ( err ) => {
55
62
console . error ( "Could not print current location. Reason:" , err ) ;
56
63
} ) ,
64
+ ( ) =>
65
+ printBleScanResult ( ) . catch ( ( err ) => {
66
+ console . error (
67
+ "Could not print current nearby BLE devices. Reason:" ,
68
+ err
69
+ ) ;
70
+ } ) ,
57
71
( ) =>
58
72
printWifiScanResult ( ) . catch ( ( err ) => {
59
73
console . error (
60
- "Could not print current nearby wifi scan . Reason:" ,
74
+ "Could not print current nearby wifi APs . Reason:" ,
61
75
err
62
76
) ;
63
77
} ) ,
@@ -70,6 +84,15 @@ async function showUpdates(addListeners = false): Promise<void> {
70
84
err
71
85
)
72
86
) ,
87
+ ( ) =>
88
+ printBleScanUpdates ( )
89
+ . then ( ( subscription ) => ( bleScanSubscription = subscription ) )
90
+ . catch ( ( err ) =>
91
+ console . error (
92
+ "An error occurred while getting ble scan updates. Reason:" ,
93
+ err
94
+ )
95
+ ) ,
73
96
( ) =>
74
97
printWifiScanUpdates ( )
75
98
. then ( ( subscription ) => ( wifiScanSubscription = subscription ) )
@@ -106,6 +129,19 @@ async function printWifiScanResult() {
106
129
}
107
130
}
108
131
132
+ async function printBleScanResult ( ) {
133
+ const provider = contextApis . bleScanProvider ;
134
+ const ok = await prepareBleScanProvider ( provider ) ;
135
+ if ( ok ) {
136
+ const bleScanResult = await provider . acquireBleScan ( {
137
+ scanTime : 5000 ,
138
+ scanMode : BleScanMode . BALANCED ,
139
+ iBeaconUuids : I_BEACON_UUIDS ,
140
+ } ) ;
141
+ console . log ( `Last ble scan result: ${ JSON . stringify ( bleScanResult ) } ` ) ;
142
+ }
143
+ }
144
+
109
145
async function printLocationUpdates ( ) : Promise < Subscription > {
110
146
const provider = contextApis . geolocationProvider ;
111
147
const ok = await prepareGeolocationProvider ( provider ) ;
@@ -152,6 +188,28 @@ async function printWifiScanUpdates(): Promise<Subscription> {
152
188
} ) ;
153
189
}
154
190
191
+ async function printBleScanUpdates ( ) : Promise < Subscription > {
192
+ const provider = contextApis . bleScanProvider ;
193
+ const ok = await prepareBleScanProvider ( provider ) ;
194
+
195
+ const stream = ok
196
+ ? provider . bleScanStream ( {
197
+ reportInterval : 2000 ,
198
+ scanMode : BleScanMode . LOW_LATENCY ,
199
+ iBeaconUuids : I_BEACON_UUIDS ,
200
+ } )
201
+ : of ( null ) ;
202
+
203
+ return stream . subscribe ( {
204
+ next : ( bleScanResult ) =>
205
+ console . log (
206
+ `New ble scan result!: ${ JSON . stringify ( bleScanResult ) } `
207
+ ) ,
208
+ error : ( error ) =>
209
+ console . error ( `Ble scan result could not be acquired: ${ error } ` ) ,
210
+ } ) ;
211
+ }
212
+
155
213
export async function listenToActivityChanges ( addListener = false ) {
156
214
for ( const recognizerType of activityRecognizers ) {
157
215
try {
@@ -257,3 +315,28 @@ async function prepareWifiScanProvider(
257
315
_preparingWifiProv = null ;
258
316
}
259
317
}
318
+
319
+ let _preparingBleProv : Promise < any > ;
320
+ async function prepareBleScanProvider (
321
+ provider : BleScanProvider
322
+ ) : Promise < boolean > {
323
+ const isReady = await provider . isReady ( ) ;
324
+ if ( isReady ) {
325
+ return true ;
326
+ }
327
+
328
+ try {
329
+ if ( ! _preparingBleProv ) {
330
+ _preparingBleProv = provider . prepare ( ) ;
331
+ }
332
+ await _preparingBleProv ;
333
+ return true ;
334
+ } catch ( e ) {
335
+ console . error (
336
+ `BleScanProvider couldn't be prepared: ${ JSON . stringify ( e ) } `
337
+ ) ;
338
+ return false ;
339
+ } finally {
340
+ _preparingBleProv = null ;
341
+ }
342
+ }
0 commit comments