react-native-bluebreeze 0.0.12 → 1.0.0

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.
Files changed (96) hide show
  1. package/README.md +97 -13
  2. package/android/build.gradle +2 -3
  3. package/android/generated/jni/CMakeLists.txt +1 -9
  4. package/android/generated/jni/RNBlueBreezeSpec-generated.cpp +1 -1
  5. package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI.h +234 -279
  6. package/android/src/main/java/com/bluebreeze/BlueBreezeModule.kt +140 -120
  7. package/ios/BlueBreezeImpl.swift +30 -24
  8. package/ios/Bluebreeze.h +1 -1
  9. package/ios/Bluebreeze.mm +2 -7
  10. package/ios/generated/{RNBlueBreezeSpecJSI.h → ReactCodegen/RNBlueBreezeSpecJSI.h} +234 -279
  11. package/lib/commonjs/implementation/bluebreeze_characteristic.js +42 -11
  12. package/lib/commonjs/implementation/bluebreeze_characteristic.js.map +1 -1
  13. package/lib/commonjs/implementation/bluebreeze_connection_status.js +2 -0
  14. package/lib/commonjs/implementation/bluebreeze_connection_status.js.map +1 -0
  15. package/lib/commonjs/implementation/bluebreeze_device.js +53 -13
  16. package/lib/commonjs/implementation/bluebreeze_device.js.map +1 -1
  17. package/lib/commonjs/implementation/bluebreeze_scan_result.js +19 -0
  18. package/lib/commonjs/implementation/bluebreeze_scan_result.js.map +1 -1
  19. package/lib/commonjs/implementation/bluebreeze_service.js +9 -0
  20. package/lib/commonjs/implementation/bluebreeze_service.js.map +1 -1
  21. package/lib/commonjs/implementation/native_bridge.js +108 -0
  22. package/lib/commonjs/implementation/native_bridge.js.map +1 -0
  23. package/lib/commonjs/index.js +29 -100
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/package.json +1 -0
  26. package/lib/module/implementation/bluebreeze_characteristic.js +43 -10
  27. package/lib/module/implementation/bluebreeze_characteristic.js.map +1 -1
  28. package/lib/module/implementation/bluebreeze_connection_status.js +2 -0
  29. package/lib/module/implementation/bluebreeze_connection_status.js.map +1 -0
  30. package/lib/module/implementation/bluebreeze_device.js +54 -12
  31. package/lib/module/implementation/bluebreeze_device.js.map +1 -1
  32. package/lib/module/implementation/bluebreeze_scan_result.js +19 -0
  33. package/lib/module/implementation/bluebreeze_scan_result.js.map +1 -1
  34. package/lib/module/implementation/bluebreeze_service.js +9 -0
  35. package/lib/module/implementation/bluebreeze_service.js.map +1 -1
  36. package/lib/module/implementation/native_bridge.js +99 -0
  37. package/lib/module/implementation/native_bridge.js.map +1 -0
  38. package/lib/module/index.js +29 -100
  39. package/lib/module/index.js.map +1 -1
  40. package/lib/module/package.json +1 -0
  41. package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts +1 -1
  42. package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts.map +1 -1
  43. package/lib/typescript/commonjs/src/implementation/bluebreeze_authorization.d.ts +10 -1
  44. package/lib/typescript/commonjs/src/implementation/bluebreeze_authorization.d.ts.map +1 -1
  45. package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts +29 -0
  46. package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
  47. package/lib/typescript/commonjs/src/implementation/bluebreeze_connection_status.d.ts +7 -0
  48. package/lib/typescript/commonjs/src/implementation/bluebreeze_connection_status.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts +42 -1
  50. package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts.map +1 -1
  51. package/lib/typescript/commonjs/src/implementation/bluebreeze_scan_result.d.ts +11 -0
  52. package/lib/typescript/commonjs/src/implementation/bluebreeze_scan_result.d.ts.map +1 -1
  53. package/lib/typescript/commonjs/src/implementation/bluebreeze_service.d.ts +6 -0
  54. package/lib/typescript/commonjs/src/implementation/bluebreeze_service.d.ts.map +1 -1
  55. package/lib/typescript/commonjs/src/implementation/bluebreeze_state.d.ts +16 -1
  56. package/lib/typescript/commonjs/src/implementation/bluebreeze_state.d.ts.map +1 -1
  57. package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts +17 -0
  58. package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts.map +1 -0
  59. package/lib/typescript/commonjs/src/index.d.ts +0 -13
  60. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  61. package/lib/typescript/module/src/NativeBlueBreeze.d.ts +1 -1
  62. package/lib/typescript/module/src/NativeBlueBreeze.d.ts.map +1 -1
  63. package/lib/typescript/module/src/implementation/bluebreeze_authorization.d.ts +10 -1
  64. package/lib/typescript/module/src/implementation/bluebreeze_authorization.d.ts.map +1 -1
  65. package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts +29 -0
  66. package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
  67. package/lib/typescript/module/src/implementation/bluebreeze_connection_status.d.ts +7 -0
  68. package/lib/typescript/module/src/implementation/bluebreeze_connection_status.d.ts.map +1 -0
  69. package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts +43 -2
  70. package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts.map +1 -1
  71. package/lib/typescript/module/src/implementation/bluebreeze_scan_result.d.ts +12 -1
  72. package/lib/typescript/module/src/implementation/bluebreeze_scan_result.d.ts.map +1 -1
  73. package/lib/typescript/module/src/implementation/bluebreeze_service.d.ts +7 -1
  74. package/lib/typescript/module/src/implementation/bluebreeze_service.d.ts.map +1 -1
  75. package/lib/typescript/module/src/implementation/bluebreeze_state.d.ts +16 -1
  76. package/lib/typescript/module/src/implementation/bluebreeze_state.d.ts.map +1 -1
  77. package/lib/typescript/module/src/implementation/native_bridge.d.ts +17 -0
  78. package/lib/typescript/module/src/implementation/native_bridge.d.ts.map +1 -0
  79. package/lib/typescript/module/src/index.d.ts +6 -19
  80. package/lib/typescript/module/src/index.d.ts.map +1 -1
  81. package/package.json +13 -23
  82. package/react-native-bluebreeze.podspec +1 -1
  83. package/src/NativeBlueBreeze.ts +1 -1
  84. package/src/implementation/bluebreeze_authorization.ts +10 -1
  85. package/src/implementation/bluebreeze_characteristic.ts +48 -10
  86. package/src/implementation/bluebreeze_connection_status.ts +6 -0
  87. package/src/implementation/bluebreeze_device.ts +62 -13
  88. package/src/implementation/bluebreeze_scan_result.ts +18 -0
  89. package/src/implementation/bluebreeze_service.ts +8 -0
  90. package/src/implementation/bluebreeze_state.ts +16 -1
  91. package/src/implementation/native_bridge.ts +196 -0
  92. package/src/index.tsx +24 -202
  93. package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI-generated.cpp +0 -179
  94. package/ios/generated/RNBlueBreezeSpecJSI-generated.cpp +0 -179
  95. /package/ios/generated/{RNBlueBreezeSpec → ReactCodegen/RNBlueBreezeSpec}/RNBlueBreezeSpec-generated.mm +0 -0
  96. /package/ios/generated/{RNBlueBreezeSpec → ReactCodegen/RNBlueBreezeSpec}/RNBlueBreezeSpec.h +0 -0
