forked from michalkow/react-native-bluetooth-serial-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
546 lines (488 loc) · 13.7 KB
/
index.d.ts
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Type definitions for react-native-bluetooth-serial-next 1.0.0
// Project: https://github.com/nuttawutmalee/react-native-bluetooth-serial-next
// Definitions by: Nuttawut Malee <https://github.com/nuttawutmalee>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.1.6
import * as ReactNative from "react-native";
import * as React from "react";
export type Buffer = (data: number[]) => void;
/**
* High order component that will
* attach native event emitter and
* send it as a props named subscription.
*
* It will create an emitter when component did mount
* and remove all listeners when component will unmount.
*
* @param options
*/
export function withSubscription(options: {
subscriptionName?: string = "subscription";
destroyOnWillUnmount?: boolean = true;
}): (WrappedComponent: React.Component) => React.Component;
declare namespace BluetoothSerial {
interface CommonDevice extends Object {
id: string;
name: string;
}
interface AndroidBluetoothDevice extends CommonDevice {
address: string;
class: string | undefined;
}
interface iOSBluetoothDevice extends CommonDevice {
uuid: string;
rssi: string | undefined;
}
interface Service extends Object {
name?: string;
service: string;
read: string;
write: string;
}
export const DEFAULT_SERVICES: Array<Service>;
/**
* Prompts user device to enable bluetooth adapter.
*
* @throws this will always throw an error in iOS,
* but in Android, only if the user does not enable
* the bluetooth service upon request.
*/
export function requestEnable(): Promise<boolean>;
/**
* Enable bluetooth adapter service.
*
* @throws this will always throw an error in iOS,
* but in Android, only if bluetooth adapter
* is missing.
*/
export function enable(): Promise<boolean>;
/**
* Disable bluetooth adapter service.
*
* @throws this will always throw an error in iOS,
* but in Android, only if bluetooth adapter
* is missing.
*/
export function disable(): Promise<boolean>;
/**
* Indicates bluetooth adapter service status.
*/
export function isEnabled(): Promise<boolean>;
/**
* Connect to certain bluetooth device / peripheral.
*
* @param id Device id or uuid
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
export function connect(
id: string
): Promise<AndroidBluetoothDevice | iOSBluetoothDevice>;
/**
* Disconnect from connected bluetooth device / peripheral.
*
* @param id Device id or uuid
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
export function disconnect(id?: string): Promise<boolean>;
/**
* Disconnect all connected bluetooth devices.
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
export function disconnectAll(): Promise<boolean>;
/**
* Indicates if you are connected to the active bluetooth device / peripheral or not.
*
* @param id Device id or uuid
*/
export function isConnected(id?: string): Promise<boolean>;
/**
* List all paired (Android) / connected (iOS) bluetooth devices.
*/
export function list(): Promise<
Array<AndroidBluetoothDevice> | Array<iOSBluetoothDevice>
>;
/**
* List all unpaired bluetooth devices.
*
* @alias discoverUnpairedDevices
*
* @throws this will always throws an error in iOS.
*/
export function listUnpaired(): Promise<
Array<AndroidBluetoothDevice> | Array<iOSBluetoothDevice>
>;
/**
* List all unpaired bluetooth devices.
*
* @alias listUnpaired
*
* @throws this will always throws an error in iOS.
*/
export function discoverUnpairedDevices(): Promise<
Array<AndroidBluetoothDevice> | Array<iOSBluetoothDevice>
>;
/**
* Cancel bluetooth device discovery.
*
* @alias stopScanning
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
export function cancelDiscovery(): Promise<boolean>;
/**
* Cancel bluetooth device discovery.
*
* @alias cancelDiscovery
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
export function stopScanning(): Promise<boolean>;
/**
* Pair with certain bluetooth device.
*
* @param id Device id
*
* @throws this will always throw an error in iOS,
* but in Android, only if bluetooth adapter
* is missing.
*/
export function pairDevice(
id: string
): Promise<AndroidBluetoothDevice | iOSBluetoothDevice | null>;
/**
* Unpair from certain bluetooth device.
*
* @param id Device id
*
* @throws this will always throw an error in iOS,
* but in Android, only if bluetooth adapter
* is missing.
*/
export function unpairDevice(
id: string
): Promise<AndroidBluetoothDevice | iOSBluetoothDevice | null>;
/**
* Listen and read data from connected device.
*
* @param callback
* @param delimiter
* @param id
*/
export function read(
callback: (
data: string,
subscription: ReactNative.EmitterSubscription
) => {},
delimiter?: string | null = "",
id?: string
): void;
/**
* Read data from connected device once.
*
* @param delimiter
* @param id
*/
export function readOnce(delimiter?: string | null = "", id?: string): Promise<string>;
/**
* Read data from connected device every n ms.
*
* @param callback
* @param ms
* @param delimiter
* @param id
*/
export function readEvery(
callback: (data: string, intervalId: number) => void,
ms?: number = 1000,
delimiter?: string | null = "",
id?: string
): void;
/**
* Read all buffer data from connected device.
*
* @param id Device id or uuid
*/
export function readFromDevice(id?: string): Promise<string>;
/**
* Read all buffer data up to particular delimiter
* from connected device.
*
* @param delimiter
* @param id Device id or uuid
*/
export function readUntilDelimiter(
delimiter: string,
id?: string
): Promise<string>;
/**
* Write data to device, you can pass string or buffer,
*
* @param data
* @param id Device id or uuid
*/
export function write(data: Buffer | string, id?: string): Promise<boolean>;
/**
* Write string to device.
*
* @param data
* @param id Device id or uuid
*/
export function writeToDevice(data: string, id?: string): Promise<boolean>;
/**
* Clear all buffer data.
*
* @param id Device id or uuid
*/
export function clear(id?: string): Promise<boolean>;
/**
* Get length of buffer data.
*
* @param id Device id or uuid
*/
export function available(id?: string): Promise<number>;
/**
* Set bluetooth adapter a new name.
*
* @param name
*
* @throws this will always throw an error in iOS,
* but in Android, only if bluetooth adapter
* is missing.
*/
export function setAdapterName(name: string): Promise<string>;
/**
* Set delimiter split the buffer data
* when you are reading from device.
*
* @param delimiter
* @param id Device id or uuid
*/
export function withDelimiter(
delimiter: string,
id?: string
): Promise<string>;
/**
* [iOS] Set custom services (read and write characteristics.)
*
* @param services
* @param includeDefaultServices
*
* @throws this will throws an error if each service object in the array
* is invalid; each of them needs to have
* `service`, `read`, and `write` key-value.
*/
export function setServices(
services: Array<Service>,
includeDefaultServices?: boolean = true
): Promise<Array<Service>>;
/**
* [iOS] Get current services (read and write characteristics.)
*/
export function getServices(): Promise<Array<Service>>;
/**
* [iOS] Set current services to the default ones.
*/
export function restoreServices(): Promise<Array<Service>>;
/**
* Select a specific bluetooth device and
* give you the ability to read / write from
* that device.
*
* @param id Device id or uuid
*/
export function device(
id?: string
): {
/**
* Connect to certain bluetooth device / peripheral.
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
connect(): Promise<AndroidBluetoothDevice | iOSBluetoothDevice>;
/**
* Disconnect from the selected bluetooth device / peripheral.
*
* @throws this will throws an error if Android bluetooth adapter
* is missing.
*/
disconnect: () => Promise<boolean>;
/**
* Indicates if you are connected to the selected bluetooth device / peripheral or not.
*/
isConnected: () => Promise<boolean>;
/**
* Clear all buffer data of the selected bluetooth device / peripheral.
*/
clear: () => Promise<boolean>;
/**
* Get length of buffer data from the selected bluetooth device / peripheral.
*/
available: () => Promise<number>;
/**
* Set delimiter split the buffer data
* when you are reading from the selected device.
*
* @param delimiter
*/
withDelimiter: (delimiter: string) => Promise<string>;
/**
* Listen and read data from the selected device.
*
* @param callback
* @param delimiter
*/
read: (
callback: (
data: string,
subscription: ReactNative.EmitterSubscription
) => {},
delimiter?: string | null = ""
) => void;
/**
* Read data from the selected device once.
*
* @param delimiter
*/
readOnce: (delimiter?: string = "") => Promise<string>;
/**
* Read data from the selected device every n ms.
*
* @param callback
* @param ms
* @param delimiter
*/
readEvery: (
callback: (data: string, intervalId: number) => void,
ms?: number = 1000,
delimiter?: string | null = ""
) => void;
/**
* Read all buffer data up to particular delimiter
* from the selected device.
*
* @param delimiter
*/
readUntilDelimiter: (delimiter: string) => Promise<string>;
/**
* Read all buffer data from connected device.
*/
readFromDevice: () => Promise<string>;
/**
* Write data to the selected device, you can pass string or buffer,
* We must convert to base64 in RN there is no way to pass buffer directly.
*
* @param data
*/
write: (data: Buffer | string) => Promise<boolean>;
/**
* Write string to the selected device.
*
* @param data
*/
writeToDevice: (data: string) => Promise<boolean>;
};
/**
* Similar to addListener, except that the listener is removed after it is
* invoked once.
*
* @param eventName - Name of the event to listen to
* @param listener - Function to invoke only once when the
* specified event is emitted
* @param context - Optional context object to use when invoking the
* listener
*/
export function once(
eventName: string,
listener: (...args: any[]) => any,
context: any
): ReactNative.EmitterSubscription;
/**
* Attach listener to a certain event name.
*
* @param eventName - Name of the event to listen to
* @param listener - Function to invoke only once when the
* specified event is emitted
* @param context - Optional context object to use when invoking the
* listener
*/
export function on(
eventName: string,
listener: (...args: any[]) => any,
context?: any
): ReactNative.EmitterSubscription;
/**
* Attach listener to a certain event name.
*
* @param eventName - Name of the event to listen to
* @param listener - Function to invoke only once when the
* specified event is emitted
* @param context - Optional context object to use when invoking the
* listener
*/
export function addListener(
eventName: string,
listener: (...args: any[]) => any,
context?: any
): ReactNative.EmitterSubscription;
/**
* Removes the given listener for event of specific type.
*
* @param eventName - Name of the event to emit
* @param listener - Function to invoke when the specified event is
* emitted
*
* @example
* emitter.removeListener('someEvent', export function(message) {
* console.log(message);
* }); // removes the listener if already registered
*
*/
export function off(
eventName: string,
listener: (...args: any[]) => any
): void;
/**
* Removes the given listener for event of specific type.
*
* @param eventName - Name of the event to emit
* @param listener - Function to invoke when the specified event is
* emitted
*
* @example
* emitter.removeListener('someEvent', export function(message) {
* console.log(message);
* }); // removes the listener if already registered
*
*/
export function removeListener(
eventName: string,
listener: (...args: any[]) => any
): void;
/**
* Removes all of the registered listeners, including those registered as
* listener maps.
*
* @param eventName - Optional name of the event whose registered
* listeners to remove
*/
export function removeAllListeners(eventName?: string): void;
/**
* Removes a specific subscription. Called by the `remove()` method of the
* subscription itself to ensure any necessary cleanup is performed.
*/
export function removeSubscription(
subscription: ReactNative.EmitterSubscription
): void;
}
// tslint:disable-next-line:export-just-namespace
export = BluetoothSerial;
export as namespace BluetoothSerial;