react-native-ble-nitro 1.0.0-alpha.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 (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +298 -0
  3. package/android/build.gradle +55 -0
  4. package/android/src/main/AndroidManifest.xml +23 -0
  5. package/android/src/main/kotlin/co/zyke/ble/BleNitroBleManager.kt +651 -0
  6. package/android/src/main/kotlin/co/zyke/ble/BleNitroPackage.kt +37 -0
  7. package/ios/BleNitro.podspec +37 -0
  8. package/ios/BleNitroBleManager.swift +509 -0
  9. package/ios/BleNitroModule.swift +31 -0
  10. package/lib/BleManagerCompatFactory.d.ts +53 -0
  11. package/lib/BleManagerCompatFactory.js +191 -0
  12. package/lib/BleManagerFactory.d.ts +12 -0
  13. package/lib/BleManagerFactory.js +22 -0
  14. package/lib/compatibility/constants.d.ts +49 -0
  15. package/lib/compatibility/constants.js +50 -0
  16. package/lib/compatibility/deviceWrapper.d.ts +99 -0
  17. package/lib/compatibility/deviceWrapper.js +259 -0
  18. package/lib/compatibility/enums.d.ts +43 -0
  19. package/lib/compatibility/enums.js +124 -0
  20. package/lib/compatibility/index.d.ts +11 -0
  21. package/lib/compatibility/index.js +12 -0
  22. package/lib/compatibility/serviceData.d.ts +51 -0
  23. package/lib/compatibility/serviceData.js +70 -0
  24. package/lib/errors/BleError.d.ts +59 -0
  25. package/lib/errors/BleError.js +120 -0
  26. package/lib/index.d.ts +7 -0
  27. package/lib/index.js +12 -0
  28. package/lib/specs/BleManager.nitro.d.ts +36 -0
  29. package/lib/specs/BleManager.nitro.js +1 -0
  30. package/lib/specs/Characteristic.nitro.d.ts +26 -0
  31. package/lib/specs/Characteristic.nitro.js +1 -0
  32. package/lib/specs/Descriptor.nitro.d.ts +17 -0
  33. package/lib/specs/Descriptor.nitro.js +1 -0
  34. package/lib/specs/Device.nitro.d.ts +37 -0
  35. package/lib/specs/Device.nitro.js +1 -0
  36. package/lib/specs/Service.nitro.d.ts +19 -0
  37. package/lib/specs/Service.nitro.js +1 -0
  38. package/lib/specs/types.d.ts +228 -0
  39. package/lib/specs/types.js +146 -0
  40. package/lib/utils/base64.d.ts +25 -0
  41. package/lib/utils/base64.js +80 -0
  42. package/lib/utils/index.d.ts +2 -0
  43. package/lib/utils/index.js +2 -0
  44. package/lib/utils/uuid.d.ts +9 -0
  45. package/lib/utils/uuid.js +37 -0
  46. package/nitro.json +15 -0
  47. package/package.json +102 -0
  48. package/plugin/build/index.d.ts +28 -0
  49. package/plugin/build/index.js +29 -0
  50. package/plugin/build/withBleNitro.d.ts +31 -0
  51. package/plugin/build/withBleNitro.js +87 -0
  52. package/react-native.config.js +13 -0
  53. package/src/BleManagerCompatFactory.ts +373 -0
  54. package/src/BleManagerFactory.ts +30 -0
  55. package/src/__tests__/BleManager.test.ts +327 -0
  56. package/src/__tests__/compatibility/deviceWrapper.test.ts +563 -0
  57. package/src/__tests__/compatibility/enums.test.ts +254 -0
  58. package/src/compatibility/constants.ts +71 -0
  59. package/src/compatibility/deviceWrapper.ts +427 -0
  60. package/src/compatibility/enums.ts +160 -0
  61. package/src/compatibility/index.ts +24 -0
  62. package/src/compatibility/serviceData.ts +85 -0
  63. package/src/errors/BleError.ts +193 -0
  64. package/src/index.ts +30 -0
  65. package/src/specs/BleManager.nitro.ts +152 -0
  66. package/src/specs/Characteristic.nitro.ts +61 -0
  67. package/src/specs/Descriptor.nitro.ts +28 -0
  68. package/src/specs/Device.nitro.ts +104 -0
  69. package/src/specs/Service.nitro.ts +64 -0
  70. package/src/specs/types.ts +259 -0
  71. package/src/utils/base64.ts +80 -0
  72. package/src/utils/index.ts +2 -0
  73. package/src/utils/uuid.ts +45 -0
@@ -0,0 +1,254 @@
1
+ /**
2
+ * enums.test.ts
3
+ * React Native BLE Nitro - Enum Conversion Tests
4
+ * Copyright © 2025 Zyke (https://zyke.co)
5
+ */
6
+
7
+ import {
8
+ stateToString,
9
+ stringToState,
10
+ logLevelToString,
11
+ stringToLogLevel,
12
+ characteristicSubscriptionTypeToString,
13
+ stringToCharacteristicSubscriptionType,
14
+ normalizeState,
15
+ normalizeLogLevel,
16
+ normalizeCharacteristicSubscriptionType
17
+ } from '../../compatibility/enums';
18
+ import { State, LogLevel, CharacteristicSubscriptionType } from '../../specs/types';
19
+
20
+ describe('State Enum Conversions', () => {
21
+ describe('stateToString', () => {
22
+ it('should convert numeric state values to strings', () => {
23
+ expect(stateToString(State.Unknown)).toBe('Unknown');
24
+ expect(stateToString(State.Resetting)).toBe('Resetting');
25
+ expect(stateToString(State.Unsupported)).toBe('Unsupported');
26
+ expect(stateToString(State.Unauthorized)).toBe('Unauthorized');
27
+ expect(stateToString(State.PoweredOff)).toBe('PoweredOff');
28
+ expect(stateToString(State.PoweredOn)).toBe('PoweredOn');
29
+ });
30
+
31
+ it('should handle invalid state values', () => {
32
+ expect(stateToString(999 as State)).toBe('Unknown');
33
+ expect(stateToString(-1 as State)).toBe('Unknown');
34
+ });
35
+ });
36
+
37
+ describe('stringToState', () => {
38
+ it('should convert string state values to numeric enums', () => {
39
+ expect(stringToState('Unknown')).toBe(State.Unknown);
40
+ expect(stringToState('Resetting')).toBe(State.Resetting);
41
+ expect(stringToState('Unsupported')).toBe(State.Unsupported);
42
+ expect(stringToState('Unauthorized')).toBe(State.Unauthorized);
43
+ expect(stringToState('PoweredOff')).toBe(State.PoweredOff);
44
+ expect(stringToState('PoweredOn')).toBe(State.PoweredOn);
45
+ });
46
+
47
+ it('should handle case insensitive conversion', () => {
48
+ expect(stringToState('poweredon')).toBe(State.PoweredOn);
49
+ expect(stringToState('POWEREDOFF')).toBe(State.PoweredOff);
50
+ expect(stringToState('UnKnOwN')).toBe(State.Unknown);
51
+ });
52
+
53
+ it('should handle invalid string values', () => {
54
+ expect(stringToState('InvalidState')).toBe(State.Unknown);
55
+ expect(stringToState('')).toBe(State.Unknown);
56
+ expect(stringToState('null')).toBe(State.Unknown);
57
+ });
58
+ });
59
+
60
+ describe('normalizeState', () => {
61
+ it('should pass through numeric state values', () => {
62
+ expect(normalizeState(State.PoweredOn)).toBe(State.PoweredOn);
63
+ expect(normalizeState(State.PoweredOff)).toBe(State.PoweredOff);
64
+ });
65
+
66
+ it('should convert string state values to numeric', () => {
67
+ expect(normalizeState('PoweredOn' as any)).toBe(State.PoweredOn);
68
+ expect(normalizeState('PoweredOff' as any)).toBe(State.PoweredOff);
69
+ });
70
+ });
71
+ });
72
+
73
+ describe('LogLevel Enum Conversions', () => {
74
+ describe('logLevelToString', () => {
75
+ it('should convert numeric log level values to strings', () => {
76
+ expect(logLevelToString(LogLevel.None)).toBe('None');
77
+ expect(logLevelToString(LogLevel.Error)).toBe('Error');
78
+ expect(logLevelToString(LogLevel.Warning)).toBe('Warning');
79
+ expect(logLevelToString(LogLevel.Info)).toBe('Info');
80
+ expect(logLevelToString(LogLevel.Debug)).toBe('Debug');
81
+ expect(logLevelToString(LogLevel.Verbose)).toBe('Verbose');
82
+ });
83
+
84
+ it('should handle invalid log level values', () => {
85
+ expect(logLevelToString(999 as LogLevel)).toBe('None');
86
+ expect(logLevelToString(-1 as LogLevel)).toBe('None');
87
+ });
88
+ });
89
+
90
+ describe('stringToLogLevel', () => {
91
+ it('should convert string log level values to numeric enums', () => {
92
+ expect(stringToLogLevel('None')).toBe(LogLevel.None);
93
+ expect(stringToLogLevel('Error')).toBe(LogLevel.Error);
94
+ expect(stringToLogLevel('Warning')).toBe(LogLevel.Warning);
95
+ expect(stringToLogLevel('Info')).toBe(LogLevel.Info);
96
+ expect(stringToLogLevel('Debug')).toBe(LogLevel.Debug);
97
+ expect(stringToLogLevel('Verbose')).toBe(LogLevel.Verbose);
98
+ });
99
+
100
+ it('should handle case insensitive conversion', () => {
101
+ expect(stringToLogLevel('error')).toBe(LogLevel.Error);
102
+ expect(stringToLogLevel('WARNING')).toBe(LogLevel.Warning);
103
+ expect(stringToLogLevel('InFo')).toBe(LogLevel.Info);
104
+ });
105
+
106
+ it('should handle invalid string values', () => {
107
+ expect(stringToLogLevel('InvalidLevel')).toBe(LogLevel.None);
108
+ expect(stringToLogLevel('')).toBe(LogLevel.None);
109
+ expect(stringToLogLevel('trace')).toBe(LogLevel.None);
110
+ });
111
+ });
112
+
113
+ describe('normalizeLogLevel', () => {
114
+ it('should pass through numeric log level values', () => {
115
+ expect(normalizeLogLevel(LogLevel.Debug)).toBe(LogLevel.Debug);
116
+ expect(normalizeLogLevel(LogLevel.Error)).toBe(LogLevel.Error);
117
+ });
118
+
119
+ it('should convert string log level values to numeric', () => {
120
+ expect(normalizeLogLevel('Debug' as any)).toBe(LogLevel.Debug);
121
+ expect(normalizeLogLevel('Error' as any)).toBe(LogLevel.Error);
122
+ });
123
+ });
124
+ });
125
+
126
+ describe('CharacteristicSubscriptionType Enum Conversions', () => {
127
+ describe('characteristicSubscriptionTypeToString', () => {
128
+ it('should convert numeric subscription type values to strings', () => {
129
+ expect(characteristicSubscriptionTypeToString(CharacteristicSubscriptionType.Notification)).toBe('notification');
130
+ expect(characteristicSubscriptionTypeToString(CharacteristicSubscriptionType.Indication)).toBe('indication');
131
+ });
132
+
133
+ it('should handle invalid subscription type values', () => {
134
+ expect(characteristicSubscriptionTypeToString(999 as CharacteristicSubscriptionType)).toBe('notification');
135
+ expect(characteristicSubscriptionTypeToString(-1 as CharacteristicSubscriptionType)).toBe('notification');
136
+ });
137
+ });
138
+
139
+ describe('stringToCharacteristicSubscriptionType', () => {
140
+ it('should convert string subscription type values to numeric enums', () => {
141
+ expect(stringToCharacteristicSubscriptionType('Notification')).toBe(CharacteristicSubscriptionType.Notification);
142
+ expect(stringToCharacteristicSubscriptionType('Indication')).toBe(CharacteristicSubscriptionType.Indication);
143
+ });
144
+
145
+ it('should handle case insensitive conversion', () => {
146
+ expect(stringToCharacteristicSubscriptionType('notification')).toBe(CharacteristicSubscriptionType.Notification);
147
+ expect(stringToCharacteristicSubscriptionType('INDICATION')).toBe(CharacteristicSubscriptionType.Indication);
148
+ });
149
+
150
+ it('should handle invalid string values', () => {
151
+ expect(stringToCharacteristicSubscriptionType('InvalidType')).toBe(CharacteristicSubscriptionType.Notification);
152
+ expect(stringToCharacteristicSubscriptionType('')).toBe(CharacteristicSubscriptionType.Notification);
153
+ expect(stringToCharacteristicSubscriptionType('subscribe')).toBe(CharacteristicSubscriptionType.Notification);
154
+ });
155
+ });
156
+
157
+ describe('normalizeCharacteristicSubscriptionType', () => {
158
+ it('should pass through numeric subscription type values', () => {
159
+ expect(normalizeCharacteristicSubscriptionType(CharacteristicSubscriptionType.Indication)).toBe(CharacteristicSubscriptionType.Indication);
160
+ expect(normalizeCharacteristicSubscriptionType(CharacteristicSubscriptionType.Notification)).toBe(CharacteristicSubscriptionType.Notification);
161
+ });
162
+
163
+ it('should convert string subscription type values to numeric', () => {
164
+ expect(normalizeCharacteristicSubscriptionType('Indication' as any)).toBe(CharacteristicSubscriptionType.Indication);
165
+ expect(normalizeCharacteristicSubscriptionType('Notification' as any)).toBe(CharacteristicSubscriptionType.Notification);
166
+ });
167
+
168
+ it('should handle legacy string values from react-native-ble-plx', () => {
169
+ expect(normalizeCharacteristicSubscriptionType('notification')).toBe(CharacteristicSubscriptionType.Notification);
170
+ expect(normalizeCharacteristicSubscriptionType('indication')).toBe(CharacteristicSubscriptionType.Indication);
171
+ });
172
+ });
173
+ });
174
+
175
+ describe('Backward Compatibility', () => {
176
+ it('should maintain compatibility with react-native-ble-plx string values', () => {
177
+ // Test that the old string-based API still works through normalization
178
+ const state1 = normalizeState('PoweredOn' as any);
179
+ const state2 = normalizeState(State.PoweredOn);
180
+ expect(state1).toBe(state2);
181
+
182
+ const logLevel1 = normalizeLogLevel('Debug' as any);
183
+ const logLevel2 = normalizeLogLevel(LogLevel.Debug);
184
+ expect(logLevel1).toBe(logLevel2);
185
+
186
+ const subscriptionType1 = normalizeCharacteristicSubscriptionType('notification');
187
+ const subscriptionType2 = normalizeCharacteristicSubscriptionType(CharacteristicSubscriptionType.Notification);
188
+ expect(subscriptionType1).toBe(subscriptionType2);
189
+ });
190
+
191
+ it('should provide string representations for user-facing display', () => {
192
+ // Test that numeric enums can be converted back to user-friendly strings
193
+ expect(stateToString(State.PoweredOn)).toBe('PoweredOn');
194
+ expect(logLevelToString(LogLevel.Debug)).toBe('Debug');
195
+ expect(characteristicSubscriptionTypeToString(CharacteristicSubscriptionType.Notification)).toBe('notification');
196
+ });
197
+
198
+ it('should handle edge cases gracefully', () => {
199
+ // Test that invalid values don't crash and default to reasonable fallbacks
200
+ expect(() => stateToString(999 as State)).not.toThrow();
201
+ expect(() => stringToState('invalid')).not.toThrow();
202
+ expect(() => normalizeState(null as any)).not.toThrow();
203
+ expect(() => normalizeLogLevel(undefined as any)).not.toThrow();
204
+ });
205
+ });
206
+
207
+ describe('Enum Value Validation', () => {
208
+ it('should have correct numeric enum values', () => {
209
+ // Verify that our numeric enums match the expected values
210
+ expect(State.Unknown).toBe(0);
211
+ expect(State.Resetting).toBe(1);
212
+ expect(State.Unsupported).toBe(2);
213
+ expect(State.Unauthorized).toBe(3);
214
+ expect(State.PoweredOff).toBe(4);
215
+ expect(State.PoweredOn).toBe(5);
216
+
217
+ expect(LogLevel.None).toBe(0);
218
+ expect(LogLevel.Verbose).toBe(1);
219
+ expect(LogLevel.Debug).toBe(2);
220
+ expect(LogLevel.Info).toBe(3);
221
+ expect(LogLevel.Warning).toBe(4);
222
+ expect(LogLevel.Error).toBe(5);
223
+
224
+ expect(CharacteristicSubscriptionType.Notification).toBe(0);
225
+ expect(CharacteristicSubscriptionType.Indication).toBe(1);
226
+ });
227
+
228
+ it('should provide complete bidirectional conversion coverage', () => {
229
+ // Test that every enum value can be converted to string and back
230
+ const states = [State.Unknown, State.Resetting, State.Unsupported, State.Unauthorized, State.PoweredOff, State.PoweredOn];
231
+
232
+ states.forEach(state => {
233
+ const stringValue = stateToString(state);
234
+ const backToNumber = stringToState(stringValue);
235
+ expect(backToNumber).toBe(state);
236
+ });
237
+
238
+ const logLevels = [LogLevel.None, LogLevel.Error, LogLevel.Warning, LogLevel.Info, LogLevel.Debug, LogLevel.Verbose];
239
+
240
+ logLevels.forEach(level => {
241
+ const stringValue = logLevelToString(level);
242
+ const backToNumber = stringToLogLevel(stringValue);
243
+ expect(backToNumber).toBe(level);
244
+ });
245
+
246
+ const subscriptionTypes = [CharacteristicSubscriptionType.Notification, CharacteristicSubscriptionType.Indication];
247
+
248
+ subscriptionTypes.forEach(type => {
249
+ const stringValue = characteristicSubscriptionTypeToString(type);
250
+ const backToNumber = stringToCharacteristicSubscriptionType(stringValue);
251
+ expect(backToNumber).toBe(type);
252
+ });
253
+ });
254
+ });
@@ -0,0 +1,71 @@
1
+ /**
2
+ * React-native-ble-plx compatibility constants
3
+ *
4
+ * Re-exports all constants and types to maintain API compatibility
5
+ */
6
+
7
+ // Re-export original string-based constants for backward compatibility
8
+ export const State = {
9
+ Unknown: 'Unknown',
10
+ Resetting: 'Resetting',
11
+ Unsupported: 'Unsupported',
12
+ Unauthorized: 'Unauthorized',
13
+ PoweredOff: 'PoweredOff',
14
+ PoweredOn: 'PoweredOn',
15
+ } as const;
16
+
17
+ export const LogLevel = {
18
+ None: 'None',
19
+ Verbose: 'Verbose',
20
+ Debug: 'Debug',
21
+ Info: 'Info',
22
+ Warning: 'Warning',
23
+ Error: 'Error',
24
+ } as const;
25
+
26
+ // Type definitions for compatibility
27
+ export type StateString = typeof State[keyof typeof State];
28
+ export type LogLevelString = typeof LogLevel[keyof typeof LogLevel];
29
+
30
+ // Subscription type constants
31
+ export const CharacteristicSubscriptionType = {
32
+ Notification: 'notification',
33
+ Indication: 'indication',
34
+ } as const;
35
+
36
+ export type CharacteristicSubscriptionTypeString = typeof CharacteristicSubscriptionType[keyof typeof CharacteristicSubscriptionType];
37
+
38
+ // Connection options constants
39
+ export const RefreshGattMoment = {
40
+ OnConnected: 'OnConnected',
41
+ } as const;
42
+
43
+ export type RefreshGattMomentString = typeof RefreshGattMoment[keyof typeof RefreshGattMoment];
44
+
45
+ // Scan mode constants (these remain numeric as in original)
46
+ export const ScanMode = {
47
+ Opportunistic: -1,
48
+ LowPower: 0,
49
+ Balanced: 1,
50
+ LowLatency: 2,
51
+ } as const;
52
+
53
+ export const ScanCallbackType = {
54
+ AllMatches: 1,
55
+ FirstMatch: 2,
56
+ MatchLost: 4,
57
+ } as const;
58
+
59
+ export const ConnectionPriority = {
60
+ Balanced: 0,
61
+ High: 1,
62
+ LowPower: 2,
63
+ } as const;
64
+
65
+ // Re-export all BLE error codes
66
+ export {
67
+ BleErrorCode,
68
+ BleATTErrorCode,
69
+ BleIOSErrorCode,
70
+ BleAndroidErrorCode
71
+ } from '../specs/types';