@@ -21,6 +21,8 @@ import dev.likemagic.bluebreeze.BBUUID
21
21
  import kotlinx.coroutines.CoroutineScope
22
22
  import kotlinx.coroutines.Dispatchers
23
23
  import kotlinx.coroutines.Job
24
+ import kotlinx.coroutines.SupervisorJob
25
+ import kotlinx.coroutines.cancel
24
26
  import kotlinx.coroutines.flow.FlowCollector
25
27
  import kotlinx.coroutines.flow.SharedFlow
26
28
  import kotlinx.coroutines.launch
@@ -28,16 +30,37 @@ import kotlinx.coroutines.launch
28
30
  @ReactModule(name = BlueBreezeModule.NAME)
29
31
  class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreezeSpec(reactContext) {
30
32
  val manager = BBManager(reactContext)
31
- val jobs: MutableList<Job> = mutableListOf()
32
33
 
33
34
  val context: Context
34
- get() = currentActivity ?: reactApplicationContext
35
+ get() = getCurrentActivity() ?: reactApplicationContext
35
36
 
36
- private val trackedDeviceServices = mutableSetOf<String>()
37
- private val trackedDeviceConnectionStatus = mutableSetOf<String>()
38
- private val trackedDeviceMTU = mutableSetOf<String>()
39
- private val trackedDeviceCharacteristicData = mutableSetOf<String>()
40
- private val trackedDeviceCharacteristicNotifyEnabled = mutableSetOf<String>()
37
+ // Per-device subscriptions
38
+
39
+ private class DeviceTracking {
40
+ // Subscriptions to device.services/connectionStatus/mtu, set up once per device
41
+ val deviceJobs = mutableListOf<Job>()
42
+
43
+ // Per characteristic data and notification state subscriptions
44
+ val characteristicDataJobs = mutableMapOf<String, Job>()
45
+ val characteristicNotificationJobs = mutableMapOf<String, Job>()
46
+
47
+ fun cancelCharacteristics() {
48
+ characteristicDataJobs.values.forEach { it.cancel() }
49
+ characteristicDataJobs.clear()
50
+
51
+ characteristicNotificationJobs.values.forEach { it.cancel() }
52
+ characteristicNotificationJobs.clear()
53
+ }
54
+
55
+ fun cancelAll() {
56
+ cancelCharacteristics()
57
+
58
+ deviceJobs.forEach { it.cancel() }
59
+ deviceJobs.clear()
60
+ }
61
+ }
62
+
63
+ private val deviceTracking = mutableMapOf<String, DeviceTracking>()
41
64
 
42
65
  // Lifecycle
43
66
 
@@ -50,7 +73,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
50
73
  safeEmit {
51
74
  emitStateEmitter(it.name)
52
75
  }
53
- }.storeIn(jobs)
76
+ }
54
77
 
