-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
68 lines (62 loc) · 2.13 KB
/
test.js
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
console.log("ble ecg interfacing");
// CONSTANTS
var noble = require('noble');
var ARRAY_SIZE = 1000;
var buffer_x = 1000;
var buffer_y = 1000;
var min_width = 60;
var MIN_NUM_VALUES_TO_PLOT = 100;
var counter = -1;
var lead1data = [];
var lead2data = [];
var ws;
var ECG_DATA_PORT = 8097;
var connected = false;
// start scanning after connection is established
noble.on('stateChange', function(state) {
console.log("statechange:" + state);
if (state === 'poweredOn') {
console.log("Bluetooth is on here");
noble.startScanning();
} else {
console.log("Bluetooth not on");
noble.stopScanning();
}
});
// if the connected device is recognised, call function
noble.on('discover', function(peripheral) {
var advertisement = peripheral.advertisement;
var localName = advertisement.localName;
console.log(localName);
// OSX will have `undefined` as peripheral name if not connected earlier
if (localName == undefined || localName.substring(0, 6) != "ADS129") {
return;
} else {
noble.stopScanning();
}
explore(peripheral);
});
function explore(peripheral) {
peripheral.on('disconnect', function() {
console.log('device disconnected');
});
peripheral.connect(function(error) {
console.log('connected to peripheral: ' + peripheral.uuid);
peripheral.writeHandle('0x0013', new Buffer([0x01, 0x00]), 0, function(error) {
console.log("handle written");
});
peripheral.discoverServices(['2d0d'], function(error, services) {
console.log('service discovered');
var service = services[0];
service.discoverCharacteristics(['2d37'], function(error, characteristics) {
//console.log(characteristics);
var characteristic = characteristics[0];
characteristic.on('read', function(data, isNotification) {
// 8 bits for each data entry X 3
x = data[0] * 65536 + data[1] * 256 + data[2];
y = data[3] * 65536 + data[4] * 256 + data[5];
});
});
});
});
}