forked from qbalsdon/BLE_Rpi_Peripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipheral.js
99 lines (88 loc) · 3.17 KB
/
peripheral.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
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
var bleno = require('bleno');
var exec = require('child_process').exec;
var CHUNK_SIZE = 20;
var Descriptor = bleno.Descriptor;
var deviceName = 'RaspberrPi3';
var myId = '4afb720a-5214-4337-841b-d5f954214877';
var data = new Buffer('Send me some data to display');
var output = "";
var updateCallback;
var terminalCallback;
var terminalResponse;
var START_CHAR = String.fromCharCode(002); //START OF TEXT CHAR
var END_CHAR = String.fromCharCode(003); //END OF TEXT CHAR
function sliceUpResponse(callback, responseText) {
if (!responseText || !responseText.trim()) return;
callback(new Buffer(START_CHAR));
while(responseText !== '') {
callback(new Buffer(responseText.substring(0, CHUNK_SIZE)));
responseText = responseText.substring(CHUNK_SIZE);
}
callback(new Buffer(END_CHAR));
}
var terminal = new bleno.Characteristic({
uuid : '8bacc104-15eb-4b37-bea6-0df3ac364199',
properties : ['write','read','notify'],
onReadRequest : function(offset, callback) {
console.log("Read request");
callback(bleno.Characteristic.RESULT_SUCCESS, new Buffer(terminalResponse).slice(offset));
},
onWriteRequest : function(newData, offset, withoutResponse, callback) {
if(offset) {
callback(bleno.Characteristic.RESULT_ATTR_NOT_LONG);
} else {
var data = newData.toString('utf8');
console.log("Command received: [" + data + "]");
dir = exec(data, function(err, stdout, stderr) {
if (err) {
var stringError = JSON.stringify(err);
console.log(stringError);
callback(bleno.Characteristic.RESULT_SUCCESS);
terminalResponse = stringError;
} else {
console.log(stdout);
callback(bleno.Characteristic.RESULT_SUCCESS);
terminalResponse = stdout;
}
if (terminalCallback) sliceUpResponse(terminalCallback, terminalResponse);
});
}
},
onSubscribe: function(maxValueSize, updateValueCallback) {
console.log("onSubscribe called");
terminalCallback = updateValueCallback;
},
onUnsubscribe: function() {
terminalCallback = null;
console.log("onUnsubscribe");
}
});
bleno.on('stateChange', function(state) {
console.log('on -> stateChange: ' + state);
if (state === 'poweredOn') {
bleno.startAdvertising(deviceName,[myId]);
} else {
bleno.stopAdvertising();
}
});
bleno.on('advertisingStart', function(error) {
console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
if (!error) {
bleno.setServices([
new bleno.PrimaryService({
uuid : myId,
characteristics : [
// add characteristics here
terminal
]
})
]);
console.log('service added');
}
});
bleno.on('accept', function(clientAddress) {
console.log("Accepted connection from: " + clientAddress);
});
bleno.on('disconnect', function(clientAddress) {
console.log("Disconnected from: " + clientAddress);
});