-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.module.ts
95 lines (85 loc) · 3.68 KB
/
bluetooth.module.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
import * as PiStation from "../../node_modules/pistation-definitions/PiStation";
import * as Rx from 'rxjs/Rx';
import {Module} from "../../app/module";
import {Server} from "../../app/server";
import {StoreReadData} from "../../app/server";
export class BluetoohModule extends Module {
private noble: any;
private availableDevices: any[];
constructor(private server:Server) {
super('BleutoothModule');
this.noble = require('noble');
// nobleStateChanged('stateChange').subscribe((event)=>{
// console.log('test', event);
// }, (error)=>{
// console.log('errors', error)
// }, ()=> console.log('complete'));
let stateChangeEvent = this.on('stateChange');
let isPoweredOn = stateChangeEvent.filter((state)=> state === 'poweredOn');
let isPoweredOff = stateChangeEvent.filter((state)=> state === 'poweredOff');
isPoweredOff.do(()=> this.noble.stopScanning());
let nobleSearchStart = this.on('scanStart').takeUntil(isPoweredOff);
let nobleSearchStop = this.on('scanStop');
isPoweredOn.subscribe((data)=> console.log('isPoweredOn', data));
nobleSearchStart.map((search) => {
console.log('search', search)
});
let discoveredDevices = this.on('discover')
.bufferTime(5000)
.first();
discoveredDevices.subscribe((devices: any[]) => {
this.availableDevices = devices;
let connectBluetoothButton = new PiStation.Function('connectBluetooth', [
new PiStation.ArgumentMultiple({
key: 'connectBluetooth',
label: 'Connect Bluetooth',
options: devices.map((device) => {
return {key:device.uuid, value:`${device.advertisement.localName} ${device.rssi}`}
}),
required: true,
})
]);
this.addFunction(connectBluetoothButton);
console.log('Connect bluetooth button: ', connectBluetoothButton);
console.log(devices[0]);
});
isPoweredOn.subscribe((state) => {
console.log('noble state: ', state);
this.noble.startScanning();
});
nobleSearchStart.subscribe((event) => {
console.log('noble Search Start: ', event);
});
nobleSearchStop.subscribe((event) => {
console.log('noble Search Stop: ', event);
});
}
private on(event : string) {
console.log(1);
return Rx.Observable.create((observer : Rx.Observer<string>)=> {
this.noble.on(event, (stateChange : string)=> {
observer.next(stateChange);
})
});
}
private connectBluetooth(args) {
let device = this.availableDevices.filter((device) => device.uuid === args.connectBluetooth)[0];
device.connect((error) => {
console.log('connected to peripheral: ' + device.uuid);
device.discoverServices(null, (error, services) => {
services[0].discoverCharacteristics(null, (error, characteristics) => {
console.log(characteristics[1]);
characteristics[1].on('read', (data, isNotification) => {
// data is a buffer
console.log('Data', data);
console.log('Notification? ', isNotification);
});
characteristics[1].notify(true, (error) => {
console.log('notify on.');
});
})
});
});
return console.log('button clicked', device);
}
}