-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
test-page-model.ts
236 lines (220 loc) · 8.87 KB
/
test-page-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { AdvertismentData, getBluetoothInstance, Peripheral, Service } from 'nativescript-bluetooth';
import { GPS, Options as GeolocationOptions } from 'nativescript-gps';
import { ApplicationEventData, on as applicationOn, resumeEvent, suspendEvent } from '@nativescript/core/application';
import { Observable } from '@nativescript/core/data/observable';
import { ObservableArray } from '@nativescript/core/data/observable-array';
import * as dialogs from '@nativescript/core/ui/dialogs';
import { Accuracy } from '@nativescript/core/ui/enums/enums';
import { topmost } from '@nativescript/core/ui/frame';
import { Prop } from './utils/obs-prop';
const geolocation = new GPS();
export class TestViewModel extends Observable {
@Prop() public discoveredServices = new ObservableArray<Service>();
@Prop() public isLoading = false;
@Prop() public connected = false;
@Prop() public peripheral: Peripheral;
@Prop() public advertismentData: AdvertismentData;
private _bluetooth = getBluetoothInstance();
_isIOSBackgroundMode = false;
watchId;
currentWatcher: Function;
_deferringUpdates =false
constructor(navContext) {
super();
this._bluetooth.debug = false;
this.peripheral = navContext.peripheral;
this.advertismentData = navContext.peripheral.advertismentData as AdvertismentData;
console.log('peripheral', JSON.stringify(this.peripheral));
console.log('advertismentData', JSON.stringify(this.advertismentData));
console.log('serviceData', JSON.stringify(this.advertismentData.serviceData));
console.log('uuids', this.advertismentData.serviceUUIDs);
console.log('txPowerLevel', this.advertismentData.txPowerLevel);
applicationOn(suspendEvent, this.onAppPause, this);
applicationOn(resumeEvent, this.onAppResume, this);
// console.log('localName', this.advertismentData.localName);
// console.log('serviceUUIDs', this.advertismentData.serviceUUIDs);
// console.log('txPowerLevel', this.advertismentData.txPowerLevel);
// console.log('flags', this.advertismentData.flags);
// console.log('manufacturerId', this.advertismentData.manufacturerId);
// console.log('manufacturerData', this.advertismentData.manufacturerData);
// console.log('serviceData', this.advertismentData.serviceData);
}
onAppResume(args: ApplicationEventData) {
if (args.ios) {
this._isIOSBackgroundMode = false;
// For iOS applications, args.ios is UIApplication.
console.log('UIApplication: resumeEvent', this.isWatching());
if (this.isWatching()) {
const watcher = this.currentWatcher;
this.stopWatch();
this.startWatch(watcher);
}
}
}
onAppPause(args: ApplicationEventData) {
if (args.ios) {
this._isIOSBackgroundMode = true;
// For iOS applications, args.ios is UIApplication.
console.log('UIApplication: suspendEvent', this.isWatching());
if (this.isWatching()) {
const watcher = this.currentWatcher;
this.stopWatch();
this.startWatch(watcher);
}
}
}
onDeferred() {
this._deferringUpdates = false;
}
onLocation(loc, manager?: any) {
if (manager && this._isIOSBackgroundMode && !this._deferringUpdates) {
this._deferringUpdates = true;
manager.allowDeferredLocationUpdatesUntilTraveledTimeout(0, 10);
}
}
startWatch(onLoc?: Function) {
this.currentWatcher = onLoc;
const options: GeolocationOptions = { desiredAccuracy: Accuracy.high, minimumUpdateTime: 1000, onDeferred:this.onDeferred.bind(this) };
// if (!isAndroid) {
// if (this._isIOSBackgroundMode) {
// options.pausesLocationUpdatesAutomatically = false;
// options.allowsBackgroundLocationUpdates = true;
// options.activityType = CLActivityType.Fitness;
// } else {
options.pausesLocationUpdatesAutomatically = false;
options.allowsBackgroundLocationUpdates = true;
options.activityType = 3;
// }
// } else {
// if (!gVars.isWatch) {
// options.provider = 'gps';
// }
// }
console.log('startWatch', options);
return geolocation.watchLocation(this.onLocation.bind(this), () => {}, options).then(id => (this.watchId = id));
}
stopWatch() {
console.log('stopWatch', this.watchId);
if (this.watchId) {
geolocation.clearWatch(this.watchId);
this.watchId = null;
this.currentWatcher = null;
}
}
isWatching() {
return !!this.watchId;
}
formatData(title: string, advKey: string) {
return title + ': ' + JSON.stringify(this.advertismentData[advKey]);
}
sendTimer;
startSendTimer() {
if (!this.sendTimer) {
this.sendTimer = setInterval(() => {
this._bluetooth
.write({
peripheralUUID: this.peripheral.UUID,
serviceUUID: 'ec00',
characteristicUUID: 'ec0e',
value: `echo ${Date.now()}`
})
.then(
result => {
// console.log('feedback', 'value written');
// console.log('feedbackTimestamp', new Date().toLocaleString());
},
errorMsg => {
// console.log('feedback', errorMsg);
// console.log('feedbackTimestamp', new Date().toLocaleString());
this.stopSendTimer();
}
);
}, 500);
}
}
stopSendTimer() {
if (this.sendTimer) {
clearInterval(this.sendTimer);
this.sendTimer = null;
}
}
public start() {
console.log('connecting to peripheral', this.peripheral.UUID);
this.isLoading = true;
this._bluetooth
.connect({
UUID: this.peripheral.UUID,
// NOTE: we could just use the promise as this cb is only invoked once
onConnected: peripheral => {
this.connected = true;
console.log('------- Peripheral connected: ' + JSON.stringify(peripheral));
this.startWatch();
peripheral.services.forEach(value => {
console.log('---- ###### adding service: ' + value.UUID);
this.discoveredServices.push(value);
});
this.isLoading = false;
this.startSendTimer();
},
onDisconnected: peripheral => {
this.connected = false;
this.stopSendTimer();
dialogs.alert({
title: 'Disconnected',
message: 'Disconnected from peripheral: ' + JSON.stringify(peripheral),
okButtonText: 'Okay'
});
}
})
.catch(err => {
this.connected = false;
console.log('error connecting to peripheral', err);
});
}
public onServiceTap(args) {
const service = this.discoveredServices.getItem(args.index);
console.log('--- service selected: ' + service.UUID);
const navigationEntry = {
moduleName: 'characteristics-page',
context: {
peripheral: this.peripheral,
service
},
animated: true
};
topmost().navigate(navigationEntry);
}
public onStartTap(args) {
if (!this.connected) {
this.start();
return;
}
console.log('Disconnecting peripheral ' + this.peripheral.UUID);
this._bluetooth.disconnect({
UUID: this.peripheral.UUID
});
// .then(
// () => {
// // going back to previous page
// topmost().navigate({
// moduleName: 'main-page',
// animated: true,
// transition: {
// name: 'slideRight'
// }
// });
// },
// err => {
// console.log(err);
// // still going back to previous page
// topmost().navigate({
// moduleName: 'main-page',
// animated: true,
// transition: {
// name: 'slideRight'
// }
// });
// }
// );
}
}