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

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.
@@ -51,6 +51,8 @@ class Flic2ButtonEventListener(
51
51
  })
52
52
  }
53
53
 
54
+ // Android library calls ALL applicable callback methods, causing duplicate events.
55
+ // Only onButtonSingleOrDoubleClickOrHold should emit events to match iOS behavior.
54
56
  override fun onButtonClickOrHold(
55
57
  button: Flic2Button,
56
58
  wasQueued: Boolean,
@@ -59,13 +61,11 @@ class Flic2ButtonEventListener(
59
61
  isClick: Boolean,
60
62
  isHold: Boolean
61
63
  ) {
62
- val event = if (isClick) "click" else "hold"
63
- emitEvent(createButtonEvent(button, event).apply {
64
- putBoolean("queued", wasQueued)
65
- putDouble("age", System.currentTimeMillis() - timestamp.toDouble())
66
- })
64
+ // Intentionally empty - events are handled by onButtonSingleOrDoubleClickOrHold
67
65
  }
68
66
 
67
+ // Android library calls ALL applicable callback methods, causing duplicate events.
68
+ // Only onButtonSingleOrDoubleClickOrHold should emit events to match iOS behavior.
69
69
  override fun onButtonSingleOrDoubleClick(
70
70
  button: Flic2Button,
71
71
  wasQueued: Boolean,
@@ -74,11 +74,7 @@ class Flic2ButtonEventListener(
74
74
  isSingleClick: Boolean,
75
75
  isDoubleClick: Boolean
76
76
  ) {
77
- val event = if (isSingleClick) "click" else "doubleClick"
78
- emitEvent(createButtonEvent(button, event).apply {
79
- putBoolean("queued", wasQueued)
80
- putDouble("age", System.currentTimeMillis() - timestamp.toDouble())
81
- })
77
+ // Intentionally empty - events are handled by onButtonSingleOrDoubleClickOrHold
82
78
  }
83
79
 
84
80
  override fun onButtonSingleOrDoubleClickOrHold(
@@ -95,20 +95,6 @@ class Flic2Module(reactContext: ReactApplicationContext) :
95
95
  }
96
96
  }
97
97
 
98
- // Example method - keep for reference
99
- override fun multiply(a: Double, b: Double): Double {
100
- val result = a * b
101
-
102
- val eventData = Arguments.createMap().apply {
103
- putDouble("a", a)
104
- putDouble("b", b)
105
- putDouble("result", result)
106
- }
107
- emitOnMultiply(eventData)
108
-
109
- return result
110
- }
111
-
112
98
  // MARK: - Manager Methods
113
99
 
114
100
  override fun initialize(background: Boolean, promise: Promise) {
@@ -172,46 +158,37 @@ class Flic2Module(reactContext: ReactApplicationContext) :
172
158
 
173
159
  Log.d(TAG, "Starting scan")
174
160
 
161
+ // Emit started event (matches iOS)
162
+ emitOnScanStatusChange(Arguments.createMap().apply {
163
+ putString("event", "started")
164
+ putString("eventName", "started")
165
+ })
166
+
175
167
  manager.startScan(object : Flic2ScanCallback {
176
168
  override fun onDiscoveredAlreadyPairedButton(button: Flic2Button) {
177
169
  Log.d(TAG, "Discovered already paired button")
178
- emitOnScanStatusChange(Arguments.createMap().apply {
179
- putInt("event", 0)
180
- putString("eventName", "discovered")
181
- })
182
170
  }
183
171
 
184
172
  override fun onDiscovered(bdAddr: String) {
185
173
  Log.d(TAG, "Discovered button: $bdAddr")
186
- emitOnScanStatusChange(Arguments.createMap().apply {
187
- putInt("event", 0)
188
- putString("eventName", "discovered")
189
- })
190
174
  }
191
175
 
192
176
  override fun onConnected() {
193
177
  Log.d(TAG, "Button connected during scan")
194
- emitOnScanStatusChange(Arguments.createMap().apply {
195
- putInt("event", 1)
196
- putString("eventName", "connected")
197
- })
198
178
  }
199
179
 
200
180
  override fun onComplete(result: Int, subCode: Int, button: Flic2Button?) {
201
181
  Log.d(TAG, "Scan complete: result=$result, button=${button?.uuid}")
202
182
 
203
- if (result == Flic2ScanCallback.RESULT_SUCCESS && button != null) {
204
- emitOnScanStatusChange(Arguments.createMap().apply {
205
- putInt("event", 2)
206
- putString("eventName", "verified")
207
- })
183
+ val resultCode = mapScanResultToCode(result)
208
184
 
185
+ if (result == Flic2ScanCallback.RESULT_SUCCESS && button != null) {
209
186
  // Auto-connect (trigger mode not available in Android v1.1.0+)
210
187
  button.connect()
211
188
 
212
189
  setupButtonListener(button)
213
190
 
214
- // Emit discovered event
191
+ // Emit discovered event as button event (like iOS)
215
192
  emitOnButtonEvent(Arguments.createMap().apply {
216
193
  putString("uuid", button.uuid)
217
194
  putString("event", "discovered")
@@ -221,6 +198,13 @@ class Flic2Module(reactContext: ReactApplicationContext) :
221
198
  val errorCode = Flic2Converter.scanResultToString(result)
222
199
  Log.e(TAG, "Scan failed with error code: $errorCode")
223
200
  }
201
+
202
+ // Emit scan completion with result code
203
+ emitOnScanStatusChange(Arguments.createMap().apply {
204
+ putString("event", "completion")
205
+ putString("eventName", "completion")
206
+ putInt("result", resultCode)
207
+ })
224
208
  }
225
209
  })
226
210
 
@@ -477,4 +461,21 @@ class Flic2Module(reactContext: ReactApplicationContext) :
477
461
  // Store listener reference
478
462
  buttonListeners[button.uuid] = listener
479
463
  }
464
+
465
+ private fun mapScanResultToCode(result: Int): Int {
466
+ // Map Android library's 9 result codes (0-8) to TypeScript enum codes (0-21) matching iOS
467
+ // Android library only provides these constants, so we map them to the closest equivalent
468
+ return when (result) {
469
+ Flic2ScanCallback.RESULT_SUCCESS -> 0 // SUCCESS
470
+ Flic2ScanCallback.RESULT_FAILED_ALREADY_RUNNING -> 1 // ALREADY_RUNNING
471
+ Flic2ScanCallback.RESULT_FAILED_BLUETOOTH_OFF -> 2 // BLUETOOTH_NOT_ACTIVATED
472
+ Flic2ScanCallback.RESULT_FAILED_SCAN_ERROR -> 3 // UNKNOWN
473
+ Flic2ScanCallback.RESULT_FAILED_NO_NEW_BUTTONS_FOUND -> 4 // NO_PUBLIC_BUTTON_DISCOVERED
474
+ Flic2ScanCallback.RESULT_FAILED_BUTTON_ALREADY_CONNECTED_TO_OTHER_DEVICE -> 5 // ALREADY_CONNECTED_TO_ANOTHER_DEVICE
475
+ Flic2ScanCallback.RESULT_FAILED_CONNECT_TIMED_OUT -> 6 // CONNECTION_TIMEOUT
476
+ Flic2ScanCallback.RESULT_FAILED_VERIFY_TIMED_OUT -> 7 // INVALID_VERIFIER
477
+ Flic2ScanCallback.RESULT_SYSTEM_PAIRING_DIALOG_NOT_ACCEPTED -> 9 // BLE_PAIRING_FAILED_USER_CANCELED
478
+ else -> 3 // UNKNOWN (for any unexpected codes)
479
+ }
480
+ }
480
481
  }
package/ios/Flic2.h CHANGED
@@ -6,7 +6,6 @@
6
6
 
7
7
  @interface Flic2 : NativeFlic2SpecBase <NativeFlic2Spec, FLICManagerDelegate, FLICButtonDelegate>
8
8
 
9
- @property (nonatomic, strong) FLICManager *manager;
10
9
  @property (nonatomic, assign) BOOL managerRestored;
11
10
 
12
11
  @end