This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bluetoothLowEnergy.js
263 lines (225 loc) · 8.79 KB
/
bluetoothLowEnergy.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
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var Event = require('cordova-plugin-chrome-apps-common.events');
var exec = require('cordova/exec');
var callbackWithError = require('cordova-plugin-chrome-apps-common.errors').callbackWithError;
var fail = function(callback) {
return callback && function(msg) {
callbackWithError(msg, callback);
};
};
var validateServiceId = function(serviceId) {
var parts = serviceId.split('/');
return parts.length === 2;
};
var validateCharacteristicId = function(characteristicId) {
var parts = characteristicId.split('/');
return parts.length === 3;
};
var validateDescriptorId = function(descriptorId) {
var parts = descriptorId.split('/');
return parts.length === 4;
};
exports.connect = function(deviceAddress, properties, callback) {
if (typeof properties == 'function') {
callback = properties;
properties = {};
}
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'connect', [deviceAddress, properties]);
};
exports.disconnect = function(deviceAddress, callback) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'disconnect', [deviceAddress]);
};
exports.getService = function(serviceId, callback) {
if (validateServiceId(serviceId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'getService', [serviceId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.getServices = (function() {
// A Map stores getServices' callbacks. Key is the deviceAddress, and value
// is an array of callback.
var getServicesCallbacks = {};
return function(deviceAddress, callback) {
if (getServicesCallbacks[deviceAddress] !== undefined) {
getServicesCallbacks[deviceAddress].push(callback);
} else {
getServicesCallbacks[deviceAddress] = [callback];
var handleWin = function(arg) {
getServicesCallbacks[deviceAddress].forEach(function(callback) {
callback(arg);
});
delete getServicesCallbacks[deviceAddress];
};
var handleFail = function(arg) {
getServicesCallbacks[deviceAddress].forEach(function(callback) {
(fail(callback)(arg));
});
delete getServicesCallbacks[deviceAddress];
};
exec(handleWin, handleFail, 'ChromeBluetoothLowEnergy', 'getServices', [deviceAddress]);
}
};
}());
exports.getCharacteristic = function(characteristicId, callback) {
var win = callback && function(uuid, service, properties, instanceId, value) {
var info = {
uuid: uuid,
service: service,
properties: properties,
instanceId: instanceId,
value: value
};
callback(info);
};
if (validateCharacteristicId(characteristicId)) {
exec(win, fail(callback), 'ChromeBluetoothLowEnergy', 'getCharacteristic', [characteristicId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.getCharacteristics = function(serviceId, callback) {
if (validateServiceId(serviceId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'getCharacteristics', [serviceId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.getIncludedServices = function(serviceId, callback) {
if (validateServiceId(serviceId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'getIncludedServices', [serviceId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.getDescriptor = function(descriptorId, callback) {
var win = callback && function(uuid, characteristic, instanceId, value) {
var info = {
uuid: uuid,
characteristic: characteristic,
instanceId: instanceId,
value: value
};
callback(info);
};
if (validateDescriptorId(descriptorId)) {
exec(win, fail(callback), 'ChromeBluetoothLowEnergy', 'getDescriptor', [descriptorId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.getDescriptors = function(characteristicId, callback) {
if (validateCharacteristicId(characteristicId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'getDescriptors', [characteristicId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.readCharacteristicValue = function(characteristicId, callback) {
var win = callback && function(uuid, service, properties, instanceId, value) {
var info = {
uuid: uuid,
service: service,
properties: properties,
instanceId: instanceId,
value: value
};
callback(info);
};
if (validateCharacteristicId(characteristicId)) {
exec(win, fail(callback), 'ChromeBluetoothLowEnergy', 'readCharacteristicValue', [characteristicId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.writeCharacteristicValue = function(characteristicId, value, callback) {
if (validateCharacteristicId(characteristicId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'writeCharacteristicValue', [characteristicId, value]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.startCharacteristicNotifications = function(characteristicId, properties, callback) {
if (typeof properties == 'function') {
callback = properties;
properties = {};
}
if (validateCharacteristicId(characteristicId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'startCharacteristicNotifications', [characteristicId, properties]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.stopCharacteristicNotifications = function(characteristicId, callback) {
if (validateCharacteristicId(characteristicId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'stopCharacteristicNotifications', [characteristicId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.readDescriptorValue = function(descriptorId, callback) {
var win = callback && function(uuid, characteristic, instanceId, value) {
var info = {
uuid: uuid,
characteristic: characteristic,
instanceId: instanceId,
value: value
};
callback(info);
};
if (validateDescriptorId(descriptorId)) {
exec(win, fail(callback), 'ChromeBluetoothLowEnergy', 'readDescriptorValue', [descriptorId]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.writeDescriptorValue = function(descriptorId, value, callback) {
if (validateDescriptorId(descriptorId)) {
exec(callback, fail(callback), 'ChromeBluetoothLowEnergy', 'writeDescriptorValue', [descriptorId, value]);
} else {
callbackWithError('Invalid instanceId', callback);
}
};
exports.onServiceAdded = new Event('onServiceAdded');
exports.onServiceChanged = new Event('onServiceChanged');
exports.onServiceRemoved = new Event('onServiceRemoved');
exports.onCharacteristicValueChanged = new Event('onCharacteristicValueChanged');
exports.onDescriptorValueChanged = new Event('onDescriptorValueChanged');
function registerEvents() {
var onEventsCallback = function(eventType) {
switch (eventType) {
case 'onServiceAdded':
exports.onServiceAdded.fire(arguments[1]);
break;
case 'onServiceChanged':
exports.onServiceChanged.fire(arguments[1]);
break;
case 'onServiceRemoved':
exports.onServiceChanged.fire(arguments[1]);
break;
case 'onCharacteristicValueChanged':
var info = {
uuid: arguments[1],
service: arguments[2],
properties: arguments[3],
instanceId: arguments[4],
value: arguments[5]
};
exports.onCharacteristicValueChanged.fire(info);
break;
case 'onDescriptorValueChanged':
var info = {
uuid: arguments[1],
characteristic: arguments[2],
instanceId: arguments[3],
value:arguments[4]
};
exports.onDescriptorValueChanged.fire(info);
break;
}
};
exec(onEventsCallback, null, 'ChromeBluetoothLowEnergy', 'registerBluetoothLowEnergyEvents', []);
}
require('cordova-plugin-chrome-apps-common.helpers').runAtStartUp(registerEvents);