55
78
  // Authorization
56
79
 
@@ -58,7 +81,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
58
81
  safeEmit {
59
82
  emitAuthorizationStatusEmitter(it.toJs)
60
83
  }
61
- }.storeIn(jobs)
84
+ }
62
85
 
63
86
  // Scanning
64
87
 
@@ -66,13 +89,13 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
66
89
  safeEmit {
67
90
  emitScanEnabledEmitter(it)
68
91
  }
69
- }.storeIn(jobs)
92
+ }
70
93
 
71
94
  manager.scanResults.collectAsync {
72
95
  safeEmit {
73
96
  emitScanResultEmitter(it.toJs)
74
97
  }
75
- }.storeIn(jobs)
98
+ }
76
99
 
77
100
  // Devices
78
101
 
@@ -83,93 +106,93 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
83
106
  )
84
107
  }
85
108
 
109
+ // Tear down tracking for devices that dropped out of the map
110
+ deviceTracking.keys.minus(devices.keys).forEach { deviceId ->
111
+ deviceTracking.remove(deviceId)?.cancelAll()
112
+ }
113
+
86
114
  for ((deviceId, device) in devices) {
87
- // Device services
88
- if (trackedDeviceServices.contains(deviceId).not()) {
89
- trackedDeviceServices.add(deviceId)
90
- device.services.collectAsync { services ->
91
- safeEmit {
92
- emitDeviceServicesEmitter(
93
- writableMapOf(
94
- "id" to deviceId,
95
- "value" to services.map { it.toJs }
96
- )
97
- )
98
- }
99
-
100
- services.forEach { service ->
101
- val serviceId = service.uuid
102
- service.characteristics.forEach { characteristic ->
103
- val characteristicId = characteristic.uuid
104
- val trackingKey = "${deviceId}:${serviceId}:${characteristicId}"
105
-
106
- // Device characteristic data
107
- if (trackedDeviceCharacteristicData.contains(trackingKey).not()) {
108
- trackedDeviceCharacteristicData.add(trackingKey)
109
- characteristic.data.collectAsync { value ->
110
- safeEmit {
111
- emitDeviceCharacteristicDataEmitter(
112
- writableMapOf(
113
- "id" to deviceId,
114
- "serviceId" to serviceId.toJs,
115
- "characteristicId" to characteristicId.toJs,
116
- "value" to value.toJs
117
- )
118
- )
119
- }
120
- }
121
- }
122
-
123
- // Device characteristic notify enabled
124
- if (trackedDeviceCharacteristicNotifyEnabled.contains(trackingKey)
125
- .not()
126
- ) {
127
- trackedDeviceCharacteristicNotifyEnabled.add(trackingKey)
128
- characteristic.isNotifying.collectAsync { value ->
129
- safeEmit {
130
- emitDeviceCharacteristicNotifyEnabledEmitter(
131
- writableMapOf(
132
- "id" to deviceId,
133
- "serviceId" to serviceId.toJs,
134
- "characteristicId" to characteristicId.toJs,
135
- "value" to value
136
- )
137
- )
138
- }
139
- }
140
- }
141
- }
142
- }
143
- }
144
- }
115
+ val tracking = deviceTracking.getOrPut(deviceId) { DeviceTracking() }
116
+ setupDeviceTracking(deviceId, device, tracking)
117
+ }
118
+ }
119
+ }
120
+
121
+ // Device services/connectionStatus/mtu are stable flows on the same BBDevice instance for
122
+ // its whole lifetime, so we only register these collectors once per device.
123
+ private fun setupDeviceTracking(deviceId: String, device: BBDevice, tracking: DeviceTracking) {
124
+ if (tracking.deviceJobs.isNotEmpty()) return
125
+
126
+ tracking.deviceJobs += device.services.collectAsync { services ->
127
+ safeEmit {
128
+ emitDeviceServicesEmitter(
129
+ writableMapOf(
130
+ "id" to deviceId,
131
+ "value" to services.map { it.toJs }
132
+ )
133
+ )
134
+ }
135
+
136
+ setupDeviceCharacteristics(deviceId, services, tracking)
137
+ }
145
138
 
146
- // Device connection status
147
- if (trackedDeviceConnectionStatus.contains(deviceId).not()) {
148
- trackedDeviceConnectionStatus.add(deviceId)
149
- device.connectionStatus.collectAsync { value ->
150
- safeEmit {
151
- emitDeviceConnectionStatusEmitter(
152
- writableMapOf(
153
- "id" to deviceId,
154
- "value" to value.toJs,
155
- )
139
+ tracking.deviceJobs += device.connectionStatus.collectAsync { value ->
140
+ safeEmit {
141
+ emitDeviceConnectionStatusEmitter(
142
+ writableMapOf(
143
+ "id" to deviceId,
144
+ "value" to value.toJs,
145
+ )
146
+ )
147
+ }
148
+ }
149
+
150
+ tracking.deviceJobs += device.mtu.collectAsync { value ->
151
+ safeEmit {
152
+ emitDeviceMTUEmitter(
153
+ writableMapOf(
154
+ "id" to deviceId,
155
+ "value" to value,
156
+ )
157
+ )
158
+ }
159
+ }
160
+ }
161
+
162
+ // BBCharacteristics are not stable instances, so we rebuild the collectors every time
163
+ // services are updated.
164
+ private fun setupDeviceCharacteristics(deviceId: String, services: List<BBService>, tracking: DeviceTracking) {
165
+ tracking.cancelCharacteristics()
166
+
167
+ services.forEach { service ->
168
+ val serviceId = service.uuid
169
+ service.characteristics.forEach { characteristic ->
170
+ val characteristicId = characteristic.uuid
171
+ val trackingKey = "${serviceId}:${characteristicId}"
172
+
173
+ tracking.characteristicDataJobs[trackingKey] = characteristic.data.collectAsync { value ->
174
+ safeEmit {
175
+ emitDeviceCharacteristicDataEmitter(
176
+ writableMapOf(
177
+ "id" to deviceId,
178
+ "serviceId" to serviceId.toJs,
179
+ "characteristicId" to characteristicId.toJs,
180
+ "value" to value.toJs
156
181
  )
157
- }
182
+ )
158
183
  }
159
184
  }
160
185
 
161
- // Device MTU
162
- if (trackedDeviceMTU.contains(deviceId).not()) {
163
- trackedDeviceMTU.add(deviceId)
164
- device.mtu.collectAsync { value ->
165
- safeEmit {
166
- emitDeviceMTUEmitter(
167
- writableMapOf(
168
- "id" to deviceId,
169
- "value" to value,
170
- )
186
+ tracking.characteristicNotificationJobs[trackingKey] = characteristic.isNotifying.collectAsync { value ->
187
+ safeEmit {
188
+ emitDeviceCharacteristicNotifyEnabledEmitter(
189
+ writableMapOf(
190
+ "id" to deviceId,
191
+ "serviceId" to serviceId.toJs,
192
+ "characteristicId" to characteristicId.toJs,
193
+ "value" to value
171
194
  )
172
- }
195
+ )
173
196
  }
174
197
  }
175
198
  }
@@ -179,8 +202,10 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
179
202
  override fun invalidate() {
180
203
  super.invalidate()
181
204
 
182
- jobs.forEach { it.cancel() }
183
- jobs.clear()
205
+ deviceTracking.values.forEach { it.cancelAll() }
206
+ deviceTracking.clear()
207
+
208
+ moduleScope.cancel()
184
209
  }
185
210
 
186
211
  // State
@@ -246,7 +271,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
246
271
  } catch (e: Throwable) {
247
272
  promise?.reject(e)
248
273
  }
249
- }.storeIn(jobs)
274
+ }
250
275
  }
251
276
 
252
277
  override fun deviceDisconnect(id: String?, promise: Promise?) {
@@ -262,7 +287,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
262
287
  } catch (e: Throwable) {
263
288
  promise?.reject(e)
264
289
  }
265
- }.storeIn(jobs)
290
+ }
266
291
  }
267
292
 
268
293
  override fun deviceDiscoverServices(id: String?, promise: Promise?) {
@@ -278,7 +303,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
278
303
  } catch (e: Throwable) {
279
304
  promise?.reject(e)
280
305
  }
281
- }.storeIn(jobs)
306
+ }
282
307
  }
