-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.component.ts
executable file
·145 lines (127 loc) · 4.29 KB
/
app.component.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
// angular
import { Component, OnInit, OnDestroy } from "@angular/core";
import { Observable as RxObservable, Subscription, BehaviorSubject } from "rxjs/Rx";
import "rxjs/add/operator/map";
import 'rxjs/add/operator/do';
import {NgZone} from "@angular/core";
// app
import {MicrosoftBandService} from '@xmlking/nativescript-ngx-microsoftband';
@Component({
selector: 'app',
templateUrl: "app.component.html"
})
export class AppComponent implements OnInit, OnDestroy {
private tileId: NSUUID = NSUUID.alloc().initWithUUIDString("be2066df-306f-438e-860c-f82a8bc0bd6a");
public connected: boolean = false;
private conSub: Subscription;
public message: string;
public errorMsg: string;
public name : string;
public firmwareVersion : string;
public hardwareVersion : string;
public isBluetoothOn : boolean;
public isDeviceConnected : boolean;
public deviceIdentifier : NSUUID;
public userConsent : UserConsent;
constructor(private zone: NgZone, private msband: MicrosoftBandService) {
}
ngOnInit() {
this.connect()
};
ngOnDestroy() {
this.disconnect();
}
toggle() {
if(this.connected) {
this.disconnect()
} else {
this.connect()
}
}
addTile() {
if(! this.connected) {
this.errorMsg = "Band not connected"
} else {
this.msband.addTile( this.tileId, "Wearable Hub", "O", "Oo")
.then( () => {
this.message = "tile added"
})
.catch((error: NSError) => {
this.errorMsg = error.localizedDescription
})
}
}
sendNotification() {
if(! this.connected) {
this.errorMsg = "Band not connected"
} else {
this.msband.sendBandNotification( this.tileId, "sumo", "good work")
.then( () => {
this.message = "Notification Sent"
})
.catch((error: NSError) => {
this.errorMsg = error.localizedDescription
})
}
}
sendHaptic() {
if(! this.connected) {
this.errorMsg = "Band not connected"
} else {
this.msband.sendHaptic()
.then( () => {
this.message = "Haptic Sent"
})
.catch((error: NSError) => {
this.errorMsg = error.localizedDescription
})
}
}
private connect() {
this.conSub = this.msband.connection$.subscribe(
(status) => {
console.log('Next: ', status);
if(status === ConnectionStatus.Connected) {
console.log("App: Connected");
this.onConnect();
} else {
this.onDisconnect();
}
},
(err : NSError) => {
if(err.code === ConnectionError.BluetoothUnavailable)
console.log('App Error: BluetoothUnavailable',err.domain);
else if(err.code === ConnectionError.DeviceUnavailable)
console.log('App Error: DeviceUnavailable',err.domain);
else
console.log('App Error: unknown: ', err.code, err.domain, err.localizedDescription);
},
() => {
console.log('App: Completed');
});
}
private disconnect() {
if(this.conSub) {
this.conSub.unsubscribe();
}
this.connected = false
}
private onConnect() {
this.zone.run(() => {
this.connected = true;
this.name = this.msband.name;
this.firmwareVersion = this.msband.firmwareVersion;
this.hardwareVersion = this.msband.hardwareVersion;
this.isBluetoothOn = this.msband.isBluetoothOn;
this.isDeviceConnected = this.msband.isDeviceConnected;
this.deviceIdentifier = this.msband.deviceIdentifier;
this.userConsent = this.msband.userConsent;
});
}
private onDisconnect() {
console.log(".....Disconnected....");
this.zone.run(() => {
this.connected = false;
});
}
}