-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
204 lines (149 loc) · 4.84 KB
/
index.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
var util = require('util');
var stream = require('stream');
var noble = require('noble');
var debug = require('debug')('ble-serial');
var DEFAULT_SERIAL_SERVICE = '6e400001b5a3f393e0a9e50e24dcca9e';
var DEFAULT_TRANSMIT_CHARACTERISTIC = '6e400002b5a3f393e0a9e50e24dcca9e';
var DEFAULT_RECEIVE_CHARACTERISTIC = '6e400003b5a3f393e0a9e50e24dcca9e';
function compareUUIDs(a, b){
a = a || '';
b = b || '';
a = a.toLowerCase().replace(/\-/g, '');
b = b.toLowerCase().replace(/\-/g, '');
return a === b;
}
function BLESerialPort(options) {
options = options || {};
this.receiveCharacteristic = options.receiveCharacteristic || DEFAULT_RECEIVE_CHARACTERISTIC;
this.transmitCharacteristic = options.transmitCharacteristic || DEFAULT_TRANSMIT_CHARACTERISTIC;
this.serviceId = options.serviceId || DEFAULT_SERIAL_SERVICE;
this.peripheral = options.peripheral;
this.buffer = null;
this.lastCheck = 0;
this.lastSend = 0;
var self = this;
if(!this.peripheral){
if(noble.state === 'poweredOn'){
noble.startScanning([self.serviceId], false);
}else{
noble.on('stateChange', function(state) {
if (state === 'poweredOn'){
noble.startScanning([self.serviceId], false);
}else{
noble.stopScanning();
}
});
}
noble.on('discover', function(peripheral) {
// we found a peripheral, stop scanning
noble.stopScanning();
self.peripheral = peripheral;
//
// The advertisment data contains a name, power level (if available),
// certain advertised service uuids, as well as manufacturer data,
// which could be formatted as an iBeacon.
//
debug('found peripheral:', self.peripheral.advertisement);
//
// Once the peripheral has been discovered, then connect to it.
// It can also be constructed if the uuid is already known.
///
self.peripheral.connect(function(err) {
debug('connected', err);
//
// Once the peripheral has been connected, then discover the
// services and characteristics of interest.
//
self.peripheral.discoverServices([], function(err, services) {
debug('discoverServices', err, services);
services.forEach(function(service) {
debug('found service', service);
//
// So, discover its characteristics.
//
service.discoverCharacteristics([], function(err, characteristics) {
debug('found characteristics', err, characteristics);
characteristics.forEach(function(characteristic) {
//
// Loop through each characteristic and match them to the
// UUIDs that we know about.
//
debug('found characteristic:', characteristic.uuid);
if (compareUUIDs(self.transmitCharacteristic, characteristic.uuid)){
self.transmit = characteristic;
}
else if (compareUUIDs(self.receiveCharacteristic, characteristic.uuid)) {
self.receive = characteristic;
}
});
//
// Check to see if we found all of our characteristics.
//
if (self.transmit && self.receive){
debug('have both characteristics', self.transmit, self.receive);
self.emit('open');
self.receive.on('read', function(data, isNotification) {
debug('read', data, isNotification);
self.emit('data', data);
});
self.receive.notify(true, function(err) {
debug('notify', err);
});
}
else {
debug('missing characteristics');
}
});
});
});
});
});
}
else{
//TODO do rx/tx stuff on the passed in peripheral
}
}
util.inherits(BLESerialPort, stream.Stream);
BLESerialPort.prototype.open = function (callback) {
this.emit('open');
if (callback) {
callback();
}
};
BLESerialPort.prototype.write = function (data, callback) {
debug('writing', data);
if (!Buffer.isBuffer(data)) {
data = new Buffer(data);
}
if(this.transmit){
this.transmit.write(data, false);
}
if (typeof callback === 'function') {
process.nextTick(callback);
}
};
BLESerialPort.prototype.close = function (callback) {
debug('closing');
if(this.peripheral){
this.peripheral.disconnect();
}
if(callback){
callback();
}
};
BLESerialPort.prototype.flush = function (callback) {
debug('flush');
if(callback){
callback();
}
};
BLESerialPort.prototype.drain = function (callback) {
debug('drain');
if(callback){
callback();
}
};
module.exports = {
SerialPort: BLESerialPort
};