react-native-flic2 2.0.0-alpha.39 → 2.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,360 @@
1
+ import { TypedEmitter } from './lib/typedEventEmitter';
2
+ import NativeFlic2, {
3
+ type ButtonEvent,
4
+ type FlicButton,
5
+ type LatencyModeType,
6
+ type ManagerStateChangeEvent,
7
+ type ScanStatusChangeEvent,
8
+ type TriggerModeType,
9
+ } from './NativeFlic2';
10
+
11
+ class Flic2 {
12
+
13
+ private isFlic2ManagerInitialized: boolean = false;
14
+
15
+ public eventEmitter: TypedEmitter<{
16
+ buttonEvent: (event: ButtonEvent) => void;
17
+ managerStateChange: (event: ManagerStateChangeEvent) => void;
18
+ scanStatusChange: (event: ScanStatusChangeEvent) => void;
19
+ }>;
20
+
21
+ /**
22
+ * Constructor.
23
+ *
24
+ * @class
25
+ * @version 2.0.0
26
+ */
27
+ constructor() {
28
+
29
+ // create event emitter
30
+ this.eventEmitter = new TypedEmitter<{
31
+ buttonEvent: (event: ButtonEvent) => void;
32
+ managerStateChange: (event: ManagerStateChangeEvent) => void;
33
+ scanStatusChange: (event: ScanStatusChangeEvent) => void;
34
+ }>();
35
+
36
+ // listen
37
+ NativeFlic2.onButtonEvent(this.onNativeButtonEvent.bind(this));
38
+ NativeFlic2.onManagerStateChange(
39
+ this.onNativeManagerStateChange.bind(this)
40
+ );
41
+
42
+ NativeFlic2.onScanStatusChange(this.onNativeScanStatusChange.bind(this));
43
+
44
+ }
45
+
46
+ // MARK: Public management methods
47
+ /**
48
+ * Initialize the Flic2 manager.
49
+ *
50
+ * @returns A promise that resolves when the Flic2 manager is initialized.
51
+ */
52
+ public async initialize(): Promise<void> {
53
+
54
+ // check if the Flic2 manager is already initialized
55
+ if (this.isInitialized()) {
56
+
57
+ throw new Error('Flic2 manager is already initialized');
58
+
59
+ }
60
+
61
+ // initialize the Flic2 manager in background
62
+ await NativeFlic2.initialize(true);
63
+
64
+ this.onInitialized();
65
+
66
+ }
67
+
68
+ /**
69
+ * Scan for buttons.
70
+ *
71
+ * @returns A promise that resolves when the scan is started. Events will be emitted for the scan process.
72
+ */
73
+ public startScan(): Promise<void> {
74
+
75
+ return NativeFlic2.scanForButtons();
76
+
77
+ }
78
+
79
+ /**
80
+ * Stop the scan.
81
+ *
82
+ * @returns A promise that resolves when the scan is stopped.
83
+ */
84
+ public stopScan(): Promise<void> {
85
+
86
+ return NativeFlic2.stopScan();
87
+
88
+ }
89
+
90
+ /**
91
+ * Called when the Flic2 manager is initialized.
92
+ */
93
+ public onInitialized(): void {
94
+
95
+ this.isFlic2ManagerInitialized = true;
96
+
97
+ }
98
+
99
+ /**
100
+ * Check if the Flic2 manager is initialized.
101
+ *
102
+ * @returns True if the Flic2 manager is initialized, false otherwise.
103
+ */
104
+ public isInitialized(): boolean {
105
+
106
+ return this.isFlic2ManagerInitialized;
107
+
108
+ }
109
+
110
+ /**
111
+ * Connect all known buttons.
112
+ *
113
+ * @returns A promise that resolves when the all known buttons are connected.
114
+ */
115
+ public connectAllKnownButtons(): Promise<void> {
116
+
117
+ return NativeFlic2.connectAllKnownButtons();
118
+
119
+ }
120
+
121
+ /**
122
+ * Disconnect all known buttons.
123
+ *
124
+ * @returns A promise that resolves when the all known buttons are disconnected.
125
+ */
126
+ public disconnectAllKnownButtons(): Promise<void> {
127
+
128
+ return NativeFlic2.disconnectAllKnownButtons();
129
+
130
+ }
131
+
132
+ /**
133
+ * Forget all buttons.
134
+ *
135
+ * @returns A promise that resolves when the all buttons are forgotten.
136
+ */
137
+ public forgetAllButtons(): Promise<void> {
138
+
139
+ return NativeFlic2.forgetAllButtons();
140
+
141
+ }
142
+
143
+ /**
144
+ * Check if a scan is currently running.
145
+ *
146
+ * @returns A promise that resolves to true if scanning, false otherwise.
147
+ */
148
+ public isScanning(): Promise<boolean> {
149
+
150
+ return NativeFlic2.isScanning();
151
+
152
+ }
153
+
154
+ /**
155
+ * Forget a specific button by UUID.
156
+ *
157
+ * @param uuid - The UUID of the button to forget.
158
+ * @returns A promise that resolves when the button is forgotten.
159
+ */
160
+ public forgetButton(
161
+ uuid: string
162
+ ): Promise<void> {
163
+
164
+ return NativeFlic2.forgetButton(uuid);
165
+
166
+ }
167
+
168
+ // MARK: Public button methods
169
+ /**
170
+ * Connect a button.
171
+ *
172
+ * @param uuid - The UUID of the button to connect.
173
+ * @returns A promise that resolves with the button when the connection is initiated.
174
+ */
175
+ public buttonConnect(
176
+ uuid: string
177
+ ): Promise<FlicButton> {
178
+
179
+ return NativeFlic2.connectButton(uuid);
180
+
181
+ }
182
+
183
+ /**
184
+ * Disconnect a button.
185
+ *
186
+ * @param uuid - The UUID of the button to disconnect.
187
+ * @returns A promise that resolves with the button when the disconnection is initiated.
188
+ */
189
+ public buttonDisconnect(
190
+ uuid: string
191
+ ): Promise<FlicButton> {
192
+
193
+ return NativeFlic2.disconnectButton(uuid);
194
+
195
+ }
196
+
197
+ /**
198
+ * Set the trigger mode of a button.
199
+ *
200
+ * @param uuid - The UUID of the button to set the trigger mode of.
201
+ * @param mode - The trigger mode to set.
202
+ * @returns A promise that resolves with the button when the trigger mode is set.
203
+ */
204
+ public buttonSetTriggerMode(
205
+ uuid: string,
206
+ mode: TriggerModeType
207
+ ): Promise<FlicButton> {
208
+
209
+ return NativeFlic2.setTriggerMode(uuid, mode);
210
+
211
+ }
212
+
213
+ /**
214
+ * Set the latency mode of a button.
215
+ *
216
+ * @param uuid - The UUID of the button to set the latency mode of.
217
+ * @param mode - The latency mode to set.
218
+ * @returns A promise that resolves with the button when the latency mode is set.
219
+ */
220
+ public buttonSetLatencyMode(
221
+ uuid: string,
222
+ mode: LatencyModeType
223
+ ): Promise<FlicButton> {
224
+
225
+ return NativeFlic2.setLatencyMode(uuid, mode);
226
+
227
+ }
228
+
229
+ /**
230
+ * Set the nickname of a button.
231
+ *
232
+ * @param uuid - The UUID of the button to set the nickname of.
233
+ * @param nickname - The nickname to set.
234
+ * @returns A promise that resolves with the button when the nickname is set.
235
+ */
236
+ public buttonSetNickname(
237
+ uuid: string,
238
+ nickname: string
239
+ ): Promise<FlicButton> {
240
+
241
+ return NativeFlic2.setNickname(uuid, nickname);
242
+
243
+ }
244
+
245
+ /**
246
+ * Get all buttons.
247
+ *
248
+ * @returns A promise that resolves with an array of FlicButton instances.
249
+ */
250
+ public getButtons(): Promise<FlicButton[]> {
251
+
252
+ return NativeFlic2.getButtons();
253
+
254
+ }
255
+
256
+ /**
257
+ * Get a button by UUID.
258
+ *
259
+ * @param uuid - The UUID of the button to get.
260
+ * @returns A promise that resolves with the FlicButton instance or null if the button is not found.
261
+ */
262
+ public async getButton(uuid: string): Promise<FlicButton | null> {
263
+
264
+ const buttons = await NativeFlic2.getButtons();
265
+ const button = buttons.find((item: FlicButton) => item.uuid === uuid);
266
+
267
+ return button ?? null;
268
+
269
+ }
270
+
271
+ /**
272
+ * Get the battery health status of a button.
273
+ *
274
+ * @param uuid - The UUID of the button to check.
275
+ * @returns A promise that resolves to true if battery is OK (voltage > 2.65V), false otherwise.
276
+ * @throws Error if the button is not found.
277
+ */
278
+ public async getBatteryHealth(uuid: string): Promise<boolean> {
279
+
280
+ const button = await this.getButton(uuid);
281
+
282
+ if (!button) {
283
+
284
+ throw new Error(`Button with UUID ${uuid} not found`);
285
+
286
+ }
287
+
288
+ return this.isBatteryVoltageOk(button.batteryVoltage);
289
+
290
+ }
291
+
292
+ // MARK: Private Methods
293
+ /**
294
+ * Check if battery voltage is OK based on the 2.65V threshold.
295
+ *
296
+ * @param voltage - The battery voltage in volts.
297
+ * @returns True if voltage is above 2.65V (battery is OK), false otherwise.
298
+ */
299
+ private isBatteryVoltageOk(voltage: number): boolean {
300
+
301
+ return voltage * 1000 > 2650;
302
+
303
+ }
304
+
305
+ /**
306
+ * Called when a button event is received from the native side.
307
+ *
308
+ * @param event - The button event.
309
+ */
310
+ private onNativeButtonEvent(event: ButtonEvent): void {
311
+
312
+ // Enrich batteryUpdate events with batteryVoltageOk
313
+ if (event.event === 'batteryUpdate') {
314
+
315
+ const voltage = typeof event.voltage === 'number' ? event.voltage : event.button?.batteryVoltage;
316
+
317
+ const enrichedEvent: ButtonEvent = {
318
+ ...event,
319
+ batteryVoltageOk: this.isBatteryVoltageOk(voltage ?? 0),
320
+ };
321
+
322
+ this.eventEmitter.emit('buttonEvent', enrichedEvent);
323
+
324
+ return;
325
+
326
+ }
327
+
328
+ this.eventEmitter.emit('buttonEvent', event);
329
+
330
+ }
331
+
332
+ /**
333
+ * Called when a manager state change event is received from the native side.
334
+ *
335
+ * @param event - The manager state change event.
336
+ */
337
+ private onNativeManagerStateChange(event: ManagerStateChangeEvent): void {
338
+
339
+ this.eventEmitter.emit('managerStateChange', event);
340
+
341
+ }
342
+
343
+ /**
344
+ * Called when a scan status change event is received from the native side.
345
+ *
346
+ * @param event - The scan status change event.
347
+ */
348
+ private onNativeScanStatusChange(event: ScanStatusChangeEvent): void {
349
+
350
+ this.eventEmitter.emit('scanStatusChange', event);
351
+
352
+ }
353
+
354
+ }
355
+
356
+ // export as singleton
357
+ export default new Flic2();
358
+
359
+ // re-export types
360
+ export type * from './NativeFlic2';
@@ -0,0 +1,63 @@
1
+ type EventMap = Record<string, (...args: any[]) => void>;
2
+
3
+ export class TypedEmitter<E extends EventMap> {
4
+
5
+ private listeners: { [K in keyof E]?: Set<E[K]> } = {};
6
+
7
+ on<K extends keyof E>(event: K, fn: E[K]): { remove: () => void } {
8
+
9
+ (this.listeners[event] ??= new Set()).add(fn);
10
+
11
+ // return shorthand for removing the listener
12
+ return { remove: () => this.off(event, fn) };
13
+
14
+ }
15
+
16
+ off<K extends keyof E>(event: K, fn: E[K]): this {
17
+
18
+ this.listeners[event]?.delete(fn);
19
+ return this;
20
+
21
+ }
22
+
23
+ once<K extends keyof E>(event: K, fn: E[K]): { remove: () => void } {
24
+
25
+ const wrapped: E[K] = ((...args: Parameters<E[K]>) => {
26
+
27
+ this.off(event, wrapped);
28
+ fn(...args);
29
+
30
+ }) as E[K];
31
+
32
+ return this.on(event, wrapped);
33
+
34
+ }
35
+
36
+ emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): boolean {
37
+
38
+ const ls = this.listeners[event];
39
+
40
+ if (!ls || ls.size === 0) {
41
+
42
+ return false;
43
+
44
+ }
45
+
46
+ for (const fn of [...ls]) {
47
+
48
+ fn(...args);
49
+
50
+ }
51
+
52
+ return true;
53
+
54
+ }
55
+
56
+ clear(): this {
57
+
58
+ this.listeners = {};
59
+ return this;
60
+
61
+ }
62
+
63
+ }
@@ -1,112 +0,0 @@
1
- package com.flic2
2
-
3
- import android.app.Notification
4
- import android.app.NotificationChannel
5
- import android.app.NotificationManager
6
- import android.app.PendingIntent
7
- import android.app.Service
8
- import android.content.Intent
9
- import android.os.Binder
10
- import android.os.Build
11
- import android.os.Handler
12
- import android.os.IBinder
13
- import android.os.Looper
14
- import android.util.Log
15
- import androidx.core.app.NotificationCompat
16
- import io.flic.flic2libandroid.Flic2Manager
17
-
18
- class Flic2Service : Service() {
19
-
20
- private val binder = Flic2ServiceBinder()
21
- private var manager: Flic2Manager? = null
22
-
23
- companion object {
24
- private const val TAG = "Flic2Service"
25
- private const val NOTIFICATION_ID = 1
26
- private const val CHANNEL_ID = "Flic2ServiceChannel"
27
- private const val CHANNEL_NAME = "Flic2 Service"
28
- }
29
-
30
- inner class Flic2ServiceBinder : Binder() {
31
- fun getService(): Flic2Service = this@Flic2Service
32
- }
33
-
34
- override fun onCreate() {
35
- super.onCreate()
36
- Log.d(TAG, "Service onCreate")
37
-
38
- try {
39
- // Initialize Flic2Manager on main thread with Handler
40
- // v1.1.0 API: init() returns void, must call getInstance() after
41
- Flic2Manager.init(
42
- applicationContext,
43
- Handler(Looper.getMainLooper())
44
- )
45
- manager = Flic2Manager.getInstance()
46
- Log.d(TAG, "Flic2Manager initialized successfully")
47
- } catch (e: Exception) {
48
- Log.e(TAG, "Failed to initialize Flic2Manager", e)
49
- }
50
- }
51
-
52
- override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
53
- Log.d(TAG, "Service onStartCommand")
54
-
55
- // Create notification channel for Android O and above
56
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
57
- val channel = NotificationChannel(
58
- CHANNEL_ID,
59
- CHANNEL_NAME,
60
- NotificationManager.IMPORTANCE_LOW
61
- ).apply {
62
- description = "Keeps Flic2 buttons connected in the background"
63
- setShowBadge(false)
64
- }
65
-
66
- val notificationManager = getSystemService(NotificationManager::class.java)
67
- notificationManager.createNotificationChannel(channel)
68
- }
69
-
70
- // Create notification
71
- val notification = createNotification()
72
-
73
- // Start as foreground service
74
- startForeground(NOTIFICATION_ID, notification)
75
-
76
- return START_STICKY
77
- }
78
-
79
- override fun onBind(intent: Intent?): IBinder {
80
- Log.d(TAG, "Service onBind")
81
- return binder
82
- }
83
-
84
- override fun onDestroy() {
85
- Log.d(TAG, "Service onDestroy")
86
- super.onDestroy()
87
- }
88
-
89
- fun getManager(): Flic2Manager? = manager
90
-
91
- fun isManagerInitialized(): Boolean = manager != null
92
-
93
- private fun createNotification(): Notification {
94
- val notificationIntent = Intent(this, Flic2Service::class.java)
95
- val pendingIntent = PendingIntent.getActivity(
96
- this,
97
- 0,
98
- notificationIntent,
99
- PendingIntent.FLAG_IMMUTABLE
100
- )
101
-
102
- return NotificationCompat.Builder(this, CHANNEL_ID)
103
- .setContentTitle("Flic2 Service")
104
- .setContentText("Flic2 buttons are connected")
105
- .setSmallIcon(android.R.drawable.ic_dialog_info)
106
- .setContentIntent(pendingIntent)
107
- .setPriority(NotificationCompat.PRIORITY_LOW)
108
- .setOngoing(true)
109
- .build()
110
- }
111
- }
112
-
package/src/index.tsx DELETED
@@ -1,159 +0,0 @@
1
- import type { CodegenTypes } from 'react-native';
2
- import type {
3
- MultiplyEvent,
4
- ManagerStateChangeEvent,
5
- ScanStatusChangeEvent,
6
- ButtonEvent,
7
- FlicButton,
8
- TriggerModeType,
9
- LatencyModeType,
10
- } from './NativeFlic2';
11
- import NativeFlic2 from './NativeFlic2';
12
-
13
- // MARK: - Example Functions (keeping for compatibility)
14
-
15
- export function multiply(a: number, b: number): number {
16
- return NativeFlic2.multiply(a, b);
17
- }
18
-
19
- export const onMultiply =
20
- NativeFlic2.onMultiply as CodegenTypes.EventEmitter<MultiplyEvent>;
21
-
22
- // MARK: - Manager Functions
23
-
24
- export function initialize(
25
- background: boolean
26
- ): Promise<{ success: boolean; message: string }> {
27
- return NativeFlic2.initialize(background);
28
- }
29
-
30
- export function getButtons(): Promise<FlicButton[]> {
31
- return NativeFlic2.getButtons();
32
- }
33
-
34
- export function scanForButtons(): Promise<{
35
- success: boolean;
36
- message: string;
37
- }> {
38
- return NativeFlic2.scanForButtons();
39
- }
40
-
41
- export function stopScan(): Promise<{ success: boolean; message: string }> {
42
- return NativeFlic2.stopScan();
43
- }
44
-
45
- export function forgetButton(
46
- uuid: string
47
- ): Promise<{ success: boolean; message: string }> {
48
- return NativeFlic2.forgetButton(uuid);
49
- }
50
-
51
- export function connectAllKnownButtons(): Promise<{
52
- success: boolean;
53
- message: string;
54
- }> {
55
- return NativeFlic2.connectAllKnownButtons();
56
- }
57
-
58
- export function disconnectAllKnownButtons(): Promise<{
59
- success: boolean;
60
- message: string;
61
- }> {
62
- return NativeFlic2.disconnectAllKnownButtons();
63
- }
64
-
65
- export function forgetAllButtons(): Promise<{
66
- success: boolean;
67
- message: string;
68
- }> {
69
- return NativeFlic2.forgetAllButtons();
70
- }
71
-
72
- export function isScanning(): Promise<boolean> {
73
- return NativeFlic2.isScanning();
74
- }
75
-
76
- // MARK: - Button Functions
77
-
78
- export function connectButton(
79
- uuid: string
80
- ): Promise<{ success: boolean; message: string }> {
81
- return NativeFlic2.connectButton(uuid);
82
- }
83
-
84
- export function disconnectButton(
85
- uuid: string
86
- ): Promise<{ success: boolean; message: string }> {
87
- return NativeFlic2.disconnectButton(uuid);
88
- }
89
-
90
- /**
91
- * Sets the trigger mode for a button.
92
- *
93
- * @platform iOS
94
- * @param uuid - Button UUID
95
- * @param mode - Trigger mode (0-3)
96
- * @returns Promise that resolves on iOS, rejects with NOT_SUPPORTED_ON_ANDROID on Android
97
- *
98
- * **Note:** This feature is only available on iOS. On Android, this will reject due to
99
- * limitations in the Android Flic2 library v1.1.0+.
100
- */
101
- export function setTriggerMode(
102
- uuid: string,
103
- mode: TriggerModeType
104
- ): Promise<{ success: boolean; message: string }> {
105
- return NativeFlic2.setTriggerMode(uuid, mode);
106
- }
107
-
108
- /**
109
- * Sets the latency mode for a button.
110
- *
111
- * @platform iOS
112
- * @param uuid - Button UUID
113
- * @param mode - Latency mode (0-1)
114
- * @returns Promise that resolves on iOS, rejects with NOT_SUPPORTED_ON_ANDROID on Android
115
- *
116
- * **Note:** This feature is only available on iOS. On Android, this will reject due to
117
- * limitations in the Android Flic2 library v1.1.0+.
118
- */
119
- export function setLatencyMode(
120
- uuid: string,
121
- mode: LatencyModeType
122
- ): Promise<{ success: boolean; message: string }> {
123
- return NativeFlic2.setLatencyMode(uuid, mode);
124
- }
125
-
126
- export function setNickname(
127
- uuid: string,
128
- nickname: string
129
- ): Promise<{ success: boolean; message: string }> {
130
- return NativeFlic2.setNickname(uuid, nickname);
131
- }
132
-
133
- // MARK: - Event Emitters
134
-
135
- export const onManagerStateChange =
136
- NativeFlic2.onManagerStateChange as CodegenTypes.EventEmitter<ManagerStateChangeEvent>;
137
-
138
- export const onScanStatusChange =
139
- NativeFlic2.onScanStatusChange as CodegenTypes.EventEmitter<ScanStatusChangeEvent>;
140
-
141
- export const onButtonEvent =
142
- NativeFlic2.onButtonEvent as CodegenTypes.EventEmitter<ButtonEvent>;
143
-
144
- // MARK: - Re-export Types
145
-
146
- export type {
147
- MultiplyEvent,
148
- ManagerStateChangeEvent,
149
- ScanStatusChangeEvent,
150
- ButtonEvent,
151
- FlicButton,
152
- FlicManagerState,
153
- FlicButtonState,
154
- FlicTriggerMode,
155
- FlicLatencyMode,
156
- FlicScannerEvent,
157
- TriggerModeType,
158
- LatencyModeType,
159
- } from './NativeFlic2';