forked from elin-moco/ble-serialport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (120 loc) · 3.26 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
/* global process, require, module */
'use strict';
require('./polyfills');
var util = require('util');
var stream = require('stream');
if (process) {
if (process.browser) {
require('blue-yeast');
} else {
var Bluetooth = require('blue-yeast').Bluetooth;
}
}
function BleSerialPort(options) {
this.options = options;
}
util.inherits(BleSerialPort, stream.Stream);
BleSerialPort.prototype.connect = function() {
var self = this;
return new Promise(function(resolve, reject) {
try {
if (process.browser) {
self.ble = window.Bluetooth;
} else {
self.ble = Bluetooth;
}
self.device = self.ble.connect(self.options.name, self.options.address);
self.device.on('connect', function() {
this.startNotifications();
resolve();
});
} catch (exp) {
reject();
console.error('error on message', exp);
self.emit('error', 'error receiving message: ' + exp);
}
self.buffer = null;
var START_SYSEX = 0xF0;
var END_SYSEX = 0xF7;
self.device.on('data', function(data) {
try {
data = new Uint8Array(data);
if (null !== self.buffer) {
self.buffer = self._concatBuffer(self.buffer, data);
if (data[data.length - 1] === END_SYSEX) {
//end of SYSEX response
self.emit('data', self.buffer);
self.buffer = null;
}
} else if (data[0] === START_SYSEX &&
data[data.length - 1] !== END_SYSEX) {
//SYSEX response incomplete, wait for END_SYSEX byte
self.buffer = data;
} else {
self.emit('data', data);
}
} catch (exp) {
console.error('error on message', exp);
self.emit('error', 'error receiving message: ' + exp);
}
});
self.device.on('error', reject);
});
};
BleSerialPort.prototype.disconnect = function() {
return this.device.disconnect();
};
BleSerialPort.prototype.open = function(callback) {
if (callback) {
callback();
}
};
BleSerialPort.prototype.write = function(data, callback) {
this.device.send(data);
};
BleSerialPort.prototype.close = function(callback) {
if (callback) {
callback();
}
};
BleSerialPort.prototype.flush = function(callback) {
if (callback) {
callback();
}
};
BleSerialPort.prototype.drain = function(callback) {
if (callback) {
callback();
}
};
BleSerialPort.prototype._concatBuffer = function(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(buffer1 , 0);
tmp.set(buffer2, buffer1.byteLength);
return tmp;
};
BleSerialPort.prototype._parseHexString = function(str) {
var arrayBuffer = new ArrayBuffer(Math.ceil(str.length / 2));
var uint8Array = new Uint8Array(arrayBuffer);
for (var i = 0, j = 0; i < str.length; i += 2, j++) {
uint8Array[j] = parseInt(str.substr(i, 2), 16);
}
return arrayBuffer;
};
BleSerialPort.prototype._toHexString = function(arrayBuffer) {
var str = '';
if (arrayBuffer) {
var uint8Array = new Uint8Array(arrayBuffer);
for (var i = 0; i < uint8Array.length; i++) {
var b = uint8Array[i].toString(16);
if (b.length == 1) {
str += '0';
}
str += b;
}
}
return str;
};
module.exports = {
SerialPort: BleSerialPort
};