283
308
 
284
309
  override fun deviceRequestMTU(id: String?, mtu: Double, promise: Promise?) {
@@ -289,12 +314,12 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
289
314
 
290
315
  async {
291
316
  try {
292
- val result = device.requestMTU(mtu.toInt())
317
+ val result = device.requestMtu(mtu.toInt())
293
318
  promise?.resolve(result)
294
319
  } catch (e: Throwable) {
295
320
  promise?.reject(e)
296
321
  }
297
- }.storeIn(jobs)
322
+ }
298
323
  }
299
324
 
300
325
  // Device characteristic data
@@ -366,7 +391,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
366
391
  } catch (e: Throwable) {
367
392
  promise?.reject(e)
368
393
  }
369
- }.storeIn(jobs)
394
+ }
370
395
  }
371
396
 
372
397
  override fun deviceCharacteristicWrite(
@@ -411,7 +436,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
411
436
  } catch (e: Throwable) {
412
437
  promise?.reject(e)
413
438
  }
414
- }.storeIn(jobs)
439
+ }
415
440
  }
416
441
 
417
442
  override fun deviceCharacteristicSubscribe(
@@ -446,7 +471,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
446
471
  } catch (e: Throwable) {
447
472
  promise?.reject(e)
448
473
  }
449
- }.storeIn(jobs)
474
+ }
450
475
  }
