-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
98 lines (81 loc) · 2.68 KB
/
index.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
import { EventEmitter } from 'events';
import { NativeEventEmitter, NativeModules } from 'react-native';
export interface Device {
id: number;
address: string;
name: string;
modelName: string;
manufacturer: string;
}
export interface Reading {
data: string;
source?: Device;
}
export type DATA = 'data';
export type DEVICE_FOUND = 'deviceFound';
export type DEVICE_CONNECTED = 'deviceConnected';
export type DEVICE_DISCONNECTED = 'deviceDisconnected';
export type AMBIGUOUS_DEVICE_FOUND = 'ambiguousDeviceFound';
export type SCAN_FINISHED = 'scanFinished';
export type COLLECTION_FINISHED = 'collectionFinished';
export type DEVICE_EVENTS =
| DEVICE_FOUND
| DEVICE_CONNECTED
| DEVICE_DISCONNECTED
| AMBIGUOUS_DEVICE_FOUND;
export type STATE_EVENTS = SCAN_FINISHED | COLLECTION_FINISHED;
export type EVENTS = DATA | DEVICE_EVENTS | STATE_EVENTS;
const deviceKitModule = NativeModules.RNDeviceKit;
const eventEmitter = new NativeEventEmitter(deviceKitModule);
const { EVENTS } = deviceKitModule;
interface DeviceKit {
on(event: DATA, fn: (reading: Reading) => void): this;
on(event: DEVICE_EVENTS, fn: (device: Device) => void): this;
on(event: STATE_EVENTS, fn: () => void): this;
once(event: DATA, fn: (reading: Reading) => void): this;
once(event: DEVICE_EVENTS, fn: (device: Device) => void): this;
once(event: STATE_EVENTS, fn: () => void): this;
emit(event: DATA, data: Reading): boolean;
emit(event: DEVICE_EVENTS, data: Device): boolean;
emit(event: STATE_EVENTS): boolean;
removeListener(event: EVENTS, fn: (...args: any[]) => void): this;
removeAllListeners(event: EVENTS): this;
}
class DeviceKit extends EventEmitter {
constructor() {
super();
for (const e of EVENTS) {
eventEmitter.addListener(e, d => {
d !== null ? this.emit(e, d) : this.emit(e);
});
}
}
public register(key: string): Promise<void> {
return deviceKitModule.initialize(key);
}
public startScan(): Promise<void> {
return deviceKitModule.startScan();
}
public stopScan(): Promise<void> {
return deviceKitModule.stopScan();
}
public addDevice(device: Device): Promise<void> {
return deviceKitModule.addDevice(device.id);
}
public removeDevice(device: Device): Promise<void> {
return deviceKitModule.removeDevice(device.address);
}
public fetchDevices(): Promise<Device[]> {
return deviceKitModule.listDevices();
}
public cancelPairings(): Promise<void> {
return deviceKitModule.cancelPairings();
}
public startCollection(): Promise<void> {
return deviceKitModule.startCollection();
}
public stopCollection(): Promise<void> {
return deviceKitModule.stopCollection();
}
}
export default DeviceKit;