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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Zyke (https://zyke.co)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,298 @@
1
+ # react-native-ble-nitro
2
+
3
+ [![npm version](https://badge.fury.io/js/react-native-ble-nitro.svg)](https://badge.fury.io/js/react-native-ble-nitro)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A high-performance React Native BLE library built on [Nitro Modules](https://nitro.margelo.com/) - **drop-in replacement** for `react-native-ble-plx` with significantly better performance and stability.
7
+
8
+ Originally developed for [Zyke Band](https://zykeband.com) - a fitness and health tracker created by a small team.
9
+
10
+ ## ✨ Features
11
+
12
+ - 🚀 **High Performance**: Built on Nitro Modules with JSI for zero-overhead native communication
13
+ - 🔄 **100% Compatible**: Drop-in replacement for `react-native-ble-plx@3.5.0`
14
+ - 📱 **Cross-Platform**: iOS and Android support with platform-specific optimizations
15
+ - 🎯 **Type-Safe**: Full TypeScript support with comprehensive type definitions
16
+ - 🔧 **Expo Ready**: Built-in Expo config plugin for easy setup
17
+ - 🏗️ **New Architecture**: Full support for React Native's new architecture
18
+ - ⚡ **Zero Bridge**: Direct JSI communication eliminates bridge bottlenecks
19
+ - 🛡️ **Reliable**: Swift/Kotlin native implementations for maximum stability
20
+
21
+ ## 🚀 Quick Start
22
+
23
+ ### Installation
24
+
25
+ ```bash
26
+ npm install react-native-nitro-modules react-native-ble-nitro
27
+ ```
28
+
29
+ ### Expo Setup
30
+
31
+ Add the plugin to your `app.json` or `app.config.js`:
32
+
33
+ ```json
34
+ {
35
+ "expo": {
36
+ "plugins": [
37
+ [
38
+ "react-native-ble-nitro",
39
+ {
40
+ "isBackgroundEnabled": true,
41
+ "modes": ["peripheral", "central"],
42
+ "bluetoothAlwaysPermission": "Allow $(PRODUCT_NAME) to connect to bluetooth devices"
43
+ }
44
+ ]
45
+ ]
46
+ }
47
+ }
48
+ ```
49
+
50
+ Then prebuild and run:
51
+
52
+ ```bash
53
+ npx expo prebuild
54
+ npx expo run:android
55
+ # or
56
+ npx expo run:ios
57
+ ```
58
+
59
+ ### React Native CLI Setup
60
+
61
+ For bare React Native projects, the library auto-links. Just run:
62
+
63
+ ```bash
64
+ npx pod-install # iOS only
65
+ ```
66
+
67
+ ## 📖 Usage
68
+
69
+ The API is 100% identical to `react-native-ble-plx`:
70
+
71
+ ```typescript
72
+ import { BleManager, State } from 'react-native-ble-nitro';
73
+
74
+ const manager = new BleManager();
75
+
76
+ // Check Bluetooth state
77
+ const state = await manager.state();
78
+ if (state === State.PoweredOn) {
79
+ console.log('Bluetooth is ready!');
80
+ }
81
+
82
+ // Scan for devices
83
+ manager.startDeviceScan(null, null, (error, device) => {
84
+ if (error) {
85
+ console.error(error);
86
+ return;
87
+ }
88
+
89
+ console.log('Found device:', device.name || 'Unknown');
90
+
91
+ if (device.name === 'MyDevice') {
92
+ manager.stopDeviceScan();
93
+
94
+ // Connect to device
95
+ device.connect()
96
+ .then(() => device.discoverAllServicesAndCharacteristics())
97
+ .then(() => {
98
+ // Your BLE operations here
99
+ console.log('Connected and ready!');
100
+ })
101
+ .catch(console.error);
102
+ }
103
+ });
104
+ ```
105
+
106
+ ## 🔄 Migration from react-native-ble-plx
107
+
108
+ **Zero code changes required!** Simply replace the import:
109
+
110
+ ```typescript
111
+ // Before
112
+ import { BleManager, Device, State } from 'react-native-ble-plx';
113
+
114
+ // After
115
+ import { BleManager, Device, State } from 'react-native-ble-nitro';
116
+
117
+ // Everything else stays exactly the same!
118
+ ```
119
+
120
+ ## 🏗️ Architecture
121
+
122
+ ### Nitro Modules Foundation
123
+
124
+ Built on [Nitro Modules](https://nitro.margelo.com/) for:
125
+
126
+ - **Direct JSI Communication**: No React Native bridge overhead
127
+ - **Type-Safe Bindings**: Compile-time type checking across JS/Native boundary
128
+ - **High Performance**: Near-native performance for all operations
129
+ - **Memory Efficient**: Optimal memory management with smart references
130
+
131
+ ### Platform Implementation
132
+
133
+ - **iOS**: Native Swift implementation using Core Bluetooth
134
+ - **Android**: Native Kotlin implementation using Android BLE APIs
135
+ - **Shared C++**: Common logic and type definitions
136
+
137
+ ### Compatibility Layer
138
+
139
+ While maintaining 100% API compatibility, some internal changes were needed for Nitro:
140
+
141
+ - **Enum Values**: Numeric instead of string (transparent to users)
142
+ - **Service Data**: Structured format internally (automatic conversion)
143
+ - **Type Safety**: Enhanced compile-time checks
144
+
145
+ See [API_DIFFERENCES.md](./API_DIFFERENCES.md) for technical details.
146
+
147
+ ## 📋 API Reference
148
+
149
+ The API is identical to `react-native-ble-plx`. Key classes and methods:
150
+
151
+ ### BleManager
152
+
153
+ - `new BleManager(options?)`: Create manager instance
154
+ - `state()`: Get current Bluetooth state
155
+ - `startDeviceScan()`: Start scanning for devices
156
+ - `connectToDevice()`: Connect to a BLE device
157
+ - `readCharacteristicForDevice()`: Read characteristic value
158
+ - `writeCharacteristicWithResponseForDevice()`: Write with response
159
+ - `monitorCharacteristicForDevice()`: Monitor value changes
160
+
161
+ ### Device
162
+
163
+ - `connect()`: Connect to this device
164
+ - `discoverAllServicesAndCharacteristics()`: Discover GATT structure
165
+ - `readCharacteristicForService()`: Read characteristic
166
+ - `monitorCharacteristicForService()`: Monitor characteristic
167
+
168
+ For complete API documentation, see the [react-native-ble-plx docs](https://github.com/dotintent/react-native-ble-plx) - everything applies to this library too!
169
+
170
+ ## ⚙️ Configuration
171
+
172
+ ### Expo Plugin Options
173
+
174
+ ```typescript
175
+ interface BleNitroPluginProps {
176
+ isBackgroundEnabled?: boolean; // Enable background BLE support
177
+ neverForLocation?: boolean; // Assert no location derivation [Android 12+]
178
+ modes?: ('peripheral' | 'central')[]; // iOS background modes
179
+ bluetoothAlwaysPermission?: string | false; // iOS permission message
180
+ }
181
+ ```
182
+
183
+ ### iOS Background Modes
184
+
185
+ ```json
186
+ {
187
+ "modes": ["peripheral", "central"]
188
+ }
189
+ ```
190
+
191
+ Adds these to `Info.plist`:
192
+ - `bluetooth-peripheral`: Act as BLE peripheral in background
193
+ - `bluetooth-central`: Scan/connect as central in background
194
+
195
+ ### Android Permissions
196
+
197
+ Automatically adds required permissions:
198
+
199
+ ```xml
200
+ <!-- Basic Bluetooth -->
201
+ <uses-permission android:name="android.permission.BLUETOOTH" />
202
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
203
+
204
+ <!-- Location (required for BLE scanning) -->
205
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
206
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
207
+
208
+ <!-- Android 12+ -->
209
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
210
+ <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
211
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
212
+
213
+ <!-- BLE Hardware Feature -->
214
+ <uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />
215
+ ```
216
+
217
+ ## 🔧 Development
218
+
219
+ ### Building the Library
220
+
221
+ ```bash
222
+ # Install dependencies
223
+ npm install
224
+
225
+ # Generate native Nitro code
226
+ npx nitro-codegen
227
+
228
+ # Build TypeScript
229
+ npm run build
230
+
231
+ # Run tests
232
+ npm test
233
+
234
+ # Lint code
235
+ npm run lint
236
+ ```
237
+
238
+ ### Project Structure
239
+
240
+ ```
241
+ react-native-ble-nitro/
242
+ ├── src/
243
+ │ ├── specs/ # Nitro module TypeScript specs
244
+ │ ├── compatibility/ # react-native-ble-plx compatibility layer
245
+ │ ├── utils/ # Utility functions (UUID, Base64)
246
+ │ └── errors/ # BLE error handling
247
+ ├── nitrogen/generated/ # Generated native code (Nitro)
248
+ ├── plugin/ # Expo config plugin
249
+ ├── ios/ # iOS native implementation (Swift)
250
+ ├── android/ # Android native implementation (Kotlin)
251
+ └── docs/ # Documentation
252
+ ```
253
+
254
+ ## 🤝 Contributing
255
+
256
+ We welcome contributions! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
257
+
258
+ ### Development Setup
259
+
260
+ 1. **Fork the repository** on GitHub
261
+ 2. **Clone your fork**: `git clone https://github.com/YOUR_USERNAME/react-native-ble-nitro.git`
262
+ 3. **Add upstream remote**: `git remote add upstream https://github.com/zykeco/react-native-ble-nitro.git`
263
+ 4. **Install dependencies**: `npm install`
264
+ 5. **Generate Nitro code**: `npx nitro-codegen`
265
+ 6. **Make your changes** and run tests: `npm test`
266
+ 7. **Submit a pull request**
267
+
268
+ ## 📄 License
269
+
270
+ MIT License - see [LICENSE](./LICENSE) file.
271
+
272
+ ## Comparison with react-native-ble-plx
273
+
274
+ | Feature | react-native-ble-plx | react-native-ble-nitro |
275
+ |---------|---------------------|------------------------|
276
+ | **Performance** | React Native Bridge | Direct JSI ⚡ |
277
+ | **Type Safety** | TypeScript definitions | Compile-time validation ✅ |
278
+ | **Memory Usage** | Higher overhead | Optimized 🎯 |
279
+ | **New Architecture** | Limited support | Full support ✅ |
280
+ | **API Compatibility** | Original | 100% Compatible ✅ |
281
+
282
+ ## 🙏 Acknowledgments
283
+
284
+ - [Zyke Band](https://zykeband.com) - The fitness tracker project that inspired this library
285
+ - [react-native-ble-plx](https://github.com/dotintent/react-native-ble-plx) - Original library and API design
286
+ - [Marc Rousavy](https://github.com/mrousavy) - Creator of Nitro Modules and CEO of Margelo
287
+ - [Nitro Modules](https://nitro.margelo.com/) - High-performance native module framework
288
+ - [Margelo](https://margelo.com/) - Nitro Modules creators
289
+
290
+ ## 📞 Support
291
+
292
+ - 🐛 **Bug Reports**: [GitHub Issues](https://github.com/zykeco/react-native-ble-nitro/issues)
293
+ - 💬 **Questions**: [GitHub Discussions](https://github.com/zykeco/react-native-ble-nitro/discussions)
294
+ - 📖 **Documentation**: [API Reference](./docs/api.md)
295
+
296
+ ---
297
+
298
+ **Made with ❤️ for the React Native community**
@@ -0,0 +1,55 @@
1
+ apply plugin: 'com.android.library'
2
+ apply plugin: 'kotlin-android'
3
+
4
+ def safeExtGet(prop, fallback) {
5
+ rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
6
+ }
7
+
8
+ android {
9
+ compileSdkVersion safeExtGet('compileSdkVersion', 34)
10
+
11
+ defaultConfig {
12
+ minSdkVersion safeExtGet('minSdkVersion', 21)
13
+ targetSdkVersion safeExtGet('targetSdkVersion', 34)
14
+ versionCode 1
15
+ versionName "1.0"
16
+ }
17
+
18
+ buildTypes {
19
+ release {
20
+ minifyEnabled false
21
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22
+ }
23
+ }
24
+
25
+ compileOptions {
26
+ sourceCompatibility JavaVersion.VERSION_1_8
27
+ targetCompatibility JavaVersion.VERSION_1_8
28
+ }
29
+
30
+ kotlinOptions {
31
+ jvmTarget = "1.8"
32
+ }
33
+
34
+ packagingOptions {
35
+ pickFirst '**/libc++_shared.so'
36
+ }
37
+ }
38
+
39
+ dependencies {
40
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
41
+ implementation 'androidx.core:core-ktx:1.9.0'
42
+
43
+ // React Native
44
+ implementation 'com.facebook.react:react-native:+'
45
+
46
+ // Nitro Modules
47
+ implementation 'com.margelo.nitro:nitro-modules:0.26.4'
48
+
49
+ // BLE Dependencies
50
+ implementation 'no.nordicsemi.android:ble:2.6.1'
51
+ implementation 'no.nordicsemi.android:ble-ktx:2.6.1'
52
+
53
+ // Coroutines
54
+ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
55
+ }
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
+ package="co.zyke.ble">
4
+
5
+ <!-- Required for all BLE functionality -->
6
+ <uses-permission android:name="android.permission.BLUETOOTH" />
7
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
8
+
9
+ <!-- Required for BLE scanning -->
10
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
11
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
12
+
13
+ <!-- Android 12+ runtime permissions -->
14
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
15
+ <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
16
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
17
+
18
+ <!-- BLE feature requirement -->
19
+ <uses-feature
20
+ android:name="android.hardware.bluetooth_le"
21
+ android:required="false" />
22
+
23
+ </manifest>