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,259 @@
1
+ // Base types from react-native-ble-plx
2
+ export type Base64 = string;
3
+ export type UUID = string;
4
+ export type Identifier = number;
5
+ export type DeviceId = string;
6
+ export type TransactionId = string;
7
+
8
+ export enum RefreshGattMoment {
9
+ OnConnected = 0
10
+ }
11
+
12
+ export enum CharacteristicSubscriptionType {
13
+ Notification = 0,
14
+ Indication = 1
15
+ }
16
+
17
+ // Service Data structure for BLE devices - since Nitro doesn't support index signatures,
18
+ // we'll use a more concrete structure or pass it as Base64 and handle parsing in native code
19
+ export interface ServiceDataEntry {
20
+ uuid: UUID;
21
+ data: Base64;
22
+ }
23
+
24
+ // Enums
25
+ export enum State {
26
+ Unknown = 0,
27
+ Resetting = 1,
28
+ Unsupported = 2,
29
+ Unauthorized = 3,
30
+ PoweredOff = 4,
31
+ PoweredOn = 5
32
+ }
33
+
34
+ export enum LogLevel {
35
+ None = 0,
36
+ Verbose = 1,
37
+ Debug = 2,
38
+ Info = 3,
39
+ Warning = 4,
40
+ Error = 5
41
+ }
42
+
43
+ export enum ScanMode {
44
+ Opportunistic = -1,
45
+ LowPower = 0,
46
+ Balanced = 1,
47
+ LowLatency = 2
48
+ }
49
+
50
+ export enum ScanCallbackType {
51
+ AllMatches = 1,
52
+ FirstMatch = 2,
53
+ MatchLost = 4
54
+ }
55
+
56
+ export enum ConnectionPriority {
57
+ Balanced = 0,
58
+ High = 1,
59
+ LowPower = 2
60
+ }
61
+
62
+ // Complex error code enums
63
+ export enum BleErrorCode {
64
+ UnknownError = 0,
65
+ BluetoothManagerDestroyed = 1,
66
+ OperationCancelled = 2,
67
+ OperationTimedOut = 3,
68
+ OperationStartFailed = 4,
69
+ InvalidIdentifiers = 5,
70
+ BluetoothUnsupported = 100,
71
+ BluetoothUnauthorized = 101,
72
+ BluetoothPoweredOff = 102,
73
+ BluetoothInUnknownState = 103,
74
+ BluetoothResetting = 104,
75
+ BluetoothStateChangeFailed = 105,
76
+ DeviceConnectionFailed = 200,
77
+ DeviceDisconnected = 201,
78
+ DeviceRSSIReadFailed = 202,
79
+ DeviceAlreadyConnected = 203,
80
+ DeviceNotFound = 204,
81
+ DeviceNotConnected = 205,
82
+ DeviceMTUChangeFailed = 206,
83
+ ServicesDiscoveryFailed = 300,
84
+ IncludedServicesDiscoveryFailed = 301,
85
+ ServiceNotFound = 302,
86
+ ServicesNotDiscovered = 303,
87
+ CharacteristicsDiscoveryFailed = 400,
88
+ CharacteristicWriteFailed = 401,
89
+ CharacteristicReadFailed = 402,
90
+ CharacteristicNotifyChangeFailed = 403,
91
+ CharacteristicNotFound = 404,
92
+ CharacteristicsNotDiscovered = 405,
93
+ CharacteristicInvalidDataFormat = 406,
94
+ DescriptorsDiscoveryFailed = 500,
95
+ DescriptorWriteFailed = 501,
96
+ DescriptorReadFailed = 502,
97
+ DescriptorNotFound = 503,
98
+ DescriptorsNotDiscovered = 504,
99
+ DescriptorInvalidDataFormat = 505,
100
+ DescriptorWriteNotAllowed = 506,
101
+ ScanStartFailed = 600,
102
+ LocationServicesDisabled = 601
103
+ }
104
+
105
+ export enum BleATTErrorCode {
106
+ Success = 0,
107
+ InvalidHandle = 1,
108
+ ReadNotPermitted = 2,
109
+ WriteNotPermitted = 3,
110
+ InvalidPdu = 4,
111
+ InsufficientAuthentication = 5,
112
+ RequestNotSupported = 6,
113
+ InvalidOffset = 7,
114
+ InsufficientAuthorization = 8,
115
+ PrepareQueueFull = 9,
116
+ AttributeNotFound = 10,
117
+ AttributeNotLong = 11,
118
+ InsufficientEncryptionKeySize = 12,
119
+ InvalidAttributeValueLength = 13,
120
+ UnlikelyError = 14,
121
+ InsufficientEncryption = 15,
122
+ UnsupportedGroupType = 16,
123
+ InsufficientResources = 17
124
+ }
125
+
126
+ export enum BleIOSErrorCode {
127
+ Unknown = 0,
128
+ InvalidParameters = 1,
129
+ InvalidHandle = 2,
130
+ NotConnected = 3,
131
+ OutOfSpace = 4,
132
+ OperationCancelled = 5,
133
+ ConnectionTimeout = 6,
134
+ PeripheralDisconnected = 7,
135
+ UuidNotAllowed = 8,
136
+ AlreadyAdvertising = 9,
137
+ ConnectionFailed = 10,
138
+ ConnectionLimitReached = 11,
139
+ UnknownDevice = 12
140
+ }
141
+
142
+ export enum BleAndroidErrorCode {
143
+ NoResources = 0x80,
144
+ InternalError = 0x81,
145
+ WrongState = 0x82,
146
+ DbFull = 0x83,
147
+ Busy = 0x84,
148
+ Error = 0x85,
149
+ CmdStarted = 0x86,
150
+ IllegalParameter = 0x87,
151
+ Pending = 0x88,
152
+ AuthFail = 0x89,
153
+ More = 0x8a,
154
+ InvalidCfg = 0x8b,
155
+ ServiceStarted = 0x8c,
156
+ EncrypedNoMitm = 0x8d,
157
+ NotEncrypted = 0x8e,
158
+ Congested = 0x8f
159
+ }
160
+
161
+ // Interfaces for configuration and options
162
+ export interface ScanOptions {
163
+ allowDuplicates?: boolean;
164
+ scanMode?: ScanMode;
165
+ callbackType?: ScanCallbackType;
166
+ legacyScan?: boolean;
167
+ }
168
+
169
+ export interface ConnectionOptions {
170
+ autoConnect: boolean;
171
+ requestMTU: number;
172
+ timeout: number;
173
+ }
174
+
175
+ export interface BleManagerOptions {
176
+ restoreStateIdentifier?: string;
177
+ restoreStateFunction?: (restoredState: BleRestoredState | null) => void;
178
+ errorCodesToMessagesMapping?: BleErrorCodeMessageMapping;
179
+ }
180
+
181
+ export interface BleRestoredState {
182
+ connectedPeripherals: NativeDevice[];
183
+ }
184
+
185
+ export type BleErrorCodeMessageMapping = { [key in BleErrorCode]: string };
186
+
187
+ // Native device/service/characteristic/descriptor interfaces
188
+ export interface NativeDevice {
189
+ id: DeviceId;
190
+ name: string | null;
191
+ rssi: number | null;
192
+ mtu: number;
193
+ manufacturerData: Base64 | null;
194
+ rawScanRecord: Base64;
195
+ serviceData: ServiceDataEntry[] | null;
196
+ serviceUUIDs: UUID[] | null;
197
+ localName: string | null;
198
+ txPowerLevel: number | null;
199
+ solicitedServiceUUIDs: UUID[] | null;
200
+ isConnectable: boolean | null;
201
+ overflowServiceUUIDs: UUID[] | null;
202
+ }
203
+
204
+ export interface NativeService {
205
+ id: Identifier;
206
+ uuid: UUID;
207
+ deviceID: DeviceId;
208
+ isPrimary: boolean;
209
+ }
210
+
211
+ export interface NativeCharacteristic {
212
+ id: Identifier;
213
+ uuid: UUID;
214
+ serviceID: Identifier;
215
+ serviceUUID: UUID;
216
+ deviceID: DeviceId;
217
+ isReadable: boolean;
218
+ isWritableWithResponse: boolean;
219
+ isWritableWithoutResponse: boolean;
220
+ isNotifiable: boolean;
221
+ isNotifying: boolean;
222
+ isIndicatable: boolean;
223
+ value: Base64 | null;
224
+ }
225
+
226
+ export interface NativeDescriptor {
227
+ id: Identifier;
228
+ uuid: UUID;
229
+ characteristicID: Identifier;
230
+ characteristicUUID: UUID;
231
+ serviceID: Identifier;
232
+ serviceUUID: UUID;
233
+ deviceID: DeviceId;
234
+ value: Base64 | null;
235
+ }
236
+
237
+ export interface NativeBleError {
238
+ errorCode: BleErrorCode;
239
+ attErrorCode: BleATTErrorCode | null;
240
+ iosErrorCode: BleIOSErrorCode | null;
241
+ androidErrorCode: BleAndroidErrorCode | null;
242
+ reason: string | null;
243
+ deviceID?: string;
244
+ serviceUUID?: string;
245
+ characteristicUUID?: string;
246
+ descriptorUUID?: string;
247
+ internalMessage?: string;
248
+ }
249
+
250
+ // Subscription interface
251
+ export interface Subscription {
252
+ remove(): void;
253
+ }
254
+
255
+ // Callback types for listeners
256
+ export type StateListener = (newState: State) => void;
257
+ export type DeviceScanListener = (error: NativeBleError | null, scannedDevice: NativeDevice | null) => void;
258
+ export type DeviceDisconnectedListener = (error: NativeBleError | null, device: NativeDevice | null) => void;
259
+ export type CharacteristicMonitorListener = (error: NativeBleError | null, characteristic: NativeCharacteristic | null) => void;
@@ -0,0 +1,80 @@
1
+ import type { Base64 } from '../specs/types';
2
+
3
+ /**
4
+ * Utility functions for Base64 encoding/decoding
5
+ * Maintains compatibility with react-native-ble-plx Base64 operations
6
+ */
7
+
8
+ /**
9
+ * Converts a string to Base64
10
+ */
11
+ export function stringToBase64(str: string): Base64 {
12
+ if (typeof Buffer !== 'undefined') {
13
+ // Node.js environment
14
+ return Buffer.from(str, 'utf8').toString('base64');
15
+ } else if (typeof btoa !== 'undefined') {
16
+ // Browser environment
17
+ return btoa(str);
18
+ } else {
19
+ throw new Error('Base64 encoding not supported in this environment');
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Converts Base64 to string
25
+ */
26
+ export function base64ToString(base64: Base64): string {
27
+ if (typeof Buffer !== 'undefined') {
28
+ // Node.js environment
29
+ return Buffer.from(base64, 'base64').toString('utf8');
30
+ } else if (typeof atob !== 'undefined') {
31
+ // Browser environment
32
+ return atob(base64);
33
+ } else {
34
+ throw new Error('Base64 decoding not supported in this environment');
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Converts Uint8Array to Base64
40
+ */
41
+ export function uint8ArrayToBase64(uint8Array: Uint8Array): Base64 {
42
+ if (typeof Buffer !== 'undefined') {
43
+ // Node.js environment
44
+ return Buffer.from(uint8Array).toString('base64');
45
+ } else {
46
+ // Browser environment
47
+ const binary = Array.from(uint8Array, byte => String.fromCharCode(byte)).join('');
48
+ return btoa(binary);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Converts Base64 to Uint8Array
54
+ */
55
+ export function base64ToUint8Array(base64: Base64): Uint8Array {
56
+ if (typeof Buffer !== 'undefined') {
57
+ // Node.js environment
58
+ return new Uint8Array(Buffer.from(base64, 'base64'));
59
+ } else {
60
+ // Browser environment
61
+ const binary = atob(base64);
62
+ const uint8Array = new Uint8Array(binary.length);
63
+ for (let i = 0; i < binary.length; i++) {
64
+ uint8Array[i] = binary.charCodeAt(i);
65
+ }
66
+ return uint8Array;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Validates if a string is valid Base64
72
+ */
73
+ export function isValidBase64(str: string): boolean {
74
+ try {
75
+ const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
76
+ return base64Regex.test(str) && (str.length % 4 === 0);
77
+ } catch {
78
+ return false;
79
+ }
80
+ }
@@ -0,0 +1,2 @@
1
+ export * from './uuid';
2
+ export * from './base64';
@@ -0,0 +1,45 @@
1
+ import type { UUID } from '../specs/types';
2
+
3
+ /**
4
+ * Converts UUID to full 128bit, lowercase format which should be used to compare UUID values.
5
+ * This function maintains 100% compatibility with react-native-ble-plx
6
+ *
7
+ * @param uuid 16bit, 32bit or 128bit UUID.
8
+ * @returns 128bit lowercase UUID.
9
+ */
10
+ export function fullUUID(uuid: UUID): UUID {
11
+ if (typeof uuid !== 'string') {
12
+ throw new Error('UUID must be a string');
13
+ }
14
+
15
+ // Remove dashes and convert to lowercase
16
+ const cleanUuid = uuid.replace(/-/g, '').toLowerCase();
17
+
18
+ // 16-bit UUID (4 characters)
19
+ if (cleanUuid.length === 4) {
20
+ return `0000${cleanUuid}-0000-1000-8000-00805f9b34fb`;
21
+ }
22
+
23
+ // 32-bit UUID (8 characters)
24
+ if (cleanUuid.length === 8) {
25
+ return `${cleanUuid}-0000-1000-8000-00805f9b34fb`;
26
+ }
27
+
28
+ // 128-bit UUID (32 characters)
29
+ if (cleanUuid.length === 32) {
30
+ return [
31
+ cleanUuid.substring(0, 8),
32
+ cleanUuid.substring(8, 12),
33
+ cleanUuid.substring(12, 16),
34
+ cleanUuid.substring(16, 20),
35
+ cleanUuid.substring(20, 32)
36
+ ].join('-');
37
+ }
38
+
39
+ // Already formatted UUID with dashes
40
+ if (cleanUuid.length === 36 && uuid.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)) {
41
+ return uuid.toLowerCase();
42
+ }
43
+
44
+ throw new Error(`Invalid UUID format: ${uuid}`);
45
+ }