forked from jacobrosenthal/react-native-ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipheral.js
137 lines (112 loc) · 3.33 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
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
/*jshint loopfunc: true */
var debug = require('debug')('peripheral');
var events = require('events');
var util = require('util');
function Peripheral(noble, id, address, addressType, connectable, advertisement, rssi) {
this._noble = noble;
this.id = id;
this.uuid = id; // for legacy
this.address = address;
this.addressType = addressType;
this.connectable = connectable;
this.advertisement = advertisement;
this.rssi = rssi;
this.services = null;
this.state = 'disconnected';
}
util.inherits(Peripheral, events.EventEmitter);
Peripheral.prototype.toString = function() {
return JSON.stringify({
id: this.id,
address: this.address,
addressType: this.addressType,
connectable: this.connectable,
advertisement: this.advertisement,
rssi: this.rssi,
state: this.state
});
};
Peripheral.prototype.connect = function(callback) {
if (callback) {
this.once('connect', function(error) {
callback(error);
});
}
if (this.state === 'connected') {
this.emit('connect', new Error('Peripheral already connected'));
} else {
this.state = 'connecting';
this._noble.connect(this.id);
}
};
Peripheral.prototype.disconnect = function(callback) {
if (callback) {
this.once('disconnect', function() {
callback(null);
});
}
this.state = 'disconnecting';
this._noble.disconnect(this.id);
};
Peripheral.prototype.updateRssi = function(callback) {
if (callback) {
this.once('rssiUpdate', function(rssi) {
callback(null, rssi);
});
}
this._noble.updateRssi(this.id);
};
Peripheral.prototype.discoverServices = function(uuids, callback) {
if (callback) {
this.once('servicesDiscover', function(services) {
callback(null, services);
});
}
this._noble.discoverServices(this.id, uuids);
};
Peripheral.prototype.discoverSomeServicesAndCharacteristics = function(serviceUuids, characteristicsUuids, callback) {
this.discoverServices(serviceUuids, function(err, services) {
var numDiscovered = 0;
var allCharacteristics = [];
for (var i in services) {
var service = services[i];
service.discoverCharacteristics(characteristicsUuids, function(error, characteristics) {
numDiscovered++;
if (error === null) {
for (var j in characteristics) {
var characteristic = characteristics[j];
allCharacteristics.push(characteristic);
}
}
if (numDiscovered === services.length) {
if (callback) {
callback(null, services, allCharacteristics);
}
}
}.bind(this));
}
}.bind(this));
};
Peripheral.prototype.discoverAllServicesAndCharacteristics = function(callback) {
this.discoverSomeServicesAndCharacteristics([], [], callback);
};
Peripheral.prototype.readHandle = function(handle, callback) {
if (callback) {
this.once('handleRead' + handle, function(data) {
callback(null, data);
});
}
this._noble.readHandle(this.id, handle);
};
Peripheral.prototype.writeHandle = function(handle, data, withoutResponse, callback) {
if (!(data instanceof Buffer)) {
throw new Error('data must be a Buffer');
}
if (callback) {
this.once('handleWrite' + handle, function() {
callback(null);
});
}
this._noble.writeHandle(this.id, handle, data, withoutResponse);
};
module.exports = Peripheral;