451
476
 
452
477
  override fun deviceCharacteristicUnsubscribe(
@@ -481,7 +506,7 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
481
506
  } catch (e: Throwable) {
482
507
  promise?.reject(e)
483
508
  }
484
- }.storeIn(jobs)
509
+ }
485
510
  }
486
511
 
487
512
  // Helper for safely emitting data
@@ -492,6 +517,19 @@ class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreeze
492
517
  }
493
518
  }
494
519
 
520
+ // Functions to collect data asynchronously, all launched on moduleScope so invalidate()
521
+ // can reliably cancel every collector
522
+
523
+ private val moduleScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
524
+
525
+ private fun async(
526
+ block: suspend CoroutineScope.() -> Unit,
527
+ ): Job = moduleScope.launch(block = block)
528
+
529
+ private fun <T> SharedFlow<T>.collectAsync(
530
+ collector: FlowCollector<T>,
531
+ ): Job = moduleScope.launch { collect(collector) }
532
+
495
533
  // Module name
496
534
 
497
535
  override fun getName(): String {
@@ -518,7 +556,6 @@ val BBState.toJs: String
518
556
  BBState.unknown -> "unknown"
519
557
  BBState.poweredOff -> "poweredOff"
520
558
  BBState.poweredOn -> "poweredOn"
521
- BBState.unauthorized -> "unauthorized"
522
559
  }
523
560
 
524
561
  val BBCharacteristicProperty.toJs: String
@@ -576,23 +613,6 @@ val ByteArray.toJs: WritableArray
576
613
  val BBUUID.toJs: String
577
614
  get() = toString()
578
615
 
579
- /// Functions to collect data asynchronously
580
-
581
- fun Job.storeIn(list: MutableList<Job>) = list.add(this)
582
-
583
- fun async(
584
- block: suspend CoroutineScope.() -> Unit,
585
- ): Job = CoroutineScope(Dispatchers.IO).launch(block = block)
586
-
587
- fun <T> SharedFlow<T>.collectAsync(
588
- collector: FlowCollector<T>,
589
- ): Job {
590
- return CoroutineScope(Dispatchers.IO)
591
- .launch {
592
- collect(collector)
593
- }
594
- }
595
-
596
616
  /// Functions to handle React Native types
