-
Notifications
You must be signed in to change notification settings - Fork 4
/
bleclientV2.js
67 lines (51 loc) · 1.32 KB
/
bleclientV2.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
var bleno = require('bleno');
var spawn = require("child_process").spawn;
var encoding = require("encoding");
bleno.on('stateChange', function(state){
console.log("BLE STARTED")
if(state === 'poweredOn'){
bleno.startAdvertising('Air92', ['12ab']);
}else{
bleno.stopAdvertising();
}
});
bleno.on('advertisingStart', function(error){
if(error){
// error on advertise start
}else{
var pythonProcess = spawn ("python",[PATH_TO_BLUELED]);
console.log('ADVERTISING START');
bleno.setServices([
new bleno.PrimaryService({
uuid : "12ab",
characteristics : [
new bleno.Characteristic({
value : null,
uuid : "34cd",
properties : ['read', 'write'],
onReadRequest : function(offset,callback){
sensorData().then((result) =>
{
console.log("read");
var buffer = new Buffer(result);
callback(this.RESULT_SUCCESS,buffer);
})
}
})
]
})
]);
}
});
function sensorData()
{
return new Promise((resolve, reject) =>
{
var pythonProcess = spawn ("python",[PATH_TO_SENSOR_DATA]);
pythonProcess.stdout.on("data", function (data)
{
//console.log(String.fromCharCode.apply(null,new Uint8Array(data)));
resolve(data);
});
});
}