UNPKG

15.6 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var plugin_1 = require('./plugin');
9/**
10 * @name BLE
11 * @description
12 * This plugin enables communication between a phone and Bluetooth Low Energy (BLE) peripherals.
13 *
14 * The plugin provides a simple JavaScript API for iOS and Android.
15 *
16 * - Scan for peripherals
17 * - Connect to a peripheral
18 * - Read the value of a characteristic
19 * - Write new value to a characteristic
20 * - Get notified when characteristic's value changes
21 *
22 * Advertising information is returned when scanning for peripherals. Service, characteristic, and property info is returned when connecting to a peripheral. All access is via service and characteristic UUIDs. The plugin manages handles internally.
23 *
24 * Simultaneous connections to multiple peripherals are supported.
25 *
26 * @usage
27 *
28 * ## Peripheral Data
29 *
30 * Peripheral Data is passed to the success callback when scanning and connecting. Limited data is passed when scanning.
31 *
32 * ```typescript
33 * {
34 * "name": "Battery Demo",
35 * "id": "20:FF:D0:FF:D1:C0",
36 * "advertising": [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121],
37 * "rssi": -55
38 * }
39 * ```
40 * After connecting, the peripheral object also includes service, characteristic and descriptor information.
41 *
42 * ```typescript
43 * {
44 * "name": "Battery Demo",
45 * "id": "20:FF:D0:FF:D1:C0",
46 * "advertising": [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121],
47 * "rssi": -55,
48 * "services": [
49 * "1800",
50 * "1801",
51 * "180f"
52 * ],
53 * "characteristics": [
54 * {
55 * "service": "1800",
56 * "characteristic": "2a00",
57 * "properties": [
58 * "Read"
59 * ]
60 * },
61 * {
62 * "service": "1800",
63 * "characteristic": "2a01",
64 * "properties": [
65 * "Read"
66 * ]
67 * },
68 * {
69 * "service": "1801",
70 * "characteristic": "2a05",
71 * "properties": [
72 * "Read"
73 * ]
74 * },
75 * {
76 * "service": "180f",
77 * "characteristic": "2a19",
78 * "properties": [
79 * "Read"
80 * ],
81 * "descriptors": [
82 * {
83 * "uuid": "2901"
84 * },
85 * {
86 * "uuid": "2904"
87 * }
88 * ]
89 * }
90 * ]
91 * }
92 * ```
93 *
94 * ## Advertising Data
95 * Bluetooth advertising data is returned in when scanning for devices. The format format varies depending on your platform. On Android advertising data will be the raw advertising bytes. iOS does not allow access to raw advertising data, so a dictionary of data is returned.
96 *
97 * The advertising information for both Android and iOS appears to be a combination of advertising data and scan response data.
98 *
99 * ### Android
100 *
101 * ```typescript
102 * {
103 * "name": "demo",
104 * "id": "00:1A:7D:DA:71:13",
105 * "advertising": ArrayBuffer,
106 * "rssi": -37
107 * }
108 * ```
109 *
110 * Convert the advertising info to a Uint8Array for processing. `var adData = new Uint8Array(peripheral.advertising)`
111 *
112 * ### iOS
113 *
114 * Note that iOS uses the string value of the constants for the [Advertisement Data Retrieval Keys](https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protocol/index.html#//apple_ref/doc/constant_group/Advertisement_Data_Retrieval_Keys). This will likely change in the future.
115 *
116 * ```typescript
117 * {
118 * "name": "demo",
119 * "id": "D8479A4F-7517-BCD3-91B5-3302B2F81802",
120 * "advertising": {
121 * "kCBAdvDataChannel": 37,
122 * "kCBAdvDataServiceData": {
123 * "FED8": {
124 * "byteLength": 7 // data not shown
125 * }
126 * },
127 * "kCBAdvDataLocalName": "demo",
128 * "kCBAdvDataServiceUUIDs": ["FED8"],
129 * "kCBAdvDataManufacturerData": {
130 * "byteLength": 7 // data not shown
131 * },
132 * "kCBAdvDataTxPowerLevel": 32,
133 * "kCBAdvDataIsConnectable": true
134 * },
135 * "rssi": -53
136 * }
137 * ```
138 *
139 * ## Typed Arrays
140 *
141 * This plugin uses typed Arrays or ArrayBuffers for sending and receiving data.
142 *
143 * This means that you need convert your data to ArrayBuffers before sending and from ArrayBuffers when receiving.
144 *
145 * ```typescript
146 * // ASCII only
147 * function stringToBytes(string) {
148 * var array = new Uint8Array(string.length);
149 * for (var i = 0, l = string.length; i < l; i++) {
150 * array[i] = string.charCodeAt(i);
151 * }
152 * return array.buffer;
153 * }
154 *
155 * // ASCII only
156 * function bytesToString(buffer) {
157 * return String.fromCharCode.apply(null, new Uint8Array(buffer));
158 * }
159 * ```
160 * You can read more about typed arrays in these articles on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) and [HTML5 Rocks](http://www.html5rocks.com/en/tutorials/webgl/typed_arrays/).
161 *
162 * ## UUIDs
163 *
164 * UUIDs are always strings and not numbers. Some 16-bit UUIDs, such as '2220' look like integers, but they're not. (The integer 2220 is 0x8AC in hex.) This isn't a problem with 128 bit UUIDs since they look like strings 82b9e6e1-593a-456f-be9b-9215160ebcac. All 16-bit UUIDs should also be passed to methods as strings.
165 *
166 */
167var BLE = (function () {
168 function BLE() {
169 }
170 /**
171 * Scan and discover BLE peripherals for the specified amount of time.
172 *
173 * @usage
174 * ```
175 * BLE.scan([], 5).subscribe(device => {
176 * console.log(JSON.stringify(device));
177 * });
178 * ```
179 * @param {string[]} services List of service UUIDs to discover, or `[]` to find all devices
180 * @param {number} seconds Number of seconds to run discovery
181 * @returns {Observable<any>} Returns an Observable that notifies of each peripheral that is discovered during the specified time.
182 */
183 BLE.scan = function (services, seconds) { return; };
184 /**
185 * Scan and discover BLE peripherals until `stopScan` is called.
186 *
187 * @usage
188 * ```
189 * BLE.startScan([]).subscribe(device => {
190 * console.log(JSON.stringify(device));
191 * });
192 *
193 * setTimeout(() => {
194 * BLE.stopScan();
195 * }, 5000);
196 * ```
197 * @param {string[]} services List of service UUIDs to discover, or `[]` to find all devices
198 * @returns {Observable<any>} Returns an Observable that notifies of each peripheral discovered.
199 */
200 BLE.startScan = function (services) { return; };
201 /**
202 * Scans for BLE devices. This function operates similarly to the `startScan` function, but allows you to specify extra options (like allowing duplicate device reports).
203 * @param {string[]} services List of service UUIDs to discover, or `[]` to find all devices
204 * @param options {any}
205 * @returns {Observable<any>} Returns an Observable that notifies of each peripheral discovered.
206 */
207 BLE.startScanWithOptions = function (services, options) { return; };
208 /**
209 * Stop a scan started by `startScan`.
210 *
211 * @usage
212 * ```
213 * BLE.startScan([]).subscribe(device => {
214 * console.log(JSON.stringify(device));
215 * });
216 * setTimeout(() => {
217 * BLE.stopScan().then(() => { console.log('scan stopped'); });
218 * }, 5000);
219 * ```
220 * @return returns a Promise.
221 */
222 BLE.stopScan = function () { return; };
223 /**
224 * Connect to a peripheral.
225 * @usage
226 * ```
227 * BLE.connect('12:34:56:78:9A:BC').subscribe(peripheralData => {
228 * console.log(peripheralData);
229 * },
230 * peripheralData => {
231 * console.log('disconnected');
232 * });
233 * ```
234 * @param deviceId {string} UUID or MAC address of the peripheral
235 * @return Returns an Observable that notifies of connect/disconnect.
236 */
237 BLE.connect = function (deviceId) { return; };
238 /**
239 * Disconnect from a peripheral.
240 * @usage
241 * ```
242 * BLE.disconnect('12:34:56:78:9A:BC').then(() => {
243 * console.log('Disconnected');
244 * });
245 * ```
246 * @param deviceId {string} UUID or MAC address of the peripheral
247 * @return Returns a Promise
248 */
249 BLE.disconnect = function (deviceId) { return; };
250 /**
251 * Read the value of a characteristic.
252 *
253 * @param {string} deviceId UUID or MAC address of the peripheral
254 * @param {string} serviceUUID UUID of the BLE service
255 * @param {string} characteristicUUID UUID of the BLE characteristic
256 * @return Returns a Promise
257 */
258 BLE.read = function (deviceId, serviceUUID, characteristicUUID) { return; };
259 ;
260 /**
261 * Write the value of a characteristic.
262 * @usage
263 * ```
264 * // send 1 byte to switch a light on
265 * var data = new Uint8Array(1);
266 * data[0] = 1;
267 * BLE.write(device_id, "FF10", "FF11", data.buffer);
268 *
269 * // send a 3 byte value with RGB color
270 * var data = new Uint8Array(3);
271 * data[0] = 0xFF; // red
272 * data[0] = 0x00; // green
273 * data[0] = 0xFF; // blue
274 * BLE.write(device_id, "ccc0", "ccc1", data.buffer);
275 *
276 * // send a 32 bit integer
277 * var data = new Uint32Array(1);
278 * data[0] = counterInput.value;
279 * BLE.write(device_id, SERVICE, CHARACTERISTIC, data.buffer);
280 *
281 * ```
282 * @param {string} deviceId UUID or MAC address of the peripheral
283 * @param {string} serviceUUID UUID of the BLE service
284 * @param {string} characteristicUUID UUID of the BLE characteristic
285 * @param {ArrayBuffer} value Data to write to the characteristic, as an ArrayBuffer.
286 * @return Returns a Promise
287 */
288 BLE.write = function (deviceId, serviceUUID, characteristicUUID, value) { return; };
289 /**
290 * Write the value of a characteristic without waiting for confirmation from the peripheral.
291 *
292 * @param {string} deviceId UUID or MAC address of the peripheral
293 * @param {string} serviceUUID UUID of the BLE service
294 * @param {string} characteristicUUID UUID of the BLE characteristic
295 * @param {ArrayBuffer} value Data to write to the characteristic, as an ArrayBuffer.
296 * @return Returns a Promise
297 */
298 BLE.writeWithoutResponse = function (deviceId, serviceUUID, characteristicUUID, value) { return; };
299 /**
300 * Register to be notified when the value of a characteristic changes.
301 *
302 * @usage
303 * ```
304 * BLE.startNotification(device_id, "FF10", "FF11").subscribe(buffer => {
305 * console.log(String.fromCharCode.apply(null, new Uint8Array(buffer));
306 * });
307 * ```
308 *
309 * @param {string} deviceId UUID or MAC address of the peripheral
310 * @param {string} serviceUUID UUID of the BLE service
311 * @param {string} characteristicUUID UUID of the BLE characteristic
312 * @return Returns an Observable that notifies of characteristic changes.
313 */
314 BLE.startNotification = function (deviceId, serviceUUID, characteristicUUID) { return; };
315 /**
316 * Stop being notified when the value of a characteristic changes.
317 *
318 * @param {string} deviceId UUID or MAC address of the peripheral
319 * @param {string} serviceUUID UUID of the BLE service
320 * @param {string} characteristicUUID UUID of the BLE characteristic
321 * @returns {Promise<any>}
322 */
323 BLE.stopNotification = function (deviceId, serviceUUID, characteristicUUID) { return; };
324 /**
325 * Report the connection status.
326 *
327 * @usage
328 * ```
329 * BLE.isConnected('FFCA0B09-CB1D-4DC0-A1EF-31AFD3EDFB53').then(
330 * () => { console.log('connected'); },
331 * () => { console.log('not connected'); }
332 * );
333 * ```
334 * @param {string} deviceId UUID or MAC address of the peripheral
335 * @returns {Promise<any>}
336 */
337 BLE.isConnected = function (deviceId) { return; };
338 /**
339 * Report if bluetooth is enabled.
340 *
341 * @returns {Promise<void>} Returns a Promise that resolves if Bluetooth is enabled, and rejects if disabled.
342 */
343 BLE.isEnabled = function () { return; };
344 /**
345 * Open System Bluetooth settings (Android only).
346 *
347 * @returns {Promise<any>}
348 */
349 BLE.showBluetoothSettings = function () { return; };
350 /**
351 * Enable Bluetooth on the device (Android only).
352 *
353 * @returns {Promise<any>}
354 */
355 BLE.enable = function () { return; };
356 __decorate([
357 plugin_1.Cordova({
358 observable: true
359 })
360 ], BLE, "scan", null);
361 __decorate([
362 plugin_1.Cordova({
363 observable: true,
364 clearFunction: 'stopScan',
365 clearWithArgs: false
366 })
367 ], BLE, "startScan", null);
368 __decorate([
369 plugin_1.Cordova({
370 observable: true,
371 clearFunction: 'stopScan',
372 clearWithArgs: false
373 })
374 ], BLE, "startScanWithOptions", null);
375 __decorate([
376 plugin_1.Cordova()
377 ], BLE, "stopScan", null);
378 __decorate([
379 plugin_1.Cordova({
380 observable: true,
381 clearFunction: 'disconnect',
382 clearWithArgs: true
383 })
384 ], BLE, "connect", null);
385 __decorate([
386 plugin_1.Cordova()
387 ], BLE, "disconnect", null);
388 __decorate([
389 plugin_1.Cordova()
390 ], BLE, "read", null);
391 __decorate([
392 plugin_1.Cordova()
393 ], BLE, "write", null);
394 __decorate([
395 plugin_1.Cordova()
396 ], BLE, "writeWithoutResponse", null);
397 __decorate([
398 plugin_1.Cordova({
399 observable: true,
400 clearFunction: 'stopNotification',
401 clearWithArgs: true
402 })
403 ], BLE, "startNotification", null);
404 __decorate([
405 plugin_1.Cordova()
406 ], BLE, "stopNotification", null);
407 __decorate([
408 plugin_1.Cordova()
409 ], BLE, "isConnected", null);
410 __decorate([
411 plugin_1.Cordova()
412 ], BLE, "isEnabled", null);
413 __decorate([
414 plugin_1.Cordova()
415 ], BLE, "showBluetoothSettings", null);
416 __decorate([
417 plugin_1.Cordova()
418 ], BLE, "enable", null);
419 BLE = __decorate([
420 plugin_1.Plugin({
421 pluginName: 'BLE',
422 plugin: 'cordova-plugin-ble-central',
423 pluginRef: 'ble',
424 repo: 'https://github.com/don/cordova-plugin-ble-central',
425 platforms: ['iOS', 'Android']
426 })
427 ], BLE);
428 return BLE;
429}());
430exports.BLE = BLE;
431//# sourceMappingURL=ble.js.map
\No newline at end of file