react-native-ble-nitro 1.0.0 → 1.2.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 (58) hide show
  1. package/README.md +25 -17
  2. package/ios/BleNitroBleManager.swift +126 -57
  3. package/ios/BlePeripheralDelegate.swift +36 -8
  4. package/lib/commonjs/index.d.ts +29 -20
  5. package/lib/commonjs/index.d.ts.map +1 -1
  6. package/lib/commonjs/index.js +113 -143
  7. package/lib/commonjs/index.js.map +1 -1
  8. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +19 -14
  9. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
  10. package/lib/commonjs/specs/NativeBleNitro.nitro.js.map +1 -1
  11. package/lib/index.d.ts +29 -20
  12. package/lib/index.js +112 -142
  13. package/lib/specs/NativeBleNitro.nitro.d.ts +19 -14
  14. package/nitrogen/generated/android/BleNitroOnLoad.cpp +6 -12
  15. package/nitrogen/generated/android/c++/JBLEDevice.hpp +3 -0
  16. package/nitrogen/generated/android/c++/JFunc_void_BLEDevice.hpp +3 -0
  17. package/nitrogen/generated/android/c++/JFunc_void_bool_std__shared_ptr_ArrayBuffer__std__string.hpp +78 -0
  18. package/nitrogen/generated/android/c++/JFunc_void_std__string_std__shared_ptr_ArrayBuffer_.hpp +78 -0
  19. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +111 -65
  20. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +13 -13
  21. package/nitrogen/generated/android/c++/JManufacturerData.hpp +3 -0
  22. package/nitrogen/generated/android/c++/JManufacturerDataEntry.hpp +7 -15
  23. package/nitrogen/generated/android/c++/JOperationResult.hpp +58 -0
  24. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/{Func_void_bool_std__vector_double__std__string.kt → Func_void_bool_std__shared_ptr_ArrayBuffer__std__string.kt} +12 -12
  25. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/{Func_void_std__string_std__vector_double_.kt → Func_void_std__string_std__shared_ptr_ArrayBuffer_.kt} +12 -12
  26. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +20 -65
  27. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/ManufacturerDataEntry.kt +1 -1
  28. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/OperationResult.kt +32 -0
  29. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +15 -39
  30. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +108 -113
  31. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Umbrella.hpp +6 -0
  32. package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +57 -28
  33. package/nitrogen/generated/ios/swift/{Func_void_bool_std__vector_double__std__string.swift → Func_void_bool_std__shared_ptr_ArrayBuffer__std__string.swift} +11 -11
  34. package/nitrogen/generated/ios/swift/{Func_void_std__string_std__vector_double_.swift → Func_void_std__string_std__shared_ptr_ArrayBuffer_.swift} +11 -11
  35. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +13 -13
  36. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +84 -136
  37. package/nitrogen/generated/ios/swift/ManufacturerDataEntry.swift +5 -17
  38. package/nitrogen/generated/ios/swift/OperationResult.swift +64 -0
  39. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -1
  40. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +20 -14
  41. package/nitrogen/generated/shared/c++/ManufacturerDataEntry.hpp +8 -7
  42. package/nitrogen/generated/shared/c++/OperationResult.hpp +72 -0
  43. package/package.json +1 -1
  44. package/react-native.config.js +10 -2
  45. package/src/__tests__/index.test.ts +19 -22
  46. package/src/index.ts +147 -164
  47. package/src/specs/NativeBleNitro.nitro.ts +22 -14
  48. package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +0 -74
  49. package/nitrogen/generated/android/c++/JFunc_void_bool_std__vector_double__std__string.hpp +0 -86
  50. package/nitrogen/generated/android/c++/JFunc_void_std__string_std__vector_double_.hpp +0 -86
  51. package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +0 -99
  52. package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +0 -93
  53. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_bool.kt +0 -81
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +0 -81
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_std__string_.kt +0 -81
  56. package/nitrogen/generated/ios/swift/Func_void_bool.swift +0 -47
  57. package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +0 -47
  58. package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +0 -47
@@ -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.2.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",
@@ -1,8 +1,16 @@
1
+ // https://github.com/react-native-community/cli/blob/main/docs/dependencies.md
2
+
1
3
  module.exports = {
2
4
  dependency: {
3
5
  platforms: {
4
- android: {},
6
+ /**
7
+ * @type {import('@react-native-community/cli-types').IOSDependencyParams}
8
+ */
5
9
  ios: {},
10
+ /**
11
+ * @type {import('@react-native-community/cli-types').AndroidDependencyParams}
12
+ */
13
+ android: {},
6
14
  },
7
15
  },
8
- };
16
+ }
@@ -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) {