react-native-ble-nitro 1.0.0 → 1.1.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 (40) hide show
  1. package/README.md +19 -15
  2. package/ios/BleNitroBleManager.swift +77 -43
  3. package/lib/commonjs/index.d.ts +15 -13
  4. package/lib/commonjs/index.d.ts.map +1 -1
  5. package/lib/commonjs/index.js +76 -139
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +13 -10
  8. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
  9. package/lib/index.d.ts +15 -13
  10. package/lib/index.js +75 -138
  11. package/lib/specs/NativeBleNitro.nitro.d.ts +13 -10
  12. package/nitrogen/generated/android/BleNitroOnLoad.cpp +2 -8
  13. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +90 -49
  14. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +9 -10
  15. package/nitrogen/generated/android/c++/JOperationResult.hpp +58 -0
  16. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +11 -60
  17. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/OperationResult.kt +32 -0
  18. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +5 -29
  19. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +69 -78
  20. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Umbrella.hpp +3 -0
  21. package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +39 -24
  22. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +9 -10
  23. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +60 -112
  24. package/nitrogen/generated/ios/swift/OperationResult.swift +64 -0
  25. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +0 -1
  26. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +13 -11
  27. package/nitrogen/generated/shared/c++/OperationResult.hpp +72 -0
  28. package/package.json +1 -1
  29. package/src/__tests__/index.test.ts +19 -22
  30. package/src/index.ts +100 -156
  31. package/src/specs/NativeBleNitro.nitro.ts +14 -10
  32. package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +0 -74
  33. package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +0 -99
  34. package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +0 -93
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_bool.kt +0 -81
  36. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +0 -81
  37. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_std__string_.kt +0 -81
  38. package/nitrogen/generated/ios/swift/Func_void_bool.swift +0 -47
  39. package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +0 -47
  40. package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +0 -47
