-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy-ble.js
525 lines (471 loc) · 12.6 KB
/
easy-ble.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
* File: easyble.js
* Description: Library for making BLE programming easier.
* Author: Miki
*
* Note: The object type called "device" below, is the "DeviceInfo"
* object obtained by calling evothings.ble.startScan, enhanced with
* additional properties and functions to allow easy access to
* object methods. Properties are also added to the Characteristic
* and Descriptor object. Added properties are prefixed with two
* underscores.
*/
loadPlugin = function () {
if (_.isObject(evothings)) {
return;
}
if (typeof window.evothings == 'object') {
evothings = window.evothings;
console.log('load from window');
return;
}
if (typeof window.plugins == 'object') {
if (typeof window.plugins.evothings == 'object') {
evothings = window.plugins.evothings;
console.log('load from window.plugin');
return;
}
}
if (cordova && typeof cordova.evothings == 'object') {
evothings = cordova.evothings;
console.log('load from cordova');
return;
}
if (cordova && typeof cordova.plugins == 'object') {
if (typeof cordova.plugins.evothings == 'object') {
evothings = cordova.plugins.evothings;
console.log('load from cordova.plugin');
return;
}
}
if (navigator && typeof navigator.evothings == 'object') {
evothings = navigator.evothings;
console.log('load from navigator');
return;
}
};
// Object that holds BLE data and functions.
easyble = (function()
{
/** Main object in the EasyBLE API. */
var easyble = {};
easyble.loadPlugin = loadPlugin;
/**
* Set to true to report found devices only once,
* set to false to report continuously.
*/
var reportDeviceOnce = false;
/** Internal properties and functions. */
var internal = {};
/** Internal variable used to track reading of service data. */
var readCounter = 0;
/** Table of discovered devices. */
internal.knownDevices = {};
/** Table of connected devices. */
internal.connectedDevices = {};
/**
* Set to true to report found devices only once,
* set to false to report continuously.
*/
easyble.reportDeviceOnce = function(reportOnce)
{
reportDeviceOnce = reportOnce;
};
/** Start scanning for devices. */
easyble.startScan = function(win, fail)
{
easyble.stopScan();
internal.knownDevices = {};
evothings.ble.startScan(function(device)
{
// Check if we already have got the device.
var existingDevice = internal.knownDevices[device.address]
if (existingDevice)
{
// Do not report device again if flag is set.
if (reportDeviceOnce) { return; }
// Flag not set, report device again.
existingDevice.rssi = device.rssi;
existingDevice.name = device.name;
win(existingDevice);
return;
}
// New device, add to known devices.
internal.knownDevices[device.address] = device;
// Add methods to the device info object.
internal.addMethodsToDeviceObject(device);
// Call callback function with device info.
win(device);
},
function(errorCode)
{
fail(errorCode);
});
};
/** Stop scanning for devices. */
easyble.stopScan = function()
{
//XXX
easyble.loadPlugin();
evothings.ble.stopScan();
};
/** Close all connected devices. */
easyble.closeConnectedDevices = function()
{
for (var key in internal.connectedDevices)
{
var device = internal.connectedDevices[key];
device && device.close();
internal.connectedDevices[key] = null;
}
};
/**
* Add functions to the device object to allow calling them
* in an object-oriented style.
*/
internal.addMethodsToDeviceObject = function(device)
{
/** Connect to the device. */
device.connect = function(win, fail)
{
internal.connectToDevice(device, win, fail);
};
/** Close the device. */
device.close = function()
{
device.deviceHandle && evothings.ble.close(device.deviceHandle);
};
/** Read devices RSSI. Device must be connected. */
device.readRSSI = function(win, fail)
{
evothings.ble.rssi(device.deviceHandle, win, fail);
};
/** Read all service info for the specified service UUIDs.
// If serviceUUIDs is null, info for all services is read
// (this can be time-consuming compared to reading a
// selected number of services). */
device.readServices = function(serviceUUIDs, win, fail)
{
internal.readServices(device, serviceUUIDs, win, fail);
};
/** Read value of characteristic. */
device.readCharacteristic = function(characteristicUUID, win, fail)
{
internal.readCharacteristic(device, characteristicUUID, win, fail);
};
/** Read value of descriptor. */
device.readDescriptor = function(characteristicUUID, descriptorUUID, win, fail)
{
internal.readDescriptor(device, characteristicUUID, descriptorUUID, win, fail);
};
/** Write value of characteristic. */
device.writeCharacteristic = function(characteristicUUID, value, win, fail)
{
internal.writeCharacteristic(device, characteristicUUID, value, win, fail);
};
/** Write value of descriptor. */
device.writeDescriptor = function(characteristicUUID, descriptorUUID, value, win, fail)
{
internal.writeDescriptor(device, characteristicUUID, descriptorUUID, value, win, fail);
};
/** Subscribe to characteristic value updates. */
device.enableNotification = function(characteristicUUID, win, fail)
{
internal.enableNotification(device, characteristicUUID, win, fail);
};
/** Unsubscribe from characteristic updates. */
device.disableNotification = function(characteristicUUID, win, fail)
{
internal.disableNotification(device, characteristicUUID, win, fail);
};
};
/** Connect to a device. */
internal.connectToDevice = function(device, win, fail)
{
evothings.ble.connect(device.address, function(connectInfo)
{
if (connectInfo.state == 2) // connected
{
device.deviceHandle = connectInfo.deviceHandle;
device.__uuidMap = {};
internal.connectedDevices[device.address] = device;
win(device);
}
else if (connectInfo.state == 0) // disconnected
{
internal.connectedDevices[device.address] = null;
// TODO: How to signal disconnect?
// Call error callback?
// Additional callback? (connect, disconnect, fail)
// Additional parameter on win callback with connect state?
// (Last one is the best option I think).
fail && fail('disconnected');
}
},
function(errorCode)
{
fail(errorCode);
});
};
/**
* Obtain device services, them read characteristics and descriptors
* for the services with the given uuid(s).
* If serviceUUIDs is null, info is read for all services.
*/
internal.readServices = function(device, serviceUUIDs, win, fail)
{
// Read services.
evothings.ble.services(
device.deviceHandle,
function(services)
{
// Array that stores services.
device.__services = [];
for (var i = 0; i < services.length; ++i)
{
var service = services[i];
device.__services.push(service);
device.__uuidMap[service.uuid] = service;
}
internal.readCharacteristicsForServices(
device, serviceUUIDs, win, fail);
},
function(errorCode)
{
fail(errorCode);
});
};
/**
* Read characteristics and descriptors for the services with the given uuid(s).
* If serviceUUIDs is null, info for all services are read.
* Internal function.
*/
internal.readCharacteristicsForServices = function(device, serviceUUIDs, win, fail)
{
var characteristicsCallbackFun = function(service)
{
// Array with characteristics for service.
service.__characteristics = [];
return function(characteristics)
{
--readCounter; // Decrements the count added by services.
readCounter += characteristics.length;
for (var i = 0; i < characteristics.length; ++i)
{
var characteristic = characteristics[i];
service.__characteristics.push(characteristic);
device.__uuidMap[characteristic.uuid] = characteristic;
// Read descriptors for characteristic.
evothings.ble.descriptors(
device.deviceHandle,
characteristic.handle,
descriptorsCallbackFun(characteristic),
function(errorCode)
{
fail(errorCode);
});
}
};
};
var descriptorsCallbackFun = function(characteristic)
{
// Array with descriptors for characteristic.
characteristic.__descriptors = [];
return function(descriptors)
{
--readCounter; // Decrements the count added by characteristics.
for (var i = 0; i < descriptors.length; ++i)
{
var descriptor = descriptors[i];
characteristic.__descriptors.push(descriptor);
device.__uuidMap[characteristic.uuid + ':' + descriptor.uuid] = descriptor;
}
if (0 == readCounter)
{
// Everything is read.
win(device);
}
};
};
// Initialize read counter.
readCounter = 0;
if (null != serviceUUIDs)
{
// Read info for service UUIDs.
readCounter = serviceUUIDs.length;
for (var i = 0; i < serviceUUIDs.length; ++i)
{
var uuid = serviceUUIDs[i];
var service = device.__uuidMap[uuid];
if (!service)
{
fail('Service not found: ' + uuid);
return;
}
// Read characteristics for service. Will also read descriptors.
evothings.ble.characteristics(
device.deviceHandle,
service.handle,
characteristicsCallbackFun(service),
function(errorCode)
{
fail(errorCode);
});
}
}
else
{
// Read info for all services.
readCounter = device.__services.length;
for (var i = 0; i < device.__services.length; ++i)
{
// Read characteristics for service. Will also read descriptors.
var service = device.__services[i];
evothings.ble.characteristics(
device.deviceHandle,
service.handle,
characteristicsCallbackFun(service),
function(errorCode)
{
fail(errorCode);
});
}
}
};
internal.readCharacteristic = function(device, characteristicUUID, win, fail)
{
var characteristic = device.__uuidMap[characteristicUUID];
if (!characteristic)
{
fail('Characteristic not found: ' + characteristicUUID);
return;
}
evothings.ble.readCharacteristic(
device.deviceHandle,
characteristic.handle,
win,
fail);
};
internal.readDescriptor = function(device, characteristicUUID, descriptorUUID, win, fail)
{
var descriptor = device.__uuidMap[characteristicUUID + ':' + descriptorUUID];
if (!descriptor)
{
fail('Descriptor not found: ' + descriptorUUID);
return;
}
evothings.ble.readDescriptor(
device.deviceHandle,
descriptor.handle,
value,
function()
{
win();
},
function(errorCode)
{
fail(errorCode);
});
};
internal.writeCharacteristic = function(device, characteristicUUID, value, win, fail)
{
var characteristic = device.__uuidMap[characteristicUUID];
if (!characteristic)
{
fail('Characteristic not found: ' + characteristicUUID);
return;
}
evothings.ble.writeCharacteristic(
device.deviceHandle,
characteristic.handle,
value,
function()
{
win();
},
function(errorCode)
{
fail(errorCode);
});
};
internal.writeDescriptor = function(device, characteristicUUID, descriptorUUID, value, win, fail)
{
var descriptor = device.__uuidMap[characteristicUUID + ':' + descriptorUUID];
if (!descriptor)
{
fail('Descriptor not found: ' + descriptorUUID);
return;
}
evothings.ble.writeDescriptor(
device.deviceHandle,
descriptor.handle,
value,
function()
{
win();
},
function(errorCode)
{
fail(errorCode);
});
};
internal.enableNotification = function(device, characteristicUUID, win, fail)
{
var characteristic = device.__uuidMap[characteristicUUID];
if (!characteristic)
{
fail('Characteristic not found: ' + characteristicUUID);
return;
}
evothings.ble.enableNotification(
device.deviceHandle,
characteristic.handle,
win,
fail);
};
internal.disableNotification = function(device, characteristicUUID, win, fail)
{
var characteristic = device.__uuidMap[characteristicUUID];
if (!characteristic)
{
fail('Characteristic not found: ' + characteristicUUID);
return;
}
evothings.ble.disableNotification(
device.deviceHandle,
characteristic.handle,
win,
fail);
};
// For debugging. Example call:
// easyble.printObject(device, console.log);
easyble.printObject = function(obj, printFun)
{
function print(obj, level)
{
var indent = new Array(level + 1).join(' ');
for (var prop in obj)
{
if (obj.hasOwnProperty(prop))
{
var value = obj[prop];
if (typeof value == 'object')
{
printFun(indent + prop + ':');
print(value, level + 1);
}
else
{
printFun(indent + prop + ': ' + value);
}
}
}
}
print(obj, 0);
};
easyble.reset = function()
{
evothings.ble.reset();
};
return easyble;
})();