/
ble-web.js
220 lines (198 loc) · 8.22 KB
/
ble-web.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const log = require('../../util/log');
const uint8ArrayToBase64 = array => window.btoa(String.fromCharCode(...array));
const base64ToUint8Array = base64 => {
const raw = window.atob(base64);
return Uint8Array.from(Array.prototype.map.call(raw, x => x.charCodeAt(0)));
};
class WebBLE {
/**
* A BLE peripheral object. It handles connecting, over Web Bluetooth API, to
* BLE peripherals, and reading and writing data to them.
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
* @param {string} extensionId - the id of the extension using this object.
* @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection.
* @param {object} resetCallback - a callback for resetting extension state.
*/
constructor (runtime, extensionId, peripheralOptions, connectCallback, resetCallback = null) {
/**
* Remote device which have been connected.
* @type {BluetoothDevice}
*/
this._device = null;
/**
* Remote GATT server
* @type {BluetoothRemoteGATTServer}
*/
this._server = null;
this._connectCallback = connectCallback;
this._disconnected = true;
this._characteristicDidChangeCallback = null;
this._resetCallback = resetCallback;
this._extensionId = extensionId;
this._peripheralOptions = peripheralOptions;
this._runtime = runtime;
this.requestPeripheral();
}
/**
* Request connection to the peripheral.
* Request user to choose a device, and then connect it automatically.
*/
requestPeripheral () {
if (this._server) {
this.disconnect();
}
navigator.bluetooth.requestDevice(this._peripheralOptions)
.then(device => {
this._device = device;
log.debug(`device=${this._device.name}`);
this._runtime.connectPeripheral(this._extensionId, this._device.id);
})
.catch(e => {
this._handleRequestError(e);
});
}
/**
* Try connecting to the GATT server of the device, and then call the connect
* callback when connection is successful.
*/
connectPeripheral (/* id */) {
if (!this._device) {
throw new Error('device is not chosen');
}
this._device.gatt.connect()
.then(gattServer => {
log.debug(`GATTServer is connected`);
this._server = gattServer;
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
this._disconnected = false;
this._connectCallback();
this._device.addEventListener('gattserverdisconnected',
event => {
this.onDisconnected(event);
});
});
}
/**
* Disconnect from the device and clean up.
* Then emit the connection state by the runtime.
*/
disconnect () {
if (!this._server) return;
this._server.disconnect();
this._disconnected = true;
this._server = null;
this._device = null;
this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECTED);
}
/**
* @return {bool} whether the peripheral is connected.
*/
isConnected () {
if (!this._server) return false;
return this._server.connected;
}
/**
* Start receiving notifications from the specified ble service.
* @param {number} serviceId - the ble service to read.
* @param {number} characteristicId - the ble characteristic to get notifications from.
* @param {object} onCharacteristicChanged - callback for characteristic change notifications
* like function(base64message).
* @return {Promise} - a promise from the remote startNotifications request.
*/
startNotifications (serviceId, characteristicId, onCharacteristicChanged = null) {
return this._server.getPrimaryService(serviceId)
.then(service => service.getCharacteristic(characteristicId))
.then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged',
event => {
const dataView = event.target.value;
onCharacteristicChanged(uint8ArrayToBase64(new Uint8Array(dataView.buffer)));
});
characteristic.startNotifications();
});
}
/**
* Read from the specified ble service.
* @param {number} serviceId - the ble service to read.
* @param {number} characteristicId - the ble characteristic to read.
* @param {boolean} optStartNotifications - whether to start receiving characteristic change notifications.
* @param {object} onCharacteristicChanged - callback for characteristic change notifications
* like function(base64message).
* @return {Promise} - a promise from the remote read request which resolve {message: base64string}.
*/
read (serviceId, characteristicId, optStartNotifications = false, onCharacteristicChanged = null) {
return this._server.getPrimaryService(serviceId)
.then(service => service.getCharacteristic(characteristicId))
.then(characteristic => {
if (optStartNotifications) {
this.startNotifications(serviceId, characteristicId, onCharacteristicChanged);
}
return characteristic.readValue();
})
.then(dataView => ({
message: uint8ArrayToBase64(new Uint8Array(dataView.buffer))
}));
}
/**
* Write data to the specified ble service.
* @param {number} serviceId - the ble service to write.
* @param {number} characteristicId - the ble characteristic to write.
* @param {string} message - the message to send.
* @param {string} encoding - the message encoding type.
* @param {boolean} withResponse - if true, resolve after peripheral's response.
* @return {Promise} - a promise from the remote send request.
*/
// eslint-disable-next-line no-unused-vars
write (serviceId, characteristicId, message, encoding = null, withResponse = null) {
const value = encoding === 'base64' ? base64ToUint8Array(message) : message;
return this._server.getPrimaryService(serviceId)
.then(service => service.getCharacteristic(characteristicId))
.then(characteristic => {
if (withResponse && characteristic.writeValueWithResponse) {
return characteristic.writeValueWithResponse(value);
}
if (characteristic.writeValueWithoutResponse) {
return characteristic.writeValueWithoutResponse(value);
}
return characteristic.writeValue(value);
});
}
/**
* Handle an error resulting from losing connection to a peripheral.
*
* This could be due to:
* - battery depletion
* - going out of bluetooth range
* - being powered down
*
* Disconnect the device, and if the extension using this object has a
* reset callback, call it. Finally, emit an error to the runtime.
*/
handleDisconnectError (/* e */) {
// log.error(`BLE error: ${JSON.stringify(e)}`);
if (this._disconnected) return;
this.disconnect();
if (this._resetCallback) {
this._resetCallback();
}
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTION_LOST_ERROR, {
message: `Scratch lost connection to`,
extensionId: this._extensionId
});
}
_handleRequestError (/* e */) {
// log.error(`BLE error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_REQUEST_ERROR, {
message: `Scratch lost connection to`,
extensionId: this._extensionId
});
}
/**
* Called when disconnected by the device.
*/
onDisconnected (/* event */) {
this.handleDisconnectError(new Error('device disconnected'));
}
}
module.exports = WebBLE;