Skip to content

Commit 316e228

Browse files
committed
Support notifications
1 parent dbb0c63 commit 316e228

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

lib/bluetooth_device.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global EventEmitter2 */
1+
/* global Bluetooth, EventEmitter2 */
22
'use strict';
33

44
(function(exports) {
@@ -9,22 +9,44 @@
99

1010
BluetoothDevice.prototype = Object.create(EventEmitter2.prototype);
1111

12-
['writeCharacteristic', 'notifyCharacteristic'].forEach(function(item) {
13-
Object.defineProperty(BluetoothDevice.prototype, item, {
14-
set: function(value) {
15-
this['_' + item] = value;
16-
},
17-
get: function() {
18-
return this['_' + item];
19-
}
20-
});
21-
});
12+
BluetoothDevice.prototype.gatt = null;
13+
BluetoothDevice.prototype.writeCharacteristic = null;
14+
BluetoothDevice.prototype.notifyCharacteristic = null;
15+
BluetoothDevice.prototype._isNotificationsStarted = false;
2216

2317
BluetoothDevice.prototype.send = function(data) {
2418
data = this._parseHexString(data);
2519
this.writeCharacteristic.writeValue(data);
2620
};
2721

22+
BluetoothDevice.prototype.startNotifications = function() {
23+
if (this._isNotificationsStarted) {
24+
return;
25+
} else {
26+
this._isNotificationsStarted = true;
27+
}
28+
var characteristic = this.notifyCharacteristic;
29+
this.gatt.addEventListener('characteristicchanged', (evt) => {
30+
this.emit('data', evt.characteristic.value);
31+
});
32+
characteristic.startNotifications();
33+
var descriptor = characteristic.descriptors.find(function(descriptor) {
34+
return descriptor.uuid === Bluetooth.CCCD_UUID;
35+
});
36+
if (descriptor) {
37+
var arrayBuffer = new ArrayBuffer(2);
38+
var uint8Array = new Uint8Array(arrayBuffer);
39+
uint8Array[0] = 0x01;
40+
uint8Array[1] = 0x00;
41+
descriptor.writeValue(arrayBuffer);
42+
}
43+
};
44+
45+
BluetoothDevice.prototype.stopNotifications = function() {
46+
this._isNotificationsStarted = false;
47+
this.notifyCharacteristic.stopNotifications();
48+
};
49+
2850
BluetoothDevice.prototype._parseHexString = function(str) {
2951
var arrayBuffer = new ArrayBuffer(Math.ceil(str.length / 2));
3052
var uint8Array = new Uint8Array(arrayBuffer);

lib/bluetooth_helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// The timeout to connect next device. It's used for workaround.
1616
BluetoothHelper.CONNECTING_TIMEOUT = 1000;
17+
BluetoothHelper.prototype.CCCD_UUID = '00002902-0000-1000-8000-00805f9b34fb';
1718

1819
BluetoothHelper.prototype._scan = null;
1920
BluetoothHelper.prototype._devices = null,
@@ -87,6 +88,7 @@
8788
});
8889
if (devcie) {
8990
// XXX: Workaround to connect multiple devices correctly.
91+
devcie.gatt = gatt;
9092
setTimeout(() => {
9193
gatt.connect().then(() => {
9294
this._discoverServices(devcie, gatt);

0 commit comments

Comments
 (0)