@@ -36,15 +36,15 @@ function Connection(device, callback) {
36
36
37
37
log ( connection . name + ": Connecting..." ) ;
38
38
device . connect ( function ( error ) {
39
- if ( error ) {
39
+ if ( error ) {
40
40
log ( connection . name + ": Error Connecting: " + error . toString ( ) ) ;
41
41
connection . device = undefined ;
42
42
connection . close ( ) ;
43
43
callback ( "Error Connecting: " + error . toString ( ) ) ;
44
44
} else {
45
45
log ( "Connected." ) ;
46
46
callback ( null , connection ) ;
47
- }
47
+ }
48
48
} ) ;
49
49
}
50
50
Connection . prototype . getCharacteristic = function ( serviceUUID , characteristicUUID , callback ) {
@@ -61,8 +61,8 @@ Connection.prototype.getCharacteristic = function(serviceUUID, characteristicUUI
61
61
} ;
62
62
log ( connection . name + ": found characteristic: " + matchedCharacteristic . uuid ) ;
63
63
callback ( null , matchedCharacteristic ) ;
64
- } else {
65
- callback ( "Characteristic " + characteristicUUID + " not found" ) ;
64
+ } else {
65
+ callback ( "Characteristic " + characteristicUUID + " not found" ) ;
66
66
}
67
67
} ) ;
68
68
}
@@ -74,7 +74,7 @@ Connection.prototype.getCharacteristic = function(serviceUUID, characteristicUUI
74
74
75
75
log ( connection . name + ": Getting Service..." ) ;
76
76
var timeout = setTimeout ( function ( ) {
77
- timeout = undefined ;
77
+ timeout = undefined ;
78
78
log ( connection . name + ": Timed out getting services." ) ;
79
79
callback ( "Timed out getting services." ) ;
80
80
} , 4000 ) ;
@@ -91,21 +91,75 @@ Connection.prototype.getCharacteristic = function(serviceUUID, characteristicUUI
91
91
getCharacteristicFromService ( matchedService ) ;
92
92
} else {
93
93
if ( timeout ) clearTimeout ( timeout ) ;
94
- callback ( "Service " + serviceUUID + " not found" ) ;
94
+ callback ( "Service " + serviceUUID + " not found" ) ;
95
95
}
96
- } ) ;
96
+ } ) ;
97
97
}
98
98
}
99
99
100
+ Connection . prototype . getServices = function ( callback ) {
101
+ var connection = this ;
102
+
103
+ function handleService ( allServices , index ) {
104
+ matchedService = allServices [ index ]
105
+ log ( connection . name + ": found service: " + matchedService . uuid , "getting Characteristic...." ) ;
106
+ if ( ! connection . services [ matchedService . uuid ] )
107
+ connection . services [ matchedService . uuid ] = { service : matchedService } ;
108
+ matchedService . discoverCharacteristics ( null , function ( error , characteristics ) { // do search for all characteristics
109
+ if ( ! error ) {
110
+ if ( timeout ) clearTimeout ( timeout ) ;
111
+ if ( characteristics != undefined && characteristics . length ) {
112
+ characteristics . forEach ( function ( matchedCharacteristic ) {
113
+ connection . services [ matchedService . uuid ] [ matchedCharacteristic . uuid ] = {
114
+ characteristic : matchedCharacteristic ,
115
+ notifyCallback : undefined
116
+ } ;
117
+ log ( connection . name + ": found characteristic: " + matchedCharacteristic . uuid ) ;
118
+ } ) ;
119
+ }
120
+ if ( index < allServices . length - 1 ) { // Last service in array?
121
+ handleService ( allServices , index + 1 ) // Handle next service
122
+ } else {
123
+ callback ( null , connection . services ) // Return connection's services
124
+ }
125
+ } else {
126
+ callback ( "Failed to discover characteristics" )
127
+ }
128
+ } ) ;
129
+ }
130
+
131
+ // don't look in cache
132
+
133
+ log ( connection . name + ": Getting Services..." ) ;
134
+ var timeout = setTimeout ( function ( ) {
135
+ timeout = undefined ;
136
+ log ( connection . name + ": Timed out getting services." ) ;
137
+ callback ( "Timed out getting services." ) ;
138
+ } , 4000 ) ;
139
+
140
+
141
+ this . device . discoverServices ( null , function ( error , services ) { // do search for all services
142
+ if ( ! error ) {
143
+
144
+ if ( services != undefined && services . length ) {
145
+ handleService ( services , 0 )
146
+ } else {
147
+ callback ( null , { } )
148
+ }
149
+ } else {
150
+ callback ( "Failed to discover services" ) ;
151
+ }
152
+ } ) ;
153
+ }
100
154
101
- Connection . prototype . close = function ( ) {
155
+ Connection . prototype . close = function ( ) {
102
156
if ( this . device ) {
103
157
log ( this . name + ": Disconnecting." ) ;
104
- try {
105
- this . device . disconnect ( ) ;
158
+ try {
159
+ this . device . disconnect ( ) ;
106
160
log ( this . name + ": Disconnected" ) ;
107
- } catch ( e ) {
108
- log ( this . name + ": Disconnect error: " + e ) ;
161
+ } catch ( e ) {
162
+ log ( this . name + ": Disconnect error: " + e ) ;
109
163
}
110
164
this . device = undefined ;
111
165
}
@@ -124,7 +178,7 @@ Connection.prototype.setUsed = function() {
124
178
// -----------------------------------------------------------------------------
125
179
// -----------------------------------------------------------------------------
126
180
127
- // Repeated write to characteristic
181
+ // Repeated write to characteristic
128
182
function writeToCharacteristic ( characteristic , message , callback ) { // added function to write longer strings
129
183
if ( message . length ) {
130
184
var data = message . slice ( 0 , 20 ) ;
@@ -176,7 +230,7 @@ function serviceQueue() {
176
230
if ( connections . length < MAX_CONNECTIONS ) {
177
231
var job = queue . shift ( ) ;
178
232
discovery . stopScan ( ) ;
179
- setTimeout ( job , 1000 ) ;
233
+ setTimeout ( job , 1000 ) ;
180
234
}
181
235
}
182
236
@@ -200,7 +254,7 @@ exports.write = function(device, service, characteristic, data, callback) {
200
254
getConnectedDevice ( device , function ( err , connection ) {
201
255
if ( err ) return setNotBusy ( ) ;
202
256
203
- connection . getCharacteristic ( util . uuid2noble ( service ) ,
257
+ connection . getCharacteristic ( util . uuid2noble ( service ) ,
204
258
util . uuid2noble ( characteristic ) ,
205
259
function ( err , char ) {
206
260
if ( err ) return setNotBusy ( ) ; ;
@@ -222,7 +276,7 @@ exports.read = function(device, service, characteristic, callback) {
222
276
isBusy = true ;
223
277
getConnectedDevice ( device , function ( err , connection ) {
224
278
if ( err ) return ;
225
- connection . getCharacteristic ( util . uuid2noble ( service ) ,
279
+ connection . getCharacteristic ( util . uuid2noble ( service ) ,
226
280
util . uuid2noble ( characteristic ) ,
227
281
function ( err , char ) {
228
282
if ( err ) return setNotBusy ( ) ;
@@ -235,6 +289,48 @@ exports.read = function(device, service, characteristic, callback) {
235
289
} ) ;
236
290
} ;
237
291
292
+ /* Read services from the given device */
293
+ exports . readServices = function ( device , callback ) {
294
+ if ( isBusy ) {
295
+ queue . push ( function ( ) { exports . readServices ( device , callback ) ; } ) ;
296
+ return ;
297
+ }
298
+ isBusy = true ;
299
+ getConnectedDevice ( device , function ( err , connection ) {
300
+ if ( err ) return ;
301
+ connection . getServices ( function ( err , services ) {
302
+ if ( err ) return setNotBusy ( ) ;
303
+ /* Extract UUIDs from the connection's services object.
304
+ Output array format:
305
+ [
306
+ {
307
+ uuid:serviceUuid,
308
+ characteristics: [
309
+ {
310
+ uuid:characteristicUuid
311
+ }
312
+ ]
313
+ }
314
+ ]
315
+ */
316
+ var output = [ ]
317
+ for ( service in services ) {
318
+ var item = {
319
+ uuid :service ,
320
+ characteristics :[ ]
321
+ }
322
+ for ( key in services [ service ] ) {
323
+ if ( key !== 'service' ) item . characteristics . push ( { uuid :key } )
324
+ }
325
+ output . push ( item )
326
+ }
327
+ // Stringifies array before sending
328
+ callback ( JSON . stringify ( output ) )
329
+ setNotBusy ( ) ;
330
+ } ) ;
331
+ } ) ;
332
+ } ;
333
+
238
334
/* Start notifications on the given device */
239
335
exports . notify = function ( device , service , characteristic , callback ) {
240
336
if ( isBusy ) {
@@ -246,17 +342,17 @@ exports.notify = function(device, service, characteristic, callback) {
246
342
if ( err ) return setNotBusy ( ) ;
247
343
var serviceUUID = util . uuid2noble ( service ) ;
248
344
var characteristicUUID = util . uuid2noble ( characteristic ) ;
249
- connection . getCharacteristic ( serviceUUID , characteristicUUID ,
345
+ connection . getCharacteristic ( serviceUUID , characteristicUUID ,
250
346
function ( err , char ) {
251
347
if ( err ) return setNotBusy ( ) ;
252
348
if ( connection . services [ serviceUUID ] [ characteristicUUID ] . notifyCallback ) {
253
349
connection . services [ serviceUUID ] [ characteristicUUID ] . notifyCallback = callback ;
254
350
return setNotBusy ( ) ; // notifications were already set up
255
- }
351
+ }
256
352
char . on ( 'data' , function ( data ) {
257
353
//log(connection.name+": notification on "+data.toString());
258
354
//new Uint8Array(data).buffer
259
- if ( connection . services [ serviceUUID ] [ characteristicUUID ] . notifyCallback )
355
+ if ( connection . services [ serviceUUID ] [ characteristicUUID ] . notifyCallback )
260
356
connection . services [ serviceUUID ] [ characteristicUUID ] . notifyCallback ( data . toString ( ) ) ;
261
357
} ) ;
262
358
char . subscribe ( function ( ) {
@@ -276,7 +372,7 @@ exports.ping = function(device, callback) {
276
372
}
277
373
isBusy = true ;
278
374
getConnectedDevice ( device , function ( err , connection ) {
279
- if ( err ) return setNotBusy ( ) ;
375
+ if ( err ) return setNotBusy ( ) ;
280
376
if ( callback ) callback ( null ) ;
281
377
setNotBusy ( ) ;
282
378
} ) ;
@@ -296,5 +392,5 @@ setInterval(function() {
296
392
connection . close ( ) ;
297
393
i -- ; // connection automatically removes itself from list
298
394
}
299
- }
395
+ }
300
396
} , 1000 ) ;
0 commit comments