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.
- package/README.md +37 -13
- package/android/src/main/java/com/margelo/nitro/co/zyke/ble/BleNitroBleManager.kt +48 -7
- package/ios/BleNitroBleManager.swift +95 -6
- package/ios/BlePeripheralDelegate.swift +13 -3
- package/lib/commonjs/index.d.ts +4 -194
- package/lib/commonjs/index.d.ts.map +1 -1
- package/lib/commonjs/index.js +6 -441
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/manager.d.ts +204 -0
- package/lib/commonjs/manager.d.ts.map +1 -0
- package/lib/commonjs/manager.js +471 -0
- package/lib/commonjs/manager.js.map +1 -0
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +4 -1
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
- package/lib/index.d.ts +4 -194
- package/lib/index.js +3 -436
- package/lib/manager.d.ts +203 -0
- package/lib/manager.js +457 -0
- package/lib/specs/NativeBleNitro.nitro.d.ts +4 -1
- package/nitrogen/generated/android/BleNitroOnLoad.cpp +2 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +102 -0
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +9 -4
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +81 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +11 -2
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +8 -0
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +38 -16
- package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +16 -10
- package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +47 -0
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +2 -1
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +27 -5
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +7 -6
- package/package.json +9 -1
- package/src/__tests__/index.test.ts +75 -15
- package/src/index.ts +24 -609
- package/src/manager.ts +629 -0
- 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 {
|
|
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
|
-
|
|
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
|
-
|
|
82
|
+
BleManager.startScan({ serviceUUIDs: ['test'] }, scanCallback);
|
|
79
83
|
|
|
80
84
|
// Now stop the scan
|
|
81
85
|
mockNative.stopScan.mockImplementation(() => true);
|
|
82
86
|
|
|
83
|
-
|
|
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
|
|
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(
|
|
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 =
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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 =
|
|
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
|
|
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);
|