react-native-ble-nitro 1.3.1 → 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 +24 -0
- 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 -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 +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 -194
- 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 +19 -15
- package/src/index.ts +24 -609
- 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,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,7 +121,7 @@ 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
|
|
|
@@ -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(
|
|
@@ -154,14 +158,14 @@ describe('BleNitro', () => {
|
|
|
154
158
|
mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
|
|
155
159
|
callback(true, id, '');
|
|
156
160
|
});
|
|
157
|
-
await
|
|
161
|
+
await BleManager.connect('device');
|
|
158
162
|
|
|
159
163
|
// Then disconnect
|
|
160
164
|
mockNative.disconnect.mockImplementation((_id: string, callback: (success: boolean, error: string) => void) => {
|
|
161
165
|
callback(true, '');
|
|
162
166
|
});
|
|
163
167
|
|
|
164
|
-
const result = await
|
|
168
|
+
const result = await BleManager.disconnect('device');
|
|
165
169
|
|
|
166
170
|
expect(mockNative.disconnect).toHaveBeenCalledWith('device', expect.any(Function));
|
|
167
171
|
expect(result).toBe(undefined);
|
|
@@ -172,7 +176,7 @@ describe('BleNitro', () => {
|
|
|
172
176
|
mockNative.connect.mockImplementation((id: string, callback: (success: boolean, deviceId: string, error: string) => void, _disconnectCallback?: (deviceId: string, interrupted: boolean, error: string) => void) => {
|
|
173
177
|
callback(true, id, '');
|
|
174
178
|
});
|
|
175
|
-
await
|
|
179
|
+
await BleManager.connect('device');
|
|
176
180
|
|
|
177
181
|
// Mock subscription
|
|
178
182
|
mockNative.subscribeToCharacteristic.mockImplementation((_device: string, _service: string, _char: string, updateCallback: (charId: string, data: ArrayBuffer) => void, resultCallback: (success: boolean, error: string) => void) => {
|
|
@@ -183,7 +187,7 @@ describe('BleNitro', () => {
|
|
|
183
187
|
});
|
|
184
188
|
|
|
185
189
|
const notificationCallback = jest.fn();
|
|
186
|
-
const subscription =
|
|
190
|
+
const subscription = BleManager.subscribeToCharacteristic('device', 'service', 'char', notificationCallback);
|
|
187
191
|
|
|
188
192
|
expect(mockNative.subscribeToCharacteristic).toHaveBeenCalled();
|
|
189
193
|
expect(notificationCallback).toHaveBeenCalledWith('char-id', [1, 2, 3]);
|
|
@@ -207,7 +211,7 @@ describe('BleNitro', () => {
|
|
|
207
211
|
}
|
|
208
212
|
});
|
|
209
213
|
|
|
210
|
-
const result = await
|
|
214
|
+
const result = await BleManager.connect(deviceId, onDisconnect);
|
|
211
215
|
|
|
212
216
|
expect(mockNative.connect).toHaveBeenCalledWith(deviceId, expect.any(Function), expect.any(Function));
|
|
213
217
|
expect(result).toBe(deviceId);
|