1- import { AdvertismentData , getBluetoothInstance , Peripheral , Service } from '@nativescript-community/ble' ;
2- import { GPS , Options as GeolocationOptions } from 'nativescript-gps' ;
3- import { ApplicationEventData , on as applicationOn , resumeEvent , suspendEvent } from '@nativescript/core/application' ;
1+ import { AdvertismentData , Peripheral , Service , getBluetoothInstance } from '@nativescript-community/ble' ;
42import { Observable } from '@nativescript/core/data/observable' ;
53import { ObservableArray } from '@nativescript/core/data/observable-array' ;
64import * as dialogs from '@nativescript/core/ui/dialogs' ;
7- import { Accuracy } from '@nativescript/core/ui/enums/enums' ;
8- import { topmost } from '@nativescript/core/ui/frame' ;
5+ import { Frame } from '@nativescript/core/ui/frame' ;
96import { Prop } from './utils/obs-prop' ;
107
118
12- const geolocation = new GPS ( ) ;
139export class TestViewModel extends Observable {
1410 @Prop ( ) public discoveredServices = new ObservableArray < Service > ( ) ;
1511 @Prop ( ) public isLoading = false ;
@@ -22,11 +18,9 @@ export class TestViewModel extends Observable {
2218 _isIOSBackgroundMode = false ;
2319 watchId ;
2420 currentWatcher : Function ;
25- _deferringUpdates = false
2621
2722 constructor ( navContext ) {
2823 super ( ) ;
29- this . _bluetooth . debug = false ;
3024 this . peripheral = navContext . peripheral ;
3125 this . advertismentData = navContext . peripheral . advertismentData as AdvertismentData ;
3226 console . log ( 'peripheral' , JSON . stringify ( this . peripheral ) ) ;
@@ -35,8 +29,6 @@ export class TestViewModel extends Observable {
3529 console . log ( 'uuids' , this . advertismentData . serviceUUIDs ) ;
3630 console . log ( 'txPowerLevel' , this . advertismentData . txPowerLevel ) ;
3731
38- applicationOn ( suspendEvent , this . onAppPause , this ) ;
39- applicationOn ( resumeEvent , this . onAppResume , this ) ;
4032 // console.log('localName', this.advertismentData.localName);
4133 // console.log('serviceUUIDs', this.advertismentData.serviceUUIDs);
4234 // console.log('txPowerLevel', this.advertismentData.txPowerLevel);
@@ -45,76 +37,6 @@ export class TestViewModel extends Observable {
4537 // console.log('manufacturerData', this.advertismentData.manufacturerData);
4638 // console.log('serviceData', this.advertismentData.serviceData);
4739 }
48- onAppResume ( args : ApplicationEventData ) {
49- if ( args . ios ) {
50- this . _isIOSBackgroundMode = false ;
51- // For iOS applications, args.ios is UIApplication.
52- console . log ( 'UIApplication: resumeEvent' , this . isWatching ( ) ) ;
53- if ( this . isWatching ( ) ) {
54- const watcher = this . currentWatcher ;
55- this . stopWatch ( ) ;
56- this . startWatch ( watcher ) ;
57- }
58- }
59- }
60- onAppPause ( args : ApplicationEventData ) {
61- if ( args . ios ) {
62- this . _isIOSBackgroundMode = true ;
63- // For iOS applications, args.ios is UIApplication.
64- console . log ( 'UIApplication: suspendEvent' , this . isWatching ( ) ) ;
65- if ( this . isWatching ( ) ) {
66- const watcher = this . currentWatcher ;
67- this . stopWatch ( ) ;
68- this . startWatch ( watcher ) ;
69- }
70- }
71- }
72- onDeferred ( ) {
73- this . _deferringUpdates = false ;
74- }
75- onLocation ( loc , manager ?: any ) {
76-
77- if ( manager && this . _isIOSBackgroundMode && ! this . _deferringUpdates ) {
78- this . _deferringUpdates = true ;
79- manager . allowDeferredLocationUpdatesUntilTraveledTimeout ( 0 , 10 ) ;
80- }
81- }
82- startWatch ( onLoc ?: Function ) {
83- this . currentWatcher = onLoc ;
84- const options : GeolocationOptions = { desiredAccuracy : Accuracy . high , minimumUpdateTime : 1000 , onDeferred :this . onDeferred . bind ( this ) } ;
85-
86- // if (!isAndroid) {
87- // if (this._isIOSBackgroundMode) {
88- // options.pausesLocationUpdatesAutomatically = false;
89- // options.allowsBackgroundLocationUpdates = true;
90- // options.activityType = CLActivityType.Fitness;
91- // } else {
92- options . pausesLocationUpdatesAutomatically = false ;
93- options . allowsBackgroundLocationUpdates = true ;
94- options . activityType = 3 ;
95- // }
96- // } else {
97- // if (!gVars.isWatch) {
98- // options.provider = 'gps';
99- // }
100- // }
101- console . log ( 'startWatch' , options ) ;
102-
103- return geolocation . watchLocation ( this . onLocation . bind ( this ) , ( ) => { } , options ) . then ( id => ( this . watchId = id ) ) ;
104- }
105-
106- stopWatch ( ) {
107- console . log ( 'stopWatch' , this . watchId ) ;
108- if ( this . watchId ) {
109- geolocation . clearWatch ( this . watchId ) ;
110- this . watchId = null ;
111- this . currentWatcher = null ;
112- }
113- }
114-
115- isWatching ( ) {
116- return ! ! this . watchId ;
117- }
11840 formatData ( title : string , advKey : string ) {
11941 return title + ': ' + JSON . stringify ( this . advertismentData [ advKey ] ) ;
12042 }
@@ -160,7 +82,6 @@ export class TestViewModel extends Observable {
16082 onConnected : peripheral => {
16183 this . connected = true ;
16284 console . log ( '------- Peripheral connected: ' + JSON . stringify ( peripheral ) ) ;
163- this . startWatch ( ) ;
16485 peripheral . services . forEach ( value => {
16586 console . log ( '---- ###### adding service: ' + value . UUID ) ;
16687 this . discoveredServices . push ( value ) ;
@@ -197,7 +118,7 @@ export class TestViewModel extends Observable {
197118 animated : true
198119 } ;
199120
200- topmost ( ) . navigate ( navigationEntry ) ;
121+ Frame . topmost ( ) . navigate ( navigationEntry ) ;
201122 }
202123
203124 public onStartTap ( args ) {
0 commit comments