react-native-ble-nitro 1.3.1 → 1.4.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.
Files changed (38) hide show
  1. package/README.md +37 -13
  2. package/android/src/main/java/com/margelo/nitro/co/zyke/ble/BleNitroBleManager.kt +48 -7
  3. package/ios/BleNitroBleManager.swift +95 -6
  4. package/ios/BlePeripheralDelegate.swift +13 -3
  5. package/lib/commonjs/index.d.ts +4 -194
  6. package/lib/commonjs/index.d.ts.map +1 -1
  7. package/lib/commonjs/index.js +6 -441
  8. package/lib/commonjs/index.js.map +1 -1
  9. package/lib/commonjs/manager.d.ts +204 -0
  10. package/lib/commonjs/manager.d.ts.map +1 -0
  11. package/lib/commonjs/manager.js +471 -0
  12. package/lib/commonjs/manager.js.map +1 -0
  13. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +4 -1
  14. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
  15. package/lib/index.d.ts +4 -194
  16. package/lib/index.js +3 -436
  17. package/lib/manager.d.ts +203 -0
  18. package/lib/manager.js +457 -0
  19. package/lib/specs/NativeBleNitro.nitro.d.ts +4 -1
  20. package/nitrogen/generated/android/BleNitroOnLoad.cpp +2 -0
  21. package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +102 -0
  22. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +9 -4
  23. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +2 -1
  24. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +81 -0
  25. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +11 -2
  26. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +8 -0
  27. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +38 -16
  28. package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +16 -10
  29. package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +47 -0
  30. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +2 -1
  31. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +27 -5
  32. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -0
  33. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +7 -6
  34. package/package.json +9 -1
  35. package/src/__tests__/index.test.ts +75 -15
  36. package/src/index.ts +24 -609
  37. package/src/manager.ts +629 -0
  38. package/src/specs/NativeBleNitro.nitro.ts +6 -1
