-
Notifications
You must be signed in to change notification settings - Fork 3
/
ble-devices.service.ts
146 lines (140 loc) · 4.74 KB
/
ble-devices.service.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
import { Injectable } from '@angular/core';
import { BLE } from '@ionic-native/ble/ngx';
import { Diagnostic } from '@ionic-native/diagnostic/ngx';
import { Platform } from '@ionic/angular';
import { TranslateService } from '@ngx-translate/core';
import { Actions, ofActionDispatched, ofActionSuccessful, Store } from '@ngxs/store';
import { merge, Observable, timer } from 'rxjs';
import { fromPromise } from 'rxjs/internal-compatibility';
import { buffer, map, scan, skip, switchMap, takeUntil, tap, throttleTime } from 'rxjs/operators';
import { AlertService } from '../../../services/alert.service';
import { AbstractDevice, DeviceType } from '../abstract-device';
import {
BLEConnectionLost,
BLEDevicesDiscovered,
StartDiscoverBLEDevices,
StopDiscoverDevices
} from '../devices.action';
import { DevicesService } from '../devices.service';
import { AbstractBLEDevice, RawBLEDevice } from './abstract-ble-device';
@Injectable({
providedIn: 'root'
})
export class BLEDevicesService {
private devices = [DeviceType.OGKit, DeviceType.AtomTag, DeviceType.SafeCast];
private currentAlert?: any;
private scanDuration = 3;
private scanPeriod = 5000;
constructor(
private ble: BLE,
private platform: Platform,
private actions$: Actions,
private store: Store,
private diagnostic: Diagnostic,
private alertService: AlertService,
private translateService: TranslateService,
private devicesService: DevicesService
) {
this.actions$.pipe(ofActionDispatched(StartDiscoverBLEDevices)).subscribe(() => {
if (this.currentAlert) {
this.currentAlert.dismiss();
this.currentAlert = undefined;
}
});
}
startDiscoverDevices(): Observable<any> {
return fromPromise(
this.ble
.isEnabled()
.catch(err => {
if (this.platform.is('android')) {
return this.ble.enable();
} else {
throw err;
}
})
.catch(err => {
this.onBLEError();
throw err;
})
).pipe(tap(() => this.discoverDevices()));
}
private discoverDevices() {
this.diagnostic.registerBluetoothStateChangeHandler((state: string) => {
switch (state) {
case this.diagnostic.bluetoothState.POWERED_OFF:
this.onBLEError();
this.store.dispatch(new BLEConnectionLost());
break;
}
});
const time = timer(this.scanPeriod, this.scanPeriod).pipe(
takeUntil(this.actions$.pipe(ofActionSuccessful(StopDiscoverDevices, BLEConnectionLost)))
);
merge(
this.ble.scan([], this.scanDuration).pipe(
scan<RawBLEDevice>((devices, newDevice) => [...devices, newDevice], []),
throttleTime(100)
),
time.pipe(
switchMap(() => this.ble.scan([], this.scanDuration)),
buffer(time),
skip(1)
)
)
.pipe(
map((rawDevices: RawBLEDevice[]) => {
const res: RawBLEDevice[] = [];
rawDevices.forEach(rawDevice => {
if (!res.find(rawDevice2 => rawDevice2.id === rawDevice.id)) {
res.push(rawDevice);
}
});
return res;
}),
map((rawDevices: RawBLEDevice[]) =>
rawDevices
.sort((a, b) => b.rssi - a.rssi)
.map<AbstractDevice | null>(
rawDevice =>
(rawDevice.name &&
this.devices
.map(deviceType => this.devicesService.buildDevice(deviceType, rawDevice))
.filter((device: AbstractDevice | null): device is AbstractBLEDevice => device !== null)[0]) ||
null
)
.filter((device): device is AbstractDevice => device !== null)
)
)
.subscribe(devices => this.store.dispatch(new BLEDevicesDiscovered(devices)));
}
private onBLEError() {
this.diagnostic.registerBluetoothStateChangeHandler(() => {
this.store.dispatch(new StartDiscoverBLEDevices()).subscribe();
this.diagnostic.registerBluetoothStateChangeHandler(() => {});
});
this.alertService
.show(
{
header: this.translateService.instant('BLUETOOTH.BLE_DISABLED.TITLE'),
message: this.translateService.instant('BLUETOOTH.BLE_DISABLED.NOTICE'),
backdropDismiss: false,
buttons: [
{
text: this.translateService.instant('GENERAL.GO_TO_SETTINGS'),
handler: () => {
if (this.platform.is('ios')) {
this.diagnostic.switchToSettings();
} else {
this.diagnostic.switchToBluetoothSettings();
}
return false;
}
}
]
},
false
)
.then(alert => (this.currentAlert = alert));
}
}