UNPKG

5.05 kBJavaScriptView Raw
1var EventEmitter = require('events').EventEmitter;
2var util = require('util');
3
4function Peripheral(controller, rssi, data, address) {
5 this.rssi = rssi;
6 this.services = {};
7 this.advertisingData = data;
8 this.address = address;
9 this.connection = null;
10 this.flags = null;
11 this.bondHandle = null;
12
13 // Characteristics discovered. Handles are keys
14 this.characteristics = {};
15
16 this._controller = controller;
17 this._unassignedCharacteristics = [];
18 this._unassignedDescriptors = [];
19}
20
21util.inherits(Peripheral, EventEmitter);
22
23Peripheral.prototype.toString = function() {
24 return JSON.stringify({
25 address: this.address.toString(),
26 services: this.services,
27 rssi: this.rssi
28 });
29};
30
31Peripheral.prototype.connect = function(callback) {
32 this._controller.connect(this, callback);
33};
34
35Peripheral.prototype.disconnect = function(callback) {
36 this._controller.disconnect(this, callback);
37};
38
39Peripheral.prototype.discoverServices = function(services, callback) {
40 this._controller.discoverServices(this, services, callback);
41};
42
43Peripheral.prototype.discoverAllServices = function(callback) {
44 this._controller.discoverAllServices(this, callback);
45};
46
47/*
48* When we make a new service, we should add it to our peripheral and
49* find any unmatched characteristics that belong to it (if the user called
50* discoverCharacteristics before finding services, for example)
51*/
52Peripheral.prototype.syncService = function(service, callback) {
53 // Save the service to the services object
54 this.services[service.uuid.toString()] = service;
55 // Iterate through unassigned characteristics
56 newUnassigned = [];
57
58 for (var i in this._unassignedCharacteristics) {
59 // Grab the characteristic
60 var characteristic = this._unassignedCharacteristics[i];
61 // If the handle falls in range of this service
62 if (characteristic.handle >= service._startHandle && characteristic.handle <= service._endHandle) {
63
64 // Add it to the service
65 service.characteristics[characteristic.uuid.toString()] = characteristic;
66
67 // Emit the characteristic from service when it's matched
68 setImmediate(function() {
69 service.emit('characteristicDiscover', characteristic);
70 });
71 }
72 else {
73 // Add it to the list of characteristics to put back
74 newUnassigned.push(characteristic);
75 }
76 }
77
78 // Re-assign unmatched characteristics
79 this._unassignedCharacteristics = newUnassigned;
80
81 callback && callback();
82}
83
84// Note that this won't sort correctly until beta/189 is fixed...
85Peripheral.prototype.sortServices = function(callback) {
86 var sortable = [];
87
88 for (var service in this.services) {
89 sortable.push(this.services[service]);
90 }
91
92 sortable.sort(function(a, b) {return a._startHandle - b._startHandle});
93
94 callback && callback(sortable);
95
96 return sortable;
97}
98
99Peripheral.prototype.discoverCharacteristics = function(characteristics, callback) {
100 this._controller.discoverCharacteristics(this, characteristics, callback);
101};
102
103Peripheral.prototype.discoverAllCharacteristics = function(callback) {
104 this._controller.discoverAllCharacteristics(this, callback);
105};
106
107/*
108* When characteristics are found after services, they need to be matched up
109* to the appropriate service
110*/
111Peripheral.prototype.syncCharacteristic = function(characteristic, callback) {
112
113 this.characteristics[characteristic.handle] = characteristic;
114
115 // Sort the services to speed things up a bit
116 this.sortServices(function(sortedServices) {
117 // Iterate through the services
118 for (var i = 0; i < sortedServices.length; i++) {
119 // Grab each service
120 var service = sortedServices[i];
121 // If this characteristic handle falls in the range of the start and end handle
122 if (characteristic.handle >= service._startHandle && characteristic.handle <= service._endHandle) {
123 // Assign this characteristic to this service
124 service.characteristics[characteristic.uuid.toString()] = characteristic;
125
126 return;
127 }
128 }
129 // If there is no match for this characteristic, leave it unassigned
130 this._unassignedCharacteristics.push(characteristic);
131
132 return;
133 }.bind(this));
134}
135
136Peripheral.prototype.discoverAllAttributes = function(callback) {
137 this._controller.discoverAllAttributes(this, callback);
138}
139
140Peripheral.prototype.discoverAllServicesAndCharacteristics = function(callback) {
141 this._controller.discoverAllServicesAndCharacteristics(this, callback);
142}
143
144Peripheral.prototype.discoverAllDescriptors = function(callback) {
145 this._controller.discoverAllDescriptors(this, callback);
146}
147
148Peripheral.prototype.deleteBond = function(callback) {
149 this._controller.deleteBond(this, callback);
150}
151
152Peripheral.prototype.startEncryption = function(callback) {
153 this._controller.startEncryption(this, callback);
154}
155
156Peripheral.prototype.enterPasskey = function(passKey, callback) {
157 this._controller.enterPasskey(this, passkey, callback);
158}
159
160Peripheral.prototype.updateRssi = function(callback) {
161 this._controller.updateRssi(this, callback);
162};
163
164module.exports = Peripheral;