@@ -2,6 +2,7 @@
2
2
  jest.mock('../specs/NativeBleNitro', () => ({
3
3
  __esModule: true,
4
4
  default: {
5
+ setRestoreStateCallback: jest.fn(),
5
6
  startScan: jest.fn(),
6
7
  stopScan: jest.fn(),
7
8
  isScanning: jest.fn(),
@@ -39,11 +40,14 @@ jest.mock('../specs/NativeBleNitro', () => ({
39
40
  },
40
41
  }));
41
42
 
42
- import { ble as BleNitro } from '../index';
43
+ import { BleNitro } from '../index';
43
44
 
44
45
  // Get reference to the mocked module
45
46
  const mockNative = require('../specs/NativeBleNitro').default; // eslint-disable-line @typescript-eslint/no-var-requires
46
47
 
48
+ // Get BLE instance
49
+ const BleManager = BleNitro.instance();
50
+
47
51
  describe('BleNitro', () => {
48
52
  beforeEach(() => {
49
53
  jest.clearAllMocks();
@@ -55,7 +59,7 @@ describe('BleNitro', () => {
55
59
  });
56
60
 
57
61
  const scanCallback = jest.fn();
58
- await BleNitro.startScan({ serviceUUIDs: ['test'] }, scanCallback);
62
+ BleManager.startScan({ serviceUUIDs: ['test'] }, scanCallback);
59
63
 
60
64
  expect(mockNative.startScan).toHaveBeenCalledWith(
61
65
  {
@@ -75,12 +79,12 @@ describe('BleNitro', () => {
75
79
  });
76
80
 
77
81
  const scanCallback = jest.fn();
78
- await BleNitro.startScan({ serviceUUIDs: ['test'] }, scanCallback);
82
+ BleManager.startScan({ serviceUUIDs: ['test'] }, scanCallback);
79
83
 
80
84
  // Now stop the scan
81
85
  mockNative.stopScan.mockImplementation(() => true);
82
86
 
83
- BleNitro.stopScan();
87
+ BleManager.stopScan();
84
88
 
85
89
  expect(mockNative.stopScan).toHaveBeenCalled();
86
90
  });
@@ -91,7 +95,7 @@ describe('BleNitro', () => {
91
95
  callback(true, id, '');
92
96
  });
93
97
 
94
- const result = await BleNitro.connect(deviceId);
98
+ const result = await BleManager.connect(deviceId);
95
99
 
96
100
  expect(mockNative.connect).toHaveBeenCalledWith(deviceId, expect.any(Function), undefined);
97
101
  expect(result).toBe(deviceId);
@@ -102,13 +106,13 @@ describe('BleNitro', () => {
102
106
  callback(false, '', 'Connection failed');
103
107
  });
104
108
 
105
- await expect(BleNitro.connect('test')).rejects.toThrow('Connection failed');
109
+ await expect(BleManager.connect('test')).rejects.toThrow('Connection failed');
106
110
  });
107
111
 
108
112
  test('isBluetoothEnabled calls native', () => {
109
113
  mockNative.state.mockReturnValue(5); // PoweredOn
110
114
 
111
- const result = BleNitro.isBluetoothEnabled();
115
+ const result = BleManager.isBluetoothEnabled();
112
116
 
113
117
  expect(mockNative.state).toHaveBeenCalled();
114
118
  expect(result).toBe(true);
@@ -117,16 +121,72 @@ describe('BleNitro', () => {
117
121
  test('writeCharacteristic requires connected device', async () => {
118
122
  const data = [1, 2, 3];
119
123
  await expect(
120
- BleNitro.writeCharacteristic('device', 'service', 'char',data)
124
+ BleManager.writeCharacteristic('device', 'service', 'char', data)
121
125
  ).rejects.toThrow('Device not connected');
122
126
  });
123
127
 
128
+ test('writeCharacteristic without response returns empty array', async () => {
129
+ // First connect
130
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
131
+ callback(true, id, '');
132
+ });
133
+ await BleManager.connect('device-write');
134
+
135
+ // Mock writeCharacteristic with new signature (success, responseData, error)
136
+ mockNative.writeCharacteristic.mockImplementation((_deviceId: string, _serviceId: string, _charId: string, _data: ArrayBuffer, withResponse: boolean, callback: (success: boolean, responseData: ArrayBuffer, error: string) => void) => {
137
+ // For withResponse=false, return empty ArrayBuffer
138
+ const emptyBuffer = new ArrayBuffer(0);
139
+ callback(true, emptyBuffer, '');
140
+ });
141
+
142
+ const data = [1, 2, 3];
143
+ const result = await BleManager.writeCharacteristic('device-write', 'service', 'char', data, false);
144
+
145
+ expect(mockNative.writeCharacteristic).toHaveBeenCalledWith(
146
+ 'device-write',
147
+ '0service-0000-1000-8000-00805f9b34fb',
148
+ '0000char-0000-1000-8000-00805f9b34fb',
149
+ expect.any(ArrayBuffer),
150
+ false,
151
+ expect.any(Function)
152
+ );
153
+ expect(result).toEqual([]); // Empty ByteArray for no response
154
+ });
155
+
156
+ test('writeCharacteristic with response returns response data', async () => {
157
+ // First connect
158
+ mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
159
+ callback(true, id, '');
160
+ });
161
+ await BleManager.connect('device-write-resp');
162
+
163
+ // Mock writeCharacteristic to return response data
164
+ mockNative.writeCharacteristic.mockImplementation((_deviceId: string, _serviceId: string, _charId: string, _data: ArrayBuffer, withResponse: boolean, callback: (success: boolean, responseData: ArrayBuffer, error: string) => void) => {
165
+ // For withResponse=true, return some response data
166
+ const responseData = new Uint8Array([0xAA, 0xBB, 0xCC]).buffer;
167
+ callback(true, responseData, '');
168
+ });
169
+
170
+ const data = [1, 2, 3];
171
+ const result = await BleManager.writeCharacteristic('device-write-resp', 'service', 'char', data, true);
172
+
173
+ expect(mockNative.writeCharacteristic).toHaveBeenCalledWith(
174
+ 'device-write-resp',
175
+ '0service-0000-1000-8000-00805f9b34fb',
176
+ '0000char-0000-1000-8000-00805f9b34fb',
177
+ expect.any(ArrayBuffer),
178
+ true,
179
+ expect.any(Function)
180
+ );
181
+ expect(result).toEqual([0xAA, 0xBB, 0xCC]); // Response data as ByteArray
182
+ });
183
+
124
184
  test('readCharacteristic works after connection', async () => {
125
185
  // First connect
126
186
  mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
127
187
  callback(true, id, '');
128
188
  });
129
- await BleNitro.connect('device');
189
+ await BleManager.connect('device');
130
190
 
131
191
  // Then read
132
192
  mockNative.readCharacteristic.mockImplementation((_device: string, _service: string, _char: string, callback: (success: boolean, data: ArrayBuffer, error: string) => void) => {
@@ -134,7 +194,7 @@ describe('BleNitro', () => {
134
194
  callback(true, testData.buffer, ''); // Battery level 85%
135
195
  });
136
196
 
137
- const result = await BleNitro.readCharacteristic('device', 'service', 'char');
197
+ const result = await BleManager.readCharacteristic('device', 'service', 'char');
138
198
 
139
199
  // UUIDs should be normalized in the call
140
200
  expect(mockNative.readCharacteristic).toHaveBeenCalledWith(
@@ -154,14 +214,14 @@ describe('BleNitro', () => {
154
214
  mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
155
215
  callback(true, id, '');
156
216
  });
157
- await BleNitro.connect('device');
217
+ await BleManager.connect('device');
158
218
 
159
219
  // Then disconnect
160
220
  mockNative.disconnect.mockImplementation((_id: string, callback: (success: boolean, error: string) => void) => {
161
221
  callback(true, '');
162
222
  });
163
223
 
164
- const result = await BleNitro.disconnect('device');
224
+ const result = await BleManager.disconnect('device');
165
225
 
166
226
  expect(mockNative.disconnect).toHaveBeenCalledWith('device', expect.any(Function));
167
227
  expect(result).toBe(undefined);
@@ -172,7 +232,7 @@ describe('BleNitro', () => {
172
232
  mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
173
233
  callback(true, id, '');
174
234
  });
175
- await BleNitro.connect('device');
235
+ await BleManager.connect('device');
176
236
 
177
237
  // Mock subscription
178
238
  mockNative.subscribeToCharacteristic.mockImplementation((_device: string, _service: string, _char: string, updateCallback: (charId: string, data: ArrayBuffer) => void, resultCallback: (success: boolean, error: string) => void) => {
@@ -183,7 +243,7 @@ describe('BleNitro', () => {
183
243
  });
184
244
 
185
245
  const notificationCallback = jest.fn();
186
- const subscription = BleNitro.subscribeToCharacteristic('device', 'service', 'char', notificationCallback);
246
+ const subscription = BleManager.subscribeToCharacteristic('device', 'service', 'char', notificationCallback);
187
247
 
188
248
  expect(mockNative.subscribeToCharacteristic).toHaveBeenCalled();
189
249
  expect(notificationCallback).toHaveBeenCalledWith('char-id', [1, 2, 3]);
@@ -207,7 +267,7 @@ describe('BleNitro', () => {
207
267
  }
208
268
  });
209
269
 
210
- const result = await BleNitro.connect(deviceId, onDisconnect);
270
+ const result = await BleManager.connect(deviceId, onDisconnect);
211
271
 
212
272
  expect(mockNative.connect).toHaveBeenCalledWith(deviceId, expect.any(Function), expect.any(Function));
213
273
  expect(result).toBe(deviceId);