@@ -15,24 +15,23 @@ public protocol HybridNativeBleNitroSpec_protocol: HybridObject {
15
15
 
16
16
  // Methods
17
17
  func startScan(filter: ScanFilter, callback: @escaping (_ device: BLEDevice) -> Void) throws -> Void
18
- func stopScan(callback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
19
- func isScanning(callback: @escaping (_ result: Bool) -> Void) throws -> Void
20
- func getConnectedDevices(callback: @escaping (_ devices: [BLEDevice]) -> Void) throws -> Void
18
+ func stopScan() throws -> Bool
19
+ func isScanning() throws -> Bool
20
+ func getConnectedDevices(services: [String]) throws -> [BLEDevice]
21
21
  func connect(deviceId: String, callback: @escaping (_ success: Bool, _ deviceId: String, _ error: String) -> Void, disconnectCallback: ((_ deviceId: String, _ interrupted: Bool, _ error: String) -> Void)?) throws -> Void
22
22
  func disconnect(deviceId: String, callback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
23
- func isConnected(deviceId: String, callback: @escaping (_ result: Bool) -> Void) throws -> Void
23
+ func isConnected(deviceId: String) throws -> Bool
24
24
  func discoverServices(deviceId: String, callback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
25
- func getServices(deviceId: String, callback: @escaping (_ result: [String]) -> Void) throws -> Void
26
- func getCharacteristics(deviceId: String, serviceId: String, callback: @escaping (_ result: [String]) -> Void) throws -> Void
25
+ func getServices(deviceId: String) throws -> [String]
26
+ func getCharacteristics(deviceId: String, serviceId: String) throws -> [String]
27
27
  func readCharacteristic(deviceId: String, serviceId: String, characteristicId: String, callback: @escaping (_ success: Bool, _ data: [Double], _ error: String) -> Void) throws -> Void
28
28
  func writeCharacteristic(deviceId: String, serviceId: String, characteristicId: String, data: [Double], withResponse: Bool, callback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
29
29
  func subscribeToCharacteristic(deviceId: String, serviceId: String, characteristicId: String, updateCallback: @escaping (_ characteristicId: String, _ data: [Double]) -> Void, resultCallback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
30
30
  func unsubscribeFromCharacteristic(deviceId: String, serviceId: String, characteristicId: String, callback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
31
- func isBluetoothEnabled(callback: @escaping (_ result: Bool) -> Void) throws -> Void
32
31
  func requestBluetoothEnable(callback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
33
- func state(callback: @escaping (_ state: BLEState) -> Void) throws -> Void
34
- func subscribeToStateChange(stateCallback: @escaping (_ state: BLEState) -> Void, resultCallback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
35
- func unsubscribeFromStateChange(resultCallback: @escaping (_ success: Bool, _ error: String) -> Void) throws -> Void
32
+ func state() throws -> BLEState
33
+ func subscribeToStateChange(stateCallback: @escaping (_ state: BLEState) -> Void) throws -> OperationResult
34
+ func unsubscribeFromStateChange() throws -> OperationResult
36
35
  func openSettings() throws -> Promise<Void>
37
36
  }
38
37
 
@@ -126,56 +126,44 @@ open class HybridNativeBleNitroSpec_cxx {
126
126
  }
127
127
 
128
128
  @inline(__always)
129
- public final func stopScan(callback: bridge.Func_void_bool_std__string) -> bridge.Result_void_ {
129
+ public final func stopScan() -> bridge.Result_bool_ {
130
130
  do {
131
- try self.__implementation.stopScan(callback: { () -> (Bool, String) -> Void in
132
- let __wrappedFunction = bridge.wrap_Func_void_bool_std__string(callback)
133
- return { (__success: Bool, __error: String) -> Void in
134
- __wrappedFunction.call(__success, std.string(__error))
135
- }
136
- }())
137
- return bridge.create_Result_void_()
131
+ let __result = try self.__implementation.stopScan()
132
+ let __resultCpp = __result
133
+ return bridge.create_Result_bool_(__resultCpp)
138
134
  } catch (let __error) {
139
135
  let __exceptionPtr = __error.toCpp()
140
- return bridge.create_Result_void_(__exceptionPtr)
136
+ return bridge.create_Result_bool_(__exceptionPtr)
141
137
  }
142
138
  }
143
139
 
144
140
  @inline(__always)
145
- public final func isScanning(callback: bridge.Func_void_bool) -> bridge.Result_void_ {
141
+ public final func isScanning() -> bridge.Result_bool_ {
146
142
  do {
147
- try self.__implementation.isScanning(callback: { () -> (Bool) -> Void in
148
- let __wrappedFunction = bridge.wrap_Func_void_bool(callback)
149
- return { (__result: Bool) -> Void in
150
- __wrappedFunction.call(__result)
151
- }
152
- }())
153
- return bridge.create_Result_void_()
143
+ let __result = try self.__implementation.isScanning()
144
+ let __resultCpp = __result
145
+ return bridge.create_Result_bool_(__resultCpp)
154
146
  } catch (let __error) {
155
147
  let __exceptionPtr = __error.toCpp()
156
- return bridge.create_Result_void_(__exceptionPtr)
148
+ return bridge.create_Result_bool_(__exceptionPtr)
157
149
  }
158
150
  }
159
151
 
160
152
  @inline(__always)
161
- public final func getConnectedDevices(callback: bridge.Func_void_std__vector_BLEDevice_) -> bridge.Result_void_ {
153
+ public final func getConnectedDevices(services: bridge.std__vector_std__string_) -> bridge.Result_std__vector_BLEDevice__ {
162
154
  do {
163
- try self.__implementation.getConnectedDevices(callback: { () -> ([BLEDevice]) -> Void in
164
- let __wrappedFunction = bridge.wrap_Func_void_std__vector_BLEDevice_(callback)
165
- return { (__devices: [BLEDevice]) -> Void in
166
- __wrappedFunction.call({ () -> bridge.std__vector_BLEDevice_ in
167
- var __vector = bridge.create_std__vector_BLEDevice_(__devices.count)
168
- for __item in __devices {
169
- __vector.push_back(__item)
170
- }
171
- return __vector
172
- }())
155
+ let __result = try self.__implementation.getConnectedDevices(services: services.map({ __item in String(__item) }))
156
+ let __resultCpp = { () -> bridge.std__vector_BLEDevice_ in
157
+ var __vector = bridge.create_std__vector_BLEDevice_(__result.count)
158
+ for __item in __result {
159
+ __vector.push_back(__item)
173
160
  }
174
- }())
175
- return bridge.create_Result_void_()
161
+ return __vector
162
+ }()
163
+ return bridge.create_Result_std__vector_BLEDevice__(__resultCpp)
176
164
  } catch (let __error) {
177
165
  let __exceptionPtr = __error.toCpp()
178
- return bridge.create_Result_void_(__exceptionPtr)
166
+ return bridge.create_Result_std__vector_BLEDevice__(__exceptionPtr)
179
167
  }
180
168
  }
181
169
 
@@ -223,18 +211,14 @@ open class HybridNativeBleNitroSpec_cxx {
223
211
  }
224
212
 
225
213
  @inline(__always)
226
- public final func isConnected(deviceId: std.string, callback: bridge.Func_void_bool) -> bridge.Result_void_ {
214
+ public final func isConnected(deviceId: std.string) -> bridge.Result_bool_ {
227
215
  do {
228
- try self.__implementation.isConnected(deviceId: String(deviceId), callback: { () -> (Bool) -> Void in
229
- let __wrappedFunction = bridge.wrap_Func_void_bool(callback)
230
- return { (__result: Bool) -> Void in
231
- __wrappedFunction.call(__result)
232
- }
233
- }())
234
- return bridge.create_Result_void_()
216
+ let __result = try self.__implementation.isConnected(deviceId: String(deviceId))
217
+ let __resultCpp = __result
218
+ return bridge.create_Result_bool_(__resultCpp)
235
219
  } catch (let __error) {
236
220
  let __exceptionPtr = __error.toCpp()
237
- return bridge.create_Result_void_(__exceptionPtr)
221
+ return bridge.create_Result_bool_(__exceptionPtr)
238
222
  }
239
223
  }
240
224
 
@@ -255,46 +239,38 @@ open class HybridNativeBleNitroSpec_cxx {
255
239
  }
256
240
 
257
241
  @inline(__always)
258
- public final func getServices(deviceId: std.string, callback: bridge.Func_void_std__vector_std__string_) -> bridge.Result_void_ {
242
+ public final func getServices(deviceId: std.string) -> bridge.Result_std__vector_std__string__ {
259
243
  do {
260
- try self.__implementation.getServices(deviceId: String(deviceId), callback: { () -> ([String]) -> Void in
261
- let __wrappedFunction = bridge.wrap_Func_void_std__vector_std__string_(callback)
262
- return { (__result: [String]) -> Void in
263
- __wrappedFunction.call({ () -> bridge.std__vector_std__string_ in
264
- var __vector = bridge.create_std__vector_std__string_(__result.count)
265
- for __item in __result {
266
- __vector.push_back(std.string(__item))
267
- }
268
- return __vector
269
- }())
244
+ let __result = try self.__implementation.getServices(deviceId: String(deviceId))
245
+ let __resultCpp = { () -> bridge.std__vector_std__string_ in
246
+ var __vector = bridge.create_std__vector_std__string_(__result.count)
247
+ for __item in __result {
248
+ __vector.push_back(std.string(__item))
270
249
  }
271
- }())
272
- return bridge.create_Result_void_()
250
+ return __vector
251
+ }()
252
+ return bridge.create_Result_std__vector_std__string__(__resultCpp)
273
253
  } catch (let __error) {
274
254
  let __exceptionPtr = __error.toCpp()
275
- return bridge.create_Result_void_(__exceptionPtr)
255
+ return bridge.create_Result_std__vector_std__string__(__exceptionPtr)
276
256
  }
277
257
  }
278
258
 
279
259
  @inline(__always)
280
- public final func getCharacteristics(deviceId: std.string, serviceId: std.string, callback: bridge.Func_void_std__vector_std__string_) -> bridge.Result_void_ {
260
+ public final func getCharacteristics(deviceId: std.string, serviceId: std.string) -> bridge.Result_std__vector_std__string__ {
281
261
  do {
282
- try self.__implementation.getCharacteristics(deviceId: String(deviceId), serviceId: String(serviceId), callback: { () -> ([String]) -> Void in
283
- let __wrappedFunction = bridge.wrap_Func_void_std__vector_std__string_(callback)
284
- return { (__result: [String]) -> Void in
285
- __wrappedFunction.call({ () -> bridge.std__vector_std__string_ in
286
- var __vector = bridge.create_std__vector_std__string_(__result.count)
287
- for __item in __result {
288
- __vector.push_back(std.string(__item))
289
- }
290
- return __vector
291
- }())
262
+ let __result = try self.__implementation.getCharacteristics(deviceId: String(deviceId), serviceId: String(serviceId))
263
+ let __resultCpp = { () -> bridge.std__vector_std__string_ in
264
+ var __vector = bridge.create_std__vector_std__string_(__result.count)
265
+ for __item in __result {
266
+ __vector.push_back(std.string(__item))
292
267
  }
293
- }())
294
- return bridge.create_Result_void_()
268
+ return __vector
269
+ }()
270
+ return bridge.create_Result_std__vector_std__string__(__resultCpp)
295
271
  } catch (let __error) {
296
272
  let __exceptionPtr = __error.toCpp()
297
- return bridge.create_Result_void_(__exceptionPtr)
273
+ return bridge.create_Result_std__vector_std__string__(__exceptionPtr)
298
274
  }
299
275
  }
300
276
 
@@ -379,22 +355,6 @@ open class HybridNativeBleNitroSpec_cxx {
379
355
  }
380
356
  }
381
357
 
382
- @inline(__always)
383
- public final func isBluetoothEnabled(callback: bridge.Func_void_bool) -> bridge.Result_void_ {
384
- do {
385
- try self.__implementation.isBluetoothEnabled(callback: { () -> (Bool) -> Void in
386
- let __wrappedFunction = bridge.wrap_Func_void_bool(callback)
387
- return { (__result: Bool) -> Void in
388
- __wrappedFunction.call(__result)
389
- }
390
- }())
391
- return bridge.create_Result_void_()
392
- } catch (let __error) {
393
- let __exceptionPtr = __error.toCpp()
394
- return bridge.create_Result_void_(__exceptionPtr)
395
- }
396
- }
397
-
398
358
  @inline(__always)
399
359
  public final func requestBluetoothEnable(callback: bridge.Func_void_bool_std__string) -> bridge.Result_void_ {
400
360
  do {
@@ -412,55 +372,43 @@ open class HybridNativeBleNitroSpec_cxx {
412
372
  }
413
373
 
414
374
  @inline(__always)
415
- public final func state(callback: bridge.Func_void_BLEState) -> bridge.Result_void_ {
375
+ public final func state() -> bridge.Result_BLEState_ {
416
376
  do {
417
- try self.__implementation.state(callback: { () -> (BLEState) -> Void in
418
- let __wrappedFunction = bridge.wrap_Func_void_BLEState(callback)
419
- return { (__state: BLEState) -> Void in
420
- __wrappedFunction.call(__state.rawValue)
421
- }
422
- }())
423
- return bridge.create_Result_void_()
377
+ let __result = try self.__implementation.state()
378
+ let __resultCpp = __result
379
+ return bridge.create_Result_BLEState_(__resultCpp)
424
380
  } catch (let __error) {
425
381
  let __exceptionPtr = __error.toCpp()
426
- return bridge.create_Result_void_(__exceptionPtr)
382
+ return bridge.create_Result_BLEState_(__exceptionPtr)
427
383
  }
428
384
  }
429
385
 
430
386
  @inline(__always)
431
- public final func subscribeToStateChange(stateCallback: bridge.Func_void_BLEState, resultCallback: bridge.Func_void_bool_std__string) -> bridge.Result_void_ {
387
+ public final func subscribeToStateChange(stateCallback: bridge.Func_void_BLEState) -> bridge.Result_OperationResult_ {
432
388
  do {
433
- try self.__implementation.subscribeToStateChange(stateCallback: { () -> (BLEState) -> Void in
389
+ let __result = try self.__implementation.subscribeToStateChange(stateCallback: { () -> (BLEState) -> Void in
434
390
  let __wrappedFunction = bridge.wrap_Func_void_BLEState(stateCallback)
435
391
  return { (__state: BLEState) -> Void in
436
392
  __wrappedFunction.call(__state.rawValue)
437
393
  }
438
- }(), resultCallback: { () -> (Bool, String) -> Void in
439
- let __wrappedFunction = bridge.wrap_Func_void_bool_std__string(resultCallback)
440
- return { (__success: Bool, __error: String) -> Void in
441
- __wrappedFunction.call(__success, std.string(__error))
442
- }
443
394
  }())
444
- return bridge.create_Result_void_()
395
+ let __resultCpp = __result
396
+ return bridge.create_Result_OperationResult_(__resultCpp)
445
397
  } catch (let __error) {
446
398
  let __exceptionPtr = __error.toCpp()
447
- return bridge.create_Result_void_(__exceptionPtr)
399
+ return bridge.create_Result_OperationResult_(__exceptionPtr)
448
400
  }
449
401
  }
450
402
 
451
403
  @inline(__always)
452
- public final func unsubscribeFromStateChange(resultCallback: bridge.Func_void_bool_std__string) -> bridge.Result_void_ {
404
+ public final func unsubscribeFromStateChange() -> bridge.Result_OperationResult_ {
453
405
  do {
454
- try self.__implementation.unsubscribeFromStateChange(resultCallback: { () -> (Bool, String) -> Void in
455
- let __wrappedFunction = bridge.wrap_Func_void_bool_std__string(resultCallback)
456
- return { (__success: Bool, __error: String) -> Void in
457
- __wrappedFunction.call(__success, std.string(__error))
458
- }
459
- }())
460
- return bridge.create_Result_void_()
406
+ let __result = try self.__implementation.unsubscribeFromStateChange()
407
+ let __resultCpp = __result
408
+ return bridge.create_Result_OperationResult_(__resultCpp)
461
409
  } catch (let __error) {
462
410
  let __exceptionPtr = __error.toCpp()
463
- return bridge.create_Result_void_(__exceptionPtr)
411
+ return bridge.create_Result_OperationResult_(__exceptionPtr)
464
412
  }
465
413
  }
466
414
 
@@ -0,0 +1,64 @@
1
+ ///
2
+ /// OperationResult.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import NitroModules
9
+
10
+ /**
11
+ * Represents an instance of `OperationResult`, backed by a C++ struct.
12
+ */
13
+ public typealias OperationResult = margelo.nitro.co.zyke.ble.OperationResult
14
+
15
+ public extension OperationResult {
16
+ private typealias bridge = margelo.nitro.co.zyke.ble.bridge.swift
17
+
18
+ /**
19
+ * Create a new instance of `OperationResult`.
20
+ */
21
+ init(success: Bool, error: String?) {
22
+ self.init(success, { () -> bridge.std__optional_std__string_ in
23
+ if let __unwrappedValue = error {
24
+ return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
25
+ } else {
26
+ return .init()
27
+ }
28
+ }())
29
+ }
30
+
31
+ var success: Bool {
32
+ @inline(__always)
33
+ get {
34
+ return self.__success
35
+ }
36
+ @inline(__always)
37
+ set {
38
+ self.__success = newValue
39
+ }
40
+ }
41
+
42
+ var error: String? {
43
+ @inline(__always)
44
+ get {
45
+ return { () -> String? in
46
+ if let __unwrapped = self.__error.value {
47
+ return String(__unwrapped)
48
+ } else {
49
+ return nil
50
+ }
51
+ }()
52
+ }
53
+ @inline(__always)
54
+ set {
55
+ self.__error = { () -> bridge.std__optional_std__string_ in
56
+ if let __unwrappedValue = newValue {
57
+ return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
58
+ } else {
59
+ return .init()
60
+ }
61
+ }()
62
+ }
63
+ }
64
+ }
@@ -28,7 +28,6 @@ namespace margelo::nitro::co::zyke::ble {
28
28
  prototype.registerHybridMethod("writeCharacteristic", &HybridNativeBleNitroSpec::writeCharacteristic);
29
29
  prototype.registerHybridMethod("subscribeToCharacteristic", &HybridNativeBleNitroSpec::subscribeToCharacteristic);
30
30
  prototype.registerHybridMethod("unsubscribeFromCharacteristic", &HybridNativeBleNitroSpec::unsubscribeFromCharacteristic);
31
- prototype.registerHybridMethod("isBluetoothEnabled", &HybridNativeBleNitroSpec::isBluetoothEnabled);
32
31
  prototype.registerHybridMethod("requestBluetoothEnable", &HybridNativeBleNitroSpec::requestBluetoothEnable);
33
32
  prototype.registerHybridMethod("state", &HybridNativeBleNitroSpec::state);
34
33
  prototype.registerHybridMethod("subscribeToStateChange", &HybridNativeBleNitroSpec::subscribeToStateChange);
@@ -19,14 +19,17 @@ namespace margelo::nitro::co::zyke::ble { struct ScanFilter; }
19
19
  namespace margelo::nitro::co::zyke::ble { struct BLEDevice; }
20
20
  // Forward declaration of `BLEState` to properly resolve imports.
21
21
  namespace margelo::nitro::co::zyke::ble { enum class BLEState; }
22
+ // Forward declaration of `OperationResult` to properly resolve imports.
23
+ namespace margelo::nitro::co::zyke::ble { struct OperationResult; }
22
24
 
23
25
  #include "ScanFilter.hpp"
24
26
  #include "BLEDevice.hpp"
25
27
  #include <functional>
26
- #include <string>
27
28
  #include <vector>
29
+ #include <string>
28
30
  #include <optional>
29
31
  #include "BLEState.hpp"
32
+ #include "OperationResult.hpp"
30
33
  #include <NitroModules/Promise.hpp>
31
34
 
32
35
  namespace margelo::nitro::co::zyke::ble {
@@ -61,24 +64,23 @@ namespace margelo::nitro::co::zyke::ble {
61
64
  public:
62
65
  // Methods
63
66
  virtual void startScan(const ScanFilter& filter, const std::function<void(const BLEDevice& /* device */)>& callback) = 0;
64
- virtual void stopScan(const std::function<void(bool /* success */, const std::string& /* error */)>& callback) = 0;
65
- virtual void isScanning(const std::function<void(bool /* result */)>& callback) = 0;
66
- virtual void getConnectedDevices(const std::function<void(const std::vector<BLEDevice>& /* devices */)>& callback) = 0;
67
+ virtual bool stopScan() = 0;
68
+ virtual bool isScanning() = 0;
69
+ virtual std::vector<BLEDevice> getConnectedDevices(const std::vector<std::string>& services) = 0;
67
70
  virtual void connect(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* deviceId */, const std::string& /* error */)>& callback, const std::optional<std::function<void(const std::string& /* deviceId */, bool /* interrupted */, const std::string& /* error */)>>& disconnectCallback) = 0;
68
71
  virtual void disconnect(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) = 0;
69
- virtual void isConnected(const std::string& deviceId, const std::function<void(bool /* result */)>& callback) = 0;
72
+ virtual bool isConnected(const std::string& deviceId) = 0;
70
73
  virtual void discoverServices(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) = 0;
71
- virtual void getServices(const std::string& deviceId, const std::function<void(const std::vector<std::string>& /* result */)>& callback) = 0;
72
- virtual void getCharacteristics(const std::string& deviceId, const std::string& serviceId, const std::function<void(const std::vector<std::string>& /* result */)>& callback) = 0;
74
+ virtual std::vector<std::string> getServices(const std::string& deviceId) = 0;
75
+ virtual std::vector<std::string> getCharacteristics(const std::string& deviceId, const std::string& serviceId) = 0;
73
76
  virtual void readCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(bool /* success */, const std::vector<double>& /* data */, const std::string& /* error */)>& callback) = 0;
74
77
  virtual void writeCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::vector<double>& data, bool withResponse, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) = 0;
75
78
  virtual void subscribeToCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(const std::string& /* characteristicId */, const std::vector<double>& /* data */)>& updateCallback, const std::function<void(bool /* success */, const std::string& /* error */)>& resultCallback) = 0;
76
79
  virtual void unsubscribeFromCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) = 0;
77
- virtual void isBluetoothEnabled(const std::function<void(bool /* result */)>& callback) = 0;
78
80
  virtual void requestBluetoothEnable(const std::function<void(bool /* success */, const std::string& /* error */)>& callback) = 0;
79
- virtual void state(const std::function<void(BLEState /* state */)>& callback) = 0;
80
- virtual void subscribeToStateChange(const std::function<void(BLEState /* state */)>& stateCallback, const std::function<void(bool /* success */, const std::string& /* error */)>& resultCallback) = 0;
81
- virtual void unsubscribeFromStateChange(const std::function<void(bool /* success */, const std::string& /* error */)>& resultCallback) = 0;
81
+ virtual BLEState state() = 0;
82
+ virtual OperationResult subscribeToStateChange(const std::function<void(BLEState /* state */)>& stateCallback) = 0;
83
+ virtual OperationResult unsubscribeFromStateChange() = 0;
82
84
  virtual std::shared_ptr<Promise<void>> openSettings() = 0;
83
85
 
84
86
  protected:
@@ -0,0 +1,72 @@
1
+ ///
2
+ /// OperationResult.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
11
+ #include <NitroModules/JSIConverter.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
16
+ #include <NitroModules/NitroDefines.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+
21
+
22
+
23
+ #include <string>
24
+ #include <optional>
25
+
26
+ namespace margelo::nitro::co::zyke::ble {
27
+
28
+ /**
29
+ * A struct which can be represented as a JavaScript object (OperationResult).
30
+ */
31
+ struct OperationResult {
32
+ public:
33
+ bool success SWIFT_PRIVATE;
34
+ std::optional<std::string> error SWIFT_PRIVATE;
35
+
36
+ public:
37
+ OperationResult() = default;
38
+ explicit OperationResult(bool success, std::optional<std::string> error): success(success), error(error) {}
39
+ };
40
+
41
+ } // namespace margelo::nitro::co::zyke::ble
42
+
43
+ namespace margelo::nitro {
44
+
45
+ // C++ OperationResult <> JS OperationResult (object)
46
+ template <>
47
+ struct JSIConverter<margelo::nitro::co::zyke::ble::OperationResult> final {
48
+ static inline margelo::nitro::co::zyke::ble::OperationResult fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
49
+ jsi::Object obj = arg.asObject(runtime);
50
+ return margelo::nitro::co::zyke::ble::OperationResult(
51
+ JSIConverter<bool>::fromJSI(runtime, obj.getProperty(runtime, "success")),
52
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "error"))
53
+ );
54
+ }
55
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::co::zyke::ble::OperationResult& arg) {
56
+ jsi::Object obj(runtime);
57
+ obj.setProperty(runtime, "success", JSIConverter<bool>::toJSI(runtime, arg.success));
58
+ obj.setProperty(runtime, "error", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.error));
59
+ return obj;
60
+ }
61
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
62
+ if (!value.isObject()) {
63
+ return false;
64
+ }
65
+ jsi::Object obj = value.getObject(runtime);
66
+ if (!JSIConverter<bool>::canConvert(runtime, obj.getProperty(runtime, "success"))) return false;
67
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "error"))) return false;
68
+ return true;
69
+ }
70
+ };
71
+
72
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ble-nitro",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "High-performance React Native BLE library built on Nitro Modules",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -51,20 +51,19 @@ describe('BleNitro', () => {
51
51
  );
52
52
  });
53
53
 
54
- test('stopScan calls native and resolves', async () => {
55
- mockNative.stopScan.mockImplementation((callback) => {
56
- callback(true, '');
54
+ test('stopScan calls native and resolves', () => {
55
+ mockNative.stopScan.mockImplementation(() => {
56
+ // stopScan is void, no callback needed
57
57
  });
58
58
 
59
- const result = await BleNitro.stopScan();
59
+ BleNitro.stopScan();
60
60
 
61
61
  expect(mockNative.stopScan).toHaveBeenCalled();
62
- expect(result).toBe(true);
63
62
  });
64
63
 
65
64
  test('connect calls native and resolves with device id', async () => {
66
65
  const deviceId = 'test-device';
67
- mockNative.connect.mockImplementation((id, callback, disconnectCallback) => {
66
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
68
67
  callback(true, id, '');
69
68
  });
70
69
 
@@ -75,21 +74,19 @@ describe('BleNitro', () => {
75
74
  });
76
75
 
77
76
  test('connect rejects on error', async () => {
78
- mockNative.connect.mockImplementation((id, callback, disconnectCallback) => {
77
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
79
78
  callback(false, '', 'Connection failed');
80
79
  });
81
80
 
82
81
  await expect(BleNitro.connect('test')).rejects.toThrow('Connection failed');
83
82
  });
84
83
 
85
- test('isBluetoothEnabled calls native', async () => {
86
- mockNative.isBluetoothEnabled.mockImplementation((callback) => {
87
- callback(true);
88
- });
84
+ test('isBluetoothEnabled calls native', () => {
85
+ mockNative.state.mockReturnValue(5); // PoweredOn
89
86
 
90
- const result = await BleNitro.isBluetoothEnabled();
87
+ const result = BleNitro.isBluetoothEnabled();
91
88
 
92
- expect(mockNative.isBluetoothEnabled).toHaveBeenCalled();
89
+ expect(mockNative.state).toHaveBeenCalled();
93
90
  expect(result).toBe(true);
94
91
  });
95
92
 
@@ -101,13 +98,13 @@ describe('BleNitro', () => {
101
98
 
102
99
  test('readCharacteristic works after connection', async () => {
103
100
  // First connect
104
- mockNative.connect.mockImplementation((id, callback, disconnectCallback) => {
101
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
105
102
  callback(true, id, '');
106
103
  });
107
104
  await BleNitro.connect('device');
108
105
 
109
106
  // Then read
110
- mockNative.readCharacteristic.mockImplementation((device, service, char, callback) => {
107
+ mockNative.readCharacteristic.mockImplementation((_device: string, _service: string, _char: string, callback: (success: boolean, data: number[], error: string) => void) => {
111
108
  callback(true, [85], ''); // Battery level 85%
112
109
  });
113
110
 
@@ -125,38 +122,38 @@ describe('BleNitro', () => {
125
122
 
126
123
  test('disconnect calls native', async () => {
127
124
  // First connect
128
- mockNative.connect.mockImplementation((id, callback, disconnectCallback) => {
125
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
129
126
  callback(true, id, '');
130
127
  });
131
128
  await BleNitro.connect('device');
132
129
 
133
130
  // Then disconnect
134
- mockNative.disconnect.mockImplementation((id, callback) => {
131
+ mockNative.disconnect.mockImplementation((_id: string, callback: (success: boolean, error: string) => void) => {
135
132
  callback(true, '');
136
133
  });
137
134
 
138
135
  const result = await BleNitro.disconnect('device');
139
136
 
140
137
  expect(mockNative.disconnect).toHaveBeenCalledWith('device', expect.any(Function));
141
- expect(result).toBe(true);
138
+ expect(result).toBe(undefined);
142
139
  });
143
140
 
144
141
  test('subscribeToCharacteristic calls callback', async () => {
145
142
  // First connect
146
- mockNative.connect.mockImplementation((id, callback, disconnectCallback) => {
143
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
147
144
  callback(true, id, '');
148
145
  });
149
146
  await BleNitro.connect('device');
150
147
 
151
148
  // Mock subscription
152
- mockNative.subscribeToCharacteristic.mockImplementation((device, service, char, updateCallback, resultCallback) => {
149
+ mockNative.subscribeToCharacteristic.mockImplementation((_device: string, _service: string, _char: string, updateCallback: (charId: string, data: number[]) => void, resultCallback: (success: boolean, error: string) => void) => {
153
150
  resultCallback(true, '');
154
151
  // Simulate notification
155
152
  updateCallback('char-id', [1, 2, 3]);
156
153
  });
157
154
 
158
155
  const notificationCallback = jest.fn();
159
- await BleNitro.subscribeToCharacteristic('device', 'service', 'char', notificationCallback);
156
+ BleNitro.subscribeToCharacteristic('device', 'service', 'char', notificationCallback);
160
157
 
161
158
  expect(mockNative.subscribeToCharacteristic).toHaveBeenCalled();
162
159
  expect(notificationCallback).toHaveBeenCalledWith('char-id', [1, 2, 3]);
@@ -166,7 +163,7 @@ describe('BleNitro', () => {
166
163
  const deviceId = 'test-device-2'; // Use different device ID to avoid state conflicts
167
164
  const onDisconnect = jest.fn();
168
165
 
169
- mockNative.connect.mockImplementation((id, callback, disconnectCallback) => {
166
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
170
167
  callback(true, id, '');
171
168
  // Simulate a disconnect event later
172
169
  if (disconnectCallback) {