UNPKG

13.1 kBTypeScriptView Raw
1import { Observable } from 'rxjs/Observable';
2/**
3 * @name BLE
4 * @description
5 * This plugin enables communication between a phone and Bluetooth Low Energy (BLE) peripherals.
6 *
7 * The plugin provides a simple JavaScript API for iOS and Android.
8 *
9 * - Scan for peripherals
10 * - Connect to a peripheral
11 * - Read the value of a characteristic
12 * - Write new value to a characteristic
13 * - Get notified when characteristic's value changes
14 *
15 * 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.
16 *
17 * Simultaneous connections to multiple peripherals are supported.
18 *
19 * @usage
20 *
21 * ## Peripheral Data
22 *
23 * Peripheral Data is passed to the success callback when scanning and connecting. Limited data is passed when scanning.
24 *
25 * ```typescript
26 * {
27 * "name": "Battery Demo",
28 * "id": "20:FF:D0:FF:D1:C0",
29 * "advertising": [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121],
30 * "rssi": -55
31 * }
32 * ```
33 * After connecting, the peripheral object also includes service, characteristic and descriptor information.
34 *
35 * ```typescript
36 * {
37 * "name": "Battery Demo",
38 * "id": "20:FF:D0:FF:D1:C0",
39 * "advertising": [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121],
40 * "rssi": -55,
41 * "services": [
42 * "1800",
43 * "1801",
44 * "180f"
45 * ],
46 * "characteristics": [
47 * {
48 * "service": "1800",
49 * "characteristic": "2a00",
50 * "properties": [
51 * "Read"
52 * ]
53 * },
54 * {
55 * "service": "1800",
56 * "characteristic": "2a01",
57 * "properties": [
58 * "Read"
59 * ]
60 * },
61 * {
62 * "service": "1801",
63 * "characteristic": "2a05",
64 * "properties": [
65 * "Read"
66 * ]
67 * },
68 * {
69 * "service": "180f",
70 * "characteristic": "2a19",
71 * "properties": [
72 * "Read"
73 * ],
74 * "descriptors": [
75 * {
76 * "uuid": "2901"
77 * },
78 * {
79 * "uuid": "2904"
80 * }
81 * ]
82 * }
83 * ]
84 * }
85 * ```
86 *
87 * ## Advertising Data
88 * 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.
89 *
90 * The advertising information for both Android and iOS appears to be a combination of advertising data and scan response data.
91 *
92 * ### Android
93 *
94 * ```typescript
95 * {
96 * "name": "demo",
97 * "id": "00:1A:7D:DA:71:13",
98 * "advertising": ArrayBuffer,
99 * "rssi": -37
100 * }
101 * ```
102 *
103 * Convert the advertising info to a Uint8Array for processing. `var adData = new Uint8Array(peripheral.advertising)`
104 *
105 * ### iOS
106 *
107 * 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.
108 *
109 * ```typescript
110 * {
111 * "name": "demo",
112 * "id": "D8479A4F-7517-BCD3-91B5-3302B2F81802",
113 * "advertising": {
114 * "kCBAdvDataChannel": 37,
115 * "kCBAdvDataServiceData": {
116 * "FED8": {
117 * "byteLength": 7 // data not shown
118 * }
119 * },
120 * "kCBAdvDataLocalName": "demo",
121 * "kCBAdvDataServiceUUIDs": ["FED8"],
122 * "kCBAdvDataManufacturerData": {
123 * "byteLength": 7 // data not shown
124 * },
125 * "kCBAdvDataTxPowerLevel": 32,
126 * "kCBAdvDataIsConnectable": true
127 * },
128 * "rssi": -53
129 * }
130 * ```
131 *
132 * ## Typed Arrays
133 *
134 * This plugin uses typed Arrays or ArrayBuffers for sending and receiving data.
135 *
136 * This means that you need convert your data to ArrayBuffers before sending and from ArrayBuffers when receiving.
137 *
138 * ```typescript
139 * // ASCII only
140 * function stringToBytes(string) {
141 * var array = new Uint8Array(string.length);
142 * for (var i = 0, l = string.length; i < l; i++) {
143 * array[i] = string.charCodeAt(i);
144 * }
145 * return array.buffer;
146 * }
147 *
148 * // ASCII only
149 * function bytesToString(buffer) {
150 * return String.fromCharCode.apply(null, new Uint8Array(buffer));
151 * }
152 * ```
153 * 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/).
154 *
155 * ## UUIDs
156 *
157 * 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.
158 *
159 */
160export declare class BLE {
161 /**
162 * Scan and discover BLE peripherals for the specified amount of time.
163 *
164 * @usage
165 * ```
166 * BLE.scan([], 5).subscribe(device => {
167 * console.log(JSON.stringify(device));
168 * });
169 * ```
170 * @param {string[]} services List of service UUIDs to discover, or `[]` to find all devices
171 * @param {number} seconds Number of seconds to run discovery
172 * @returns {Observable<any>} Returns an Observable that notifies of each peripheral that is discovered during the specified time.
173 */
174 static scan(services: string[], seconds: number): Observable<any>;
175 /**
176 * Scan and discover BLE peripherals until `stopScan` is called.
177 *
178 * @usage
179 * ```
180 * BLE.startScan([]).subscribe(device => {
181 * console.log(JSON.stringify(device));
182 * });
183 *
184 * setTimeout(() => {
185 * BLE.stopScan();
186 * }, 5000);
187 * ```
188 * @param {string[]} services List of service UUIDs to discover, or `[]` to find all devices
189 * @returns {Observable<any>} Returns an Observable that notifies of each peripheral discovered.
190 */
191 static startScan(services: string[]): Observable<any>;
192 /**
193 * Scans for BLE devices. This function operates similarly to the `startScan` function, but allows you to specify extra options (like allowing duplicate device reports).
194 * @param {string[]} services List of service UUIDs to discover, or `[]` to find all devices
195 * @param options {any}
196 * @returns {Observable<any>} Returns an Observable that notifies of each peripheral discovered.
197 */
198 static startScanWithOptions(services: string[], options: {
199 reportDuplicates?: boolean;
200 } | any): Observable<any>;
201 /**
202 * Stop a scan started by `startScan`.
203 *
204 * @usage
205 * ```
206 * BLE.startScan([]).subscribe(device => {
207 * console.log(JSON.stringify(device));
208 * });
209 * setTimeout(() => {
210 * BLE.stopScan().then(() => { console.log('scan stopped'); });
211 * }, 5000);
212 * ```
213 * @return returns a Promise.
214 */
215 static stopScan(): Promise<any>;
216 /**
217 * Connect to a peripheral.
218 * @usage
219 * ```
220 * BLE.connect('12:34:56:78:9A:BC').subscribe(peripheralData => {
221 * console.log(peripheralData);
222 * },
223 * peripheralData => {
224 * console.log('disconnected');
225 * });
226 * ```
227 * @param deviceId {string} UUID or MAC address of the peripheral
228 * @return Returns an Observable that notifies of connect/disconnect.
229 */
230 static connect(deviceId: string): Observable<any>;
231 /**
232 * Disconnect from a peripheral.
233 * @usage
234 * ```
235 * BLE.disconnect('12:34:56:78:9A:BC').then(() => {
236 * console.log('Disconnected');
237 * });
238 * ```
239 * @param deviceId {string} UUID or MAC address of the peripheral
240 * @return Returns a Promise
241 */
242 static disconnect(deviceId: string): Promise<any>;
243 /**
244 * Read the value of a characteristic.
245 *
246 * @param {string} deviceId UUID or MAC address of the peripheral
247 * @param {string} serviceUUID UUID of the BLE service
248 * @param {string} characteristicUUID UUID of the BLE characteristic
249 * @return Returns a Promise
250 */
251 static read(deviceId: string, serviceUUID: string, characteristicUUID: string): Promise<any>;
252 /**
253 * Write the value of a characteristic.
254 * @usage
255 * ```
256 * // send 1 byte to switch a light on
257 * var data = new Uint8Array(1);
258 * data[0] = 1;
259 * BLE.write(device_id, "FF10", "FF11", data.buffer);
260 *
261 * // send a 3 byte value with RGB color
262 * var data = new Uint8Array(3);
263 * data[0] = 0xFF; // red
264 * data[0] = 0x00; // green
265 * data[0] = 0xFF; // blue
266 * BLE.write(device_id, "ccc0", "ccc1", data.buffer);
267 *
268 * // send a 32 bit integer
269 * var data = new Uint32Array(1);
270 * data[0] = counterInput.value;
271 * BLE.write(device_id, SERVICE, CHARACTERISTIC, data.buffer);
272 *
273 * ```
274 * @param {string} deviceId UUID or MAC address of the peripheral
275 * @param {string} serviceUUID UUID of the BLE service
276 * @param {string} characteristicUUID UUID of the BLE characteristic
277 * @param {ArrayBuffer} value Data to write to the characteristic, as an ArrayBuffer.
278 * @return Returns a Promise
279 */
280 static write(deviceId: string, serviceUUID: string, characteristicUUID: string, value: ArrayBuffer): Promise<any>;
281 /**
282 * Write the value of a characteristic without waiting for confirmation from the peripheral.
283 *
284 * @param {string} deviceId UUID or MAC address of the peripheral
285 * @param {string} serviceUUID UUID of the BLE service
286 * @param {string} characteristicUUID UUID of the BLE characteristic
287 * @param {ArrayBuffer} value Data to write to the characteristic, as an ArrayBuffer.
288 * @return Returns a Promise
289 */
290 static writeWithoutResponse(deviceId: string, serviceUUID: string, characteristicUUID: string, value: ArrayBuffer): Promise<any>;
291 /**
292 * Register to be notified when the value of a characteristic changes.
293 *
294 * @usage
295 * ```
296 * BLE.startNotification(device_id, "FF10", "FF11").subscribe(buffer => {
297 * console.log(String.fromCharCode.apply(null, new Uint8Array(buffer));
298 * });
299 * ```
300 *
301 * @param {string} deviceId UUID or MAC address of the peripheral
302 * @param {string} serviceUUID UUID of the BLE service
303 * @param {string} characteristicUUID UUID of the BLE characteristic
304 * @return Returns an Observable that notifies of characteristic changes.
305 */
306 static startNotification(deviceId: string, serviceUUID: string, characteristicUUID: string): Observable<any>;
307 /**
308 * Stop being notified when the value of a characteristic changes.
309 *
310 * @param {string} deviceId UUID or MAC address of the peripheral
311 * @param {string} serviceUUID UUID of the BLE service
312 * @param {string} characteristicUUID UUID of the BLE characteristic
313 * @returns {Promise<any>}
314 */
315 static stopNotification(deviceId: string, serviceUUID: string, characteristicUUID: string): Promise<any>;
316 /**
317 * Report the connection status.
318 *
319 * @usage
320 * ```
321 * BLE.isConnected('FFCA0B09-CB1D-4DC0-A1EF-31AFD3EDFB53').then(
322 * () => { console.log('connected'); },
323 * () => { console.log('not connected'); }
324 * );
325 * ```
326 * @param {string} deviceId UUID or MAC address of the peripheral
327 * @returns {Promise<any>}
328 */
329 static isConnected(deviceId: string): Promise<any>;
330 /**
331 * Report if bluetooth is enabled.
332 *
333 * @returns {Promise<void>} Returns a Promise that resolves if Bluetooth is enabled, and rejects if disabled.
334 */
335 static isEnabled(): Promise<void>;
336 /**
337 * Open System Bluetooth settings (Android only).
338 *
339 * @returns {Promise<any>}
340 */
341 static showBluetoothSettings(): Promise<any>;
342 /**
343 * Enable Bluetooth on the device (Android only).
344 *
345 * @returns {Promise<any>}
346 */
347 static enable(): Promise<any>;
348}