597
617
 
598
618
  val List<*>.writableArray: WritableArray
@@ -111,28 +111,30 @@ struct BlueBreezeError : Error {
111
111
  .store(in: &disposeBagDevices[device.id]!)
112
112
  }
113
113
 
114
- private func subscribeServices(_ device: BBDevice, _ services: [BBUUID: [BBCharacteristic]]) {
114
+ private func subscribeServices(_ device: BBDevice, _ services: [BBService]) {
115
115
  // Clean up device data by removing missing services
116
116
  disposeBagServices[device.id] =
117
117
  disposeBagServices[device.id]?.filter({ key, value in
118
- services[key] != nil
118
+ services.contains(where: { $0.uuid == key })
119
119
  }) ?? [:]
120
120
 
121
121
  // Init all existing services
122
- services.forEach { serviceId, value in
122
+ services.forEach { service in
123
+ let serviceId = service.uuid
124
+
123
125
  // Clean up service data by removing missing characteristics
124
126
  disposeBagServices[device.id]![serviceId] =
125
127
  disposeBagServices[device.id]![serviceId]?.filter({ key, _ in
126
- value.first(where: { $0.id == key }) != nil
128
+ service.characteristics.first(where: { $0.uuid == key }) != nil
127
129
  }) ?? [:]
128
130
 
129
131
  // Init all existing characteristics
130
- value.forEach { characteristic in
131
- guard disposeBagServices[device.id]![serviceId]![characteristic.id] == nil else {
132
+ service.characteristics.forEach { characteristic in
133
+ guard disposeBagServices[device.id]![serviceId]![characteristic.uuid] == nil else {
132
134
  return
133
135
  }
134
136
 
135
- disposeBagServices[device.id]![serviceId]![characteristic.id] = []
137
+ disposeBagServices[device.id]![serviceId]![characteristic.uuid] = []
136
138
 
137
139
  characteristic.isNotifying
138
140
  .receive(on: DispatchQueue.main)
@@ -140,11 +142,11 @@ struct BlueBreezeError : Error {
140
142
  self.deviceCharacteristicNotifyEnabledObserve?(
141
143
  device.id.toJs,
142
144
  serviceId.toJs,
143
- characteristic.id.toJs,
145
+ characteristic.uuid.toJs,
144
146
  $0
145
147
  )
146
148
  }
147
- .store(in: &disposeBagServices[device.id]![serviceId]![characteristic.id]!)
149
+ .store(in: &disposeBagServices[device.id]![serviceId]![characteristic.uuid]!)
148
150
 
149
151
  characteristic.data
150
152
  .receive(on: DispatchQueue.main)
@@ -152,11 +154,11 @@ struct BlueBreezeError : Error {
152
154
  self.deviceCharacteristicDataObserve?(
153
155
  device.id.toJs,
154
156
  serviceId.toJs,
155
- characteristic.id.toJs,
156
- $0.toJs
157
+ characteristic.uuid.toJs,
158
+ $0?.toJs ?? []
157
159
  )
158
160
  }
159
- .store(in: &disposeBagServices[device.id]![serviceId]![characteristic.id]!)
161
+ .store(in: &disposeBagServices[device.id]![serviceId]![characteristic.uuid]!)
160
162
  }
161
163
  }
162
164
  }
@@ -299,7 +301,7 @@ struct BlueBreezeError : Error {
299
301
  try await device.discoverServices()
300
302
  }
301
303
 
302
- @objc public func deviceRequestMTU(id: String, mtu: Int) async throws {
304
+ @objc public func deviceRequestMTU(id: String, mtu: Int) async throws -> Int {
303
305
  guard let uuid = UUID(uuidString: id) else {
304
306
  throw BlueBreezeError(description: "Invalid UUID")
305
307
  }
@@ -307,8 +309,12 @@ struct BlueBreezeError : Error {
307
309
  guard let device = manager.devices.value[uuid] else {
308
310
  throw BlueBreezeError(description: "Device not found")
309
311
  }
310
-
311
- try await device.requestMTU(mtu)
312
+
313
+ // CoreBluetooth has no API to request a specific MTU value. negotiateMTU() only reads
314
+ // back whatever value it auto-negotiated on connect, so `mtu` is ignored on iOS.
315
+ try await device.negotiateMTU()
316
+
317
+ return device.mtu.value
312
318
  }
313
319
 
314
320
  // MARK: - Device characteristic data
@@ -326,7 +332,7 @@ struct BlueBreezeError : Error {
326
332
  return []
327
333
  }
328
334
 
329
- return characteristic.data.value.toJs
335
+ return characteristic.data.value?.toJs ?? []
330
336
  }
331
337
 
332
338
  // MARK: - Device notify enabled
@@ -419,8 +425,8 @@ struct BlueBreezeError : Error {
419
425
 
420
426
  extension BBDevice {
421
427
  func getCharacteristic(serviceId: String, characteristicId: String) -> BBCharacteristic? {
422
- let service = services.value.first { $0.key == BBUUID(string: serviceId)}
423
- let characteristic = service?.value.first { $0.id == BBUUID(string: characteristicId) }
428
+ let service = services.value.first { $0.uuid == BBUUID(string: serviceId) }
429
+ let characteristic = service?.characteristics.first { $0.uuid == BBUUID(string: characteristicId) }
424
430
  return characteristic
425
431
  }
426
432
  }
@@ -509,8 +515,8 @@ extension BBDeviceConnectionStatus {
509
515
  extension BBCharacteristic {
510
516
  var toJs: [String: Any] {
511
517
  [
512
- "id": id.uuidString,
513
- "name": BBConstants.knownCharacteristics[id] as Any,
518
+ "id": uuid.uuidString,
519
+ "name": BBAssignedNumbers.characteristicUUIDs[uuid] as Any,
514
520
  "properties": properties.map { $0.toJs }
515
521
  ]
516
522
  }
@@ -522,13 +528,13 @@ extension [BBCharacteristic] {
522
528
  }
523
529
  }
524
530
 
525
- extension [BBUUID: [BBCharacteristic]] {
531
+ extension [BBService] {
526
532
  var toJs: [[String: Any]] {
527
533
  map {
528
534
  [
529
- "id": $0.key.uuidString,
530
- "name": BBConstants.knownServices[$0.key] as Any,
531
- "characteristics": $0.value.toJs
535
+ "id": $0.uuid.uuidString,
536
+ "name": BBAssignedNumbers.serviceUUIDs[$0.uuid] as Any,
537
+ "characteristics": $0.characteristics.toJs
532
538
  ]
533
539
  }
534
540
  }
package/ios/Bluebreeze.h CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  #import <React/RCTInitializing.h>
3
3
  #import <React/RCTInvalidating.h>
4
- #import "generated/RNBlueBreezeSpec/RNBlueBreezeSpec.h"
4
+ #import "generated/ReactCodegen/RNBlueBreezeSpec/RNBlueBreezeSpec.h"
5
5
 
6
6
  @interface BlueBreeze : NativeBlueBreezeSpecBase <NativeBlueBreezeSpec, RCTInitializing, RCTInvalidating>
7
7
 
package/ios/Bluebreeze.mm CHANGED
@@ -3,11 +3,6 @@
3
3
 
4
4
  @implementation BlueBreeze {
5
5
  BlueBreezeImpl* impl;
6
- NSMutableArray<NSString*>* trackedDeviceServices;
7
- NSMutableArray<NSString*>* trackedDeviceConnectionStatus;
8
- NSMutableArray<NSString*>* trackedDeviceMTU;
9
- NSMutableArray<NSString*>* trackedDeviceCharacteristicData;
10
- NSMutableArray<NSString*>* trackedDeviceCharacteristicNotifyEnabled;
11
6
  }
12
7
 
13
8
  - (instancetype) init {
@@ -199,11 +194,11 @@ RCT_EXPORT_MODULE()
199
194
  }
200
195
 
201
196
  - (void)deviceRequestMTU:(NSString *)id mtu:(double)mtu resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
202
- [impl deviceRequestMTUWithId:id mtu:mtu completionHandler:^(NSError * _Nullable error) {
197
+ [impl deviceRequestMTUWithId:id mtu:mtu completionHandler:^(NSInteger value, NSError * _Nullable error) {
203
198
  if (error != nil) {
204
199
  reject([@([error code]) stringValue], [error description], error);
205
200
  } else {
206
- resolve(nil);
201
+ resolve(@(value));
207
202
  }
208
203
  }];
209
204
  }