react-native-ble-nitro 1.3.0 → 1.4.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.
- package/README.md +32 -10
- package/android/src/main/java/com/margelo/nitro/co/zyke/ble/BleNitroBleManager.kt +6 -0
- package/ios/BleNitroBleManager.swift +84 -2
- package/lib/commonjs/index.d.ts +4 -188
- 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 +469 -0
- package/lib/commonjs/manager.js.map +1 -0
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +2 -0
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
- package/lib/index.d.ts +4 -188
- package/lib/index.js +3 -436
- package/lib/manager.d.ts +203 -0
- package/lib/manager.js +455 -0
- package/lib/specs/NativeBleNitro.nitro.d.ts +2 -0
- 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 +6 -1
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +1 -0
- 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 +9 -0
- 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 +15 -9
- package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +47 -0
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +1 -0
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +22 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +6 -5
- package/package.json +9 -1
- package/src/__tests__/index.test.ts +24 -21
- package/src/index.ts +24 -600
- package/src/manager.ts +627 -0
- package/src/specs/NativeBleNitro.nitro.ts +4 -0
|
@@ -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,22 +106,22 @@ 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);
|
|
115
119
|
});
|
|
116
120
|
|
|
117
121
|
test('writeCharacteristic requires connected device', async () => {
|
|
118
|
-
const data =
|
|
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
|
|
|
@@ -126,7 +130,7 @@ describe('BleNitro', () => {
|
|
|
126
130
|
mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
|
|
127
131
|
callback(true, id, '');
|
|
128
132
|
});
|
|
129
|
-
await
|
|
133
|
+
await BleManager.connect('device');
|
|
130
134
|
|
|
131
135
|
// Then read
|
|
132
136
|
mockNative.readCharacteristic.mockImplementation((_device: string, _service: string, _char: string, callback: (success: boolean, data: ArrayBuffer, error: string) => void) => {
|
|
@@ -134,7 +138,7 @@ describe('BleNitro', () => {
|
|
|
134
138
|
callback(true, testData.buffer, ''); // Battery level 85%
|
|
135
139
|
});
|
|
136
140
|
|
|
137
|
-
const result = await
|
|
141
|
+
const result = await BleManager.readCharacteristic('device', 'service', 'char');
|
|
138
142
|
|
|
139
143
|
// UUIDs should be normalized in the call
|
|
140
144
|
expect(mockNative.readCharacteristic).toHaveBeenCalledWith(
|
|
@@ -144,10 +148,9 @@ describe('BleNitro', () => {
|
|
|
144
148
|
expect.any(Function)
|
|
145
149
|
);
|
|
146
150
|
|
|
147
|
-
// Result should be
|
|
148
|
-
expect(result).
|
|
149
|
-
|
|
150
|
-
expect(resultArray[0]).toBe(85);
|
|
151
|
+
// Result should be number array (ByteArray)
|
|
152
|
+
expect(Array.isArray(result)).toBe(true);
|
|
153
|
+
expect(result).toEqual([85]);
|
|
151
154
|
});
|
|
152
155
|
|
|
153
156
|
test('disconnect calls native', async () => {
|
|
@@ -155,14 +158,14 @@ describe('BleNitro', () => {
|
|
|
155
158
|
mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
|
|
156
159
|
callback(true, id, '');
|
|
157
160
|
});
|
|
158
|
-
await
|
|
161
|
+
await BleManager.connect('device');
|
|
159
162
|
|
|
160
163
|
// Then disconnect
|
|
161
164
|
mockNative.disconnect.mockImplementation((_id: string, callback: (success: boolean, error: string) => void) => {
|
|
162
165
|
callback(true, '');
|
|
163
166
|
});
|
|
164
167
|
|
|
165
|
-
const result = await
|
|
168
|
+
const result = await BleManager.disconnect('device');
|
|
166
169
|
|
|
167
170
|
expect(mockNative.disconnect).toHaveBeenCalledWith('device', expect.any(Function));
|
|
168
171
|
expect(result).toBe(undefined);
|
|
@@ -173,7 +176,7 @@ describe('BleNitro', () => {
|
|
|
173
176
|
mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
|
|
174
177
|
callback(true, id, '');
|
|
175
178
|
});
|
|
176
|
-
await
|
|
179
|
+
await BleManager.connect('device');
|
|
177
180
|
|
|
178
181
|
// Mock subscription
|
|
179
182
|
mockNative.subscribeToCharacteristic.mockImplementation((_device: string, _service: string, _char: string, updateCallback: (charId: string, data: ArrayBuffer) => void, resultCallback: (success: boolean, error: string) => void) => {
|
|
@@ -184,10 +187,10 @@ describe('BleNitro', () => {
|
|
|
184
187
|
});
|
|
185
188
|
|
|
186
189
|
const notificationCallback = jest.fn();
|
|
187
|
-
const subscription =
|
|
190
|
+
const subscription = BleManager.subscribeToCharacteristic('device', 'service', 'char', notificationCallback);
|
|
188
191
|
|
|
189
192
|
expect(mockNative.subscribeToCharacteristic).toHaveBeenCalled();
|
|
190
|
-
expect(notificationCallback).toHaveBeenCalledWith('char-id',
|
|
193
|
+
expect(notificationCallback).toHaveBeenCalledWith('char-id', [1, 2, 3]);
|
|
191
194
|
|
|
192
195
|
// Verify subscription object
|
|
193
196
|
expect(subscription).toHaveProperty('remove');
|
|
@@ -208,7 +211,7 @@ describe('BleNitro', () => {
|
|
|
208
211
|
}
|
|
209
212
|
});
|
|
210
213
|
|
|
211
|
-
const result = await
|
|
214
|
+
const result = await BleManager.connect(deviceId, onDisconnect);
|
|
212
215
|
|
|
213
216
|
expect(mockNative.connect).toHaveBeenCalledWith(deviceId, expect.any(Function), expect.any(Function));
|
|
214
217
|
expect(result).toBe(deviceId);
|