munim-bluetooth 0.1.0 → 0.2.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 CHANGED
@@ -1,18 +1,43 @@
1
1
  # munim-bluetooth
2
2
 
3
- munim-bluetooth is a react native package for all your Bluetooth, BLE, and BLE pheripheral needs
3
+ A comprehensive React Native library for all your Bluetooth Low Energy (BLE) needs, supporting both peripheral and central roles. Built with React Native's Nitro modules architecture for high performance.
4
4
 
5
5
  [![Version](https://img.shields.io/npm/v/munim-bluetooth.svg)](https://www.npmjs.com/package/munim-bluetooth)
6
6
  [![Downloads](https://img.shields.io/npm/dm/munim-bluetooth.svg)](https://www.npmjs.com/package/munim-bluetooth)
7
7
  [![License](https://img.shields.io/npm/l/munim-bluetooth.svg)](https://github.com/munimtechnologies/munim-bluetooth/LICENSE)
8
8
 
9
+ ## Features
10
+
11
+ ### Peripheral Mode (BLE Advertising & GATT Server)
12
+
13
+ - 🔵 **BLE Peripheral Mode**: Transform your React Native app into a BLE peripheral device
14
+ - 📡 **Service Advertising**: Advertise custom GATT services with multiple characteristics
15
+ - 🔄 **Real-time Communication**: Support for read, write, and notify operations
16
+ - ✅ **Platform-Supported BLE Advertising**: Support for core BLE advertising data types
17
+ - 🔧 **Dynamic Updates**: Update advertising data while advertising is active
18
+
19
+ ### Central Mode (BLE Scanning & GATT Client)
20
+
21
+ - 🔍 **Device Scanning**: Scan for BLE devices with filtering options
22
+ - 🔗 **Device Connection**: Connect and disconnect from BLE devices
23
+ - 📊 **GATT Operations**: Discover services, read/write characteristics
24
+ - 🔔 **Notifications**: Subscribe to characteristic notifications/indications
25
+ - 📶 **RSSI Monitoring**: Read signal strength for connected devices
26
+
27
+ ### Additional Features
28
+
29
+ - 📱 **Cross-platform**: Works on both iOS and Android
30
+ - 🎯 **TypeScript Support**: Full TypeScript definitions included
31
+ - ⚡ **High Performance**: Built with React Native's Nitro modules architecture
32
+ - 🔐 **Permission Handling**: Built-in permission request helpers
33
+
9
34
  ## Requirements
10
35
 
11
36
  - React Native v0.76.0 or higher
12
37
  - Node 18.0.0 or higher
13
38
 
14
39
  > [!IMPORTANT]
15
- > To Support `Nitro Views` you need to install React Native version v0.78.0 or higher.
40
+ > To Support `Nitro Modules` you need to install React Native version v0.78.0 or higher.
16
41
 
17
42
  ## Installation
18
43
 
@@ -20,10 +45,478 @@ munim-bluetooth is a react native package for all your Bluetooth, BLE, and BLE p
20
45
  npm install munim-bluetooth react-native-nitro-modules
21
46
  ```
22
47
 
23
- ## Credits
48
+ ### iOS Setup
24
49
 
25
- Bootstrapped with [create-nitro-module](https://github.com/patrickkabwe/create-nitro-module).
50
+ For iOS, the library is automatically linked. However, you need to add the following to your `Info.plist`:
51
+
52
+ ```xml
53
+ <key>NSBluetoothAlwaysUsageDescription</key>
54
+ <string>This app uses Bluetooth for BLE communication</string>
55
+ <key>NSBluetoothPeripheralUsageDescription</key>
56
+ <string>This app uses Bluetooth to create a peripheral device</string>
57
+ ```
58
+
59
+ **For Expo projects**, add these permissions to your `app.json`:
60
+
61
+ ```json
62
+ {
63
+ "expo": {
64
+ "ios": {
65
+ "infoPlist": {
66
+ "NSBluetoothAlwaysUsageDescription": "This app uses Bluetooth for BLE communication",
67
+ "NSBluetoothPeripheralUsageDescription": "This app uses Bluetooth to create a peripheral device"
68
+ }
69
+ }
70
+ }
71
+ }
72
+ ```
73
+
74
+ ### Android Setup
75
+
76
+ The required permissions are automatically included in the library's `AndroidManifest.xml`. However, for Android 12+ (API 31+), you may need to add the following to your app's `AndroidManifest.xml`:
77
+
78
+ ```xml
79
+ <uses-permission android:name="android.permission.BLUETOOTH" />
80
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
81
+ <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
82
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
83
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
84
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
85
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
86
+ ```
87
+
88
+ For Android 12+, you also need to specify that these permissions are not used for device discovery:
89
+
90
+ ```xml
91
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
92
+ ```
93
+
94
+ ## Quick Start
95
+
96
+ ### Peripheral Mode Example
97
+
98
+ ```typescript
99
+ import { startAdvertising, stopAdvertising, setServices } from 'munim-bluetooth'
100
+
101
+ // Start advertising
102
+ startAdvertising({
103
+ serviceUUIDs: ['180D', '180F'],
104
+ localName: 'My Device',
105
+ advertisingData: {
106
+ flags: 0x06,
107
+ completeLocalName: 'My Smart Device',
108
+ txPowerLevel: -12,
109
+ manufacturerData: '0102030405',
110
+ },
111
+ })
112
+
113
+ // Set up GATT services
114
+ setServices([
115
+ {
116
+ uuid: '180D',
117
+ characteristics: [
118
+ {
119
+ uuid: '2A37',
120
+ properties: ['read', 'notify'],
121
+ value: 'Hello World',
122
+ },
123
+ ],
124
+ },
125
+ ])
126
+
127
+ // Stop advertising
128
+ stopAdvertising()
129
+ ```
130
+
131
+ ### Central Mode Example
132
+
133
+ ```typescript
134
+ import {
135
+ startScan,
136
+ stopScan,
137
+ connect,
138
+ discoverServices,
139
+ readCharacteristic,
140
+ subscribeToCharacteristic,
141
+ } from 'munim-bluetooth'
142
+
143
+ // Start scanning
144
+ startScan({
145
+ serviceUUIDs: ['180D'],
146
+ allowDuplicates: false,
147
+ scanMode: 'balanced',
148
+ })
149
+
150
+ // Listen for discovered devices
151
+ // (Event handling would be implemented here)
152
+
153
+ // Connect to a device
154
+ await connect('device-id-here')
155
+
156
+ // Discover services
157
+ const services = await discoverServices('device-id-here')
158
+
159
+ // Read a characteristic
160
+ const value = await readCharacteristic('device-id-here', '180D', '2A37')
161
+
162
+ // Subscribe to notifications
163
+ subscribeToCharacteristic('device-id-here', '180D', '2A37')
164
+ ```
165
+
166
+ ## API Reference
167
+
168
+ ### Peripheral Functions
169
+
170
+ #### `startAdvertising(options)`
171
+
172
+ Starts BLE advertising with the specified options.
173
+
174
+ **Parameters:**
175
+
176
+ - `options` (object):
177
+ - `serviceUUIDs` (string[]): Array of service UUIDs to advertise
178
+ - `localName?` (string): Device name (legacy support)
179
+ - `manufacturerData?` (string): Manufacturer data in hex format (legacy support)
180
+ - `advertisingData?` (AdvertisingDataTypes): Platform-supported advertising data
181
+
182
+ #### `updateAdvertisingData(advertisingData)`
183
+
184
+ Updates the advertising data while advertising is active.
185
+
186
+ **Parameters:**
187
+
188
+ - `advertisingData` (AdvertisingDataTypes): New advertising data
189
+
190
+ #### `getAdvertisingData()`
191
+
192
+ Returns a Promise that resolves to the current advertising data.
193
+
194
+ **Returns:** Promise<AdvertisingDataTypes>
195
+
196
+ #### `stopAdvertising()`
197
+
198
+ Stops BLE advertising.
199
+
200
+ #### `setServices(services)`
201
+
202
+ Sets GATT services and characteristics.
203
+
204
+ **Parameters:**
205
+
206
+ - `services` (GATTService[]): Array of service objects
207
+
208
+ ### Central Functions
209
+
210
+ #### `isBluetoothEnabled()`
211
+
212
+ Checks if Bluetooth is enabled on the device.
213
+
214
+ **Returns:** Promise<boolean>
215
+
216
+ #### `requestBluetoothPermission()`
217
+
218
+ Requests Bluetooth permissions (Android) or checks authorization status (iOS).
219
+
220
+ **Returns:** Promise<boolean>
221
+
222
+ #### `startScan(options?)`
223
+
224
+ Starts scanning for BLE devices.
225
+
226
+ **Parameters:**
227
+
228
+ - `options?` (ScanOptions):
229
+ - `serviceUUIDs?` (string[]): Filter by service UUIDs
230
+ - `allowDuplicates?` (boolean): Allow duplicate scan results
231
+ - `scanMode?` ('lowPower' | 'balanced' | 'lowLatency'): Scan mode
232
+
233
+ #### `stopScan()`
234
+
235
+ Stops scanning for BLE devices.
236
+
237
+ #### `connect(deviceId)`
238
+
239
+ Connects to a BLE device.
240
+
241
+ **Parameters:**
242
+
243
+ - `deviceId` (string): The unique identifier of the device
244
+
245
+ **Returns:** Promise<void>
246
+
247
+ #### `disconnect(deviceId)`
248
+
249
+ Disconnects from a BLE device.
250
+
251
+ **Parameters:**
252
+
253
+ - `deviceId` (string): The unique identifier of the device
254
+
255
+ #### `discoverServices(deviceId)`
256
+
257
+ Discovers GATT services for a connected device.
258
+
259
+ **Parameters:**
260
+
261
+ - `deviceId` (string): The unique identifier of the connected device
262
+
263
+ **Returns:** Promise<GATTService[]>
264
+
265
+ #### `readCharacteristic(deviceId, serviceUUID, characteristicUUID)`
266
+
267
+ Reads a characteristic value from a connected device.
268
+
269
+ **Parameters:**
270
+
271
+ - `deviceId` (string): The unique identifier of the connected device
272
+ - `serviceUUID` (string): The UUID of the service
273
+ - `characteristicUUID` (string): The UUID of the characteristic
274
+
275
+ **Returns:** Promise<CharacteristicValue>
276
+
277
+ #### `writeCharacteristic(deviceId, serviceUUID, characteristicUUID, value, writeType?)`
278
+
279
+ Writes a value to a characteristic on a connected device.
280
+
281
+ **Parameters:**
282
+
283
+ - `deviceId` (string): The unique identifier of the connected device
284
+ - `serviceUUID` (string): The UUID of the service
285
+ - `characteristicUUID` (string): The UUID of the characteristic
286
+ - `value` (string): The value to write (hex string)
287
+ - `writeType?` ('write' | 'writeWithoutResponse'): Write type
288
+
289
+ **Returns:** Promise<void>
290
+
291
+ #### `subscribeToCharacteristic(deviceId, serviceUUID, characteristicUUID)`
292
+
293
+ Subscribes to notifications/indications from a characteristic.
294
+
295
+ **Parameters:**
296
+
297
+ - `deviceId` (string): The unique identifier of the connected device
298
+ - `serviceUUID` (string): The UUID of the service
299
+ - `characteristicUUID` (string): The UUID of the characteristic
300
+
301
+ #### `unsubscribeFromCharacteristic(deviceId, serviceUUID, characteristicUUID)`
302
+
303
+ Unsubscribes from notifications/indications from a characteristic.
304
+
305
+ **Parameters:**
306
+
307
+ - `deviceId` (string): The unique identifier of the connected device
308
+ - `serviceUUID` (string): The UUID of the service
309
+ - `characteristicUUID` (string): The UUID of the characteristic
310
+
311
+ #### `getConnectedDevices()`
312
+
313
+ Gets list of currently connected devices.
314
+
315
+ **Returns:** Promise<string[]>
316
+
317
+ #### `readRSSI(deviceId)`
318
+
319
+ Reads RSSI (signal strength) for a connected device.
320
+
321
+ **Parameters:**
322
+
323
+ - `deviceId` (string): The unique identifier of the connected device
324
+
325
+ **Returns:** Promise<number>
326
+
327
+ ### Event Management
328
+
329
+ #### `addListener(eventName)`
330
+
331
+ Adds an event listener.
332
+
333
+ **Parameters:**
334
+
335
+ - `eventName` (string): The name of the event to listen for
336
+
337
+ **Event Types:**
338
+
339
+ - `deviceFound`: Emitted when a BLE device is discovered during scanning
340
+ - `deviceConnected`: Emitted when a device is successfully connected
341
+ - `deviceDisconnected`: Emitted when a device is disconnected
342
+ - `servicesDiscovered`: Emitted when services are discovered for a device
343
+ - `characteristicsDiscovered`: Emitted when characteristics are discovered
344
+ - `characteristicValueChanged`: Emitted when a characteristic value changes
345
+ - `writeSuccess`: Emitted when a write operation succeeds
346
+ - `writeError`: Emitted when a write operation fails
347
+ - `rssiUpdated`: Emitted when RSSI is updated
348
+
349
+ #### `removeListeners(count)`
350
+
351
+ Removes event listeners.
352
+
353
+ **Parameters:**
354
+
355
+ - `count` (number): Number of listeners to remove
356
+
357
+ ## Types
358
+
359
+ ### `AdvertisingDataTypes`
360
+
361
+ Platform-supported interface for BLE advertising data types:
362
+
363
+ ```typescript
364
+ interface AdvertisingDataTypes {
365
+ flags?: number
366
+ incompleteServiceUUIDs16?: string[]
367
+ completeServiceUUIDs16?: string[]
368
+ incompleteServiceUUIDs32?: string[]
369
+ completeServiceUUIDs32?: string[]
370
+ incompleteServiceUUIDs128?: string[]
371
+ completeServiceUUIDs128?: string[]
372
+ shortenedLocalName?: string
373
+ completeLocalName?: string
374
+ txPowerLevel?: number
375
+ serviceSolicitationUUIDs16?: string[]
376
+ serviceSolicitationUUIDs128?: string[]
377
+ serviceSolicitationUUIDs32?: string[]
378
+ serviceData16?: Array<{ uuid: string; data: string }>
379
+ serviceData32?: Array<{ uuid: string; data: string }>
380
+ serviceData128?: Array<{ uuid: string; data: string }>
381
+ appearance?: number
382
+ manufacturerData?: string
383
+ }
384
+ ```
385
+
386
+ ### `GATTService`
387
+
388
+ ```typescript
389
+ interface GATTService {
390
+ uuid: string
391
+ characteristics: Array<{
392
+ uuid: string
393
+ properties: string[]
394
+ value?: string
395
+ }>
396
+ }
397
+ ```
398
+
399
+ ### `ScanOptions`
400
+
401
+ ```typescript
402
+ interface ScanOptions {
403
+ serviceUUIDs?: string[]
404
+ allowDuplicates?: boolean
405
+ scanMode?: 'lowPower' | 'balanced' | 'lowLatency'
406
+ }
407
+ ```
408
+
409
+ ## Usage Examples
410
+
411
+ ### Health Device (Peripheral)
412
+
413
+ ```typescript
414
+ import { startAdvertising, setServices } from 'munim-bluetooth'
415
+
416
+ // Health device advertising
417
+ startAdvertising({
418
+ serviceUUIDs: ['180D', '180F'], // Heart Rate, Battery Service
419
+ advertisingData: {
420
+ flags: 0x06,
421
+ completeLocalName: 'Health Monitor',
422
+ appearance: 0x03c0, // Generic Watch
423
+ txPowerLevel: -8,
424
+ manufacturerData: '0102030405',
425
+ serviceData16: [
426
+ { uuid: '180D', data: '6400' }, // Heart rate: 100 bpm
427
+ { uuid: '180F', data: '64' }, // Battery: 100%
428
+ ],
429
+ },
430
+ })
431
+
432
+ // Set up GATT services
433
+ setServices([
434
+ {
435
+ uuid: '180D', // Heart Rate Service
436
+ characteristics: [
437
+ {
438
+ uuid: '2A37', // Heart Rate Measurement
439
+ properties: ['read', 'notify'],
440
+ value: '6400', // 100 bpm
441
+ },
442
+ ],
443
+ },
444
+ ])
445
+ ```
446
+
447
+ ### Device Scanner (Central)
448
+
449
+ ```typescript
450
+ import {
451
+ startScan,
452
+ stopScan,
453
+ connect,
454
+ discoverServices,
455
+ readCharacteristic,
456
+ subscribeToCharacteristic,
457
+ addListener,
458
+ } from 'munim-bluetooth'
459
+
460
+ // Start scanning
461
+ startScan({
462
+ serviceUUIDs: ['180D'], // Filter by Heart Rate service
463
+ allowDuplicates: false,
464
+ scanMode: 'balanced',
465
+ })
466
+
467
+ // Listen for discovered devices
468
+ addListener('deviceFound')
469
+ // Handle deviceFound events to get device information
470
+
471
+ // Connect to a device
472
+ const deviceId = '...' // From deviceFound event
473
+ await connect(deviceId)
474
+
475
+ // Discover services
476
+ const services = await discoverServices(deviceId)
477
+
478
+ // Read a characteristic
479
+ const heartRate = await readCharacteristic(deviceId, '180D', '2A37')
480
+
481
+ // Subscribe to notifications
482
+ subscribeToCharacteristic(deviceId, '180D', '2A37')
483
+
484
+ // Listen for value changes
485
+ addListener('characteristicValueChanged')
486
+ // Handle characteristicValueChanged events
487
+ ```
488
+
489
+ ## Troubleshooting
490
+
491
+ ### Common Issues
492
+
493
+ 1. **Permission Denied**: Ensure you have the necessary Bluetooth permissions in your app
494
+ 2. **Advertising Not Starting**: Check that Bluetooth is enabled on the device
495
+ 3. **Services Not Visible**: Verify that your service UUIDs are properly formatted
496
+ 4. **Scanning Not Working**: On Android 6.0+, ensure location permissions are granted
497
+ 5. **Connection Fails**: Verify the device is in range and advertising
498
+
499
+ ### Platform-Specific Notes
500
+
501
+ **iOS:**
502
+
503
+ - All features work natively through Core Bluetooth
504
+ - Permission prompts appear automatically when needed
505
+
506
+ **Android:**
507
+
508
+ - Android 12+ requires additional permission flags
509
+ - Location permissions are required for BLE scanning
510
+ - Some features may behave differently on different Android versions
26
511
 
27
512
  ## Contributing
28
513
 
29
514
  Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
515
+
516
+ ## Credits
517
+
518
+ Bootstrapped with [create-nitro-module](https://github.com/patrickkabwe/create-nitro-module).
519
+
520
+ ## License
521
+
522
+ MIT
@@ -1,2 +1,20 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <!-- Bluetooth permissions for BLE peripheral and central operations -->
3
+ <uses-permission android:name="android.permission.BLUETOOTH" />
4
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5
+
6
+ <!-- BLE Advertising (Peripheral) -->
7
+ <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
8
+
9
+ <!-- BLE Scanning and Connection (Central) -->
10
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
11
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
12
+
13
+ <!-- Location permissions required for BLE scanning on Android 6.0+ -->
14
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
15
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
16
+
17
+ <!-- Feature declarations -->
18
+ <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
19
+ <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
2
20
  </manifest>