munim-bluetooth 0.3.7 → 0.3.10

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.
@@ -10,7 +10,7 @@ import CoreBluetooth
10
10
  import NitroModules
11
11
  import React
12
12
 
13
- class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegate, CBCentralManagerDelegate, CBPeripheralDelegate {
13
+ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
14
14
  // Peripheral Manager
15
15
  private var peripheralManager: CBPeripheralManager?
16
16
  private var peripheralServices: [CBMutableService] = []
@@ -24,19 +24,15 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
24
24
  private var scanOptions: ScanOptions?
25
25
  private var isScanning = false
26
26
 
27
- // Event emitter
28
- private var eventEmitter: NitroEventEmitter?
29
-
30
27
  override init() {
31
28
  super.init()
32
29
  peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
33
30
  centralManager = CBCentralManager(delegate: self, queue: nil)
34
- eventEmitter = NitroEventEmitter(moduleName: "MunimBluetooth")
35
31
  }
36
32
 
37
33
  // MARK: - Peripheral Features
38
34
 
39
- func startAdvertising(options: AdvertisingOptions) throws {
35
+ override func startAdvertising(options: AdvertisingOptions) throws {
40
36
  guard let peripheralManager = peripheralManager,
41
37
  peripheralManager.state == .poweredOn else {
42
38
  throw NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Bluetooth is not powered on"])
@@ -70,7 +66,7 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
70
66
  peripheralManager.startAdvertising(advertisingData as? [String: Any])
71
67
  }
72
68
 
73
- func updateAdvertisingData(advertisingData: AdvertisingDataTypes) throws {
69
+ override func updateAdvertisingData(advertisingData: AdvertisingDataTypes) throws {
74
70
  guard let peripheralManager = peripheralManager,
75
71
  peripheralManager.state == .poweredOn else {
76
72
  throw NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Bluetooth is not powered on"])
@@ -85,18 +81,18 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
85
81
  peripheralManager.startAdvertising(newAdvertisingData as? [String: Any])
86
82
  }
87
83
 
88
- func getAdvertisingData() throws -> Promise<AdvertisingDataTypes> {
84
+ override func getAdvertisingData() throws -> Promise<AdvertisingDataTypes> {
89
85
  return Promise { resolver in
90
86
  resolver.resolve(self.currentAdvertisingData ?? AdvertisingDataTypes())
91
87
  }
92
88
  }
93
89
 
94
- func stopAdvertising() throws {
90
+ override func stopAdvertising() throws {
95
91
  peripheralManager?.stopAdvertising()
96
92
  currentAdvertisingData = nil
97
93
  }
98
94
 
99
- func setServices(services: [GATTService]) throws {
95
+ override func setServices(services: [GATTService]) throws {
100
96
  peripheralServices.removeAll()
101
97
 
102
98
  for service in services {
@@ -157,21 +153,21 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
157
153
 
158
154
  // MARK: - Central/Manager Features
159
155
 
160
- func isBluetoothEnabled() throws -> Promise<Bool> {
156
+ override func isBluetoothEnabled() throws -> Promise<Bool> {
161
157
  return Promise { resolver in
162
158
  let isEnabled = self.centralManager?.state == .poweredOn
163
159
  resolver.resolve(isEnabled ?? false)
164
160
  }
165
161
  }
166
162
 
167
- func requestBluetoothPermission() throws -> Promise<Bool> {
163
+ override func requestBluetoothPermission() throws -> Promise<Bool> {
168
164
  return Promise { resolver in
169
165
  // In iOS, permissions are handled by CBPeripheralManager/CBCentralManager
170
166
  resolver.resolve(true)
171
167
  }
172
168
  }
173
169
 
174
- func startScan(options: ScanOptions?) throws {
170
+ override func startScan(options: ScanOptions?) throws {
175
171
  guard let centralManager = centralManager,
176
172
  centralManager.state == .poweredOn else {
177
173
  throw NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Bluetooth is not powered on"])
@@ -188,12 +184,12 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
188
184
  centralManager.scanForPeripherals(withServices: nil, options: scanOptions as [String : Any])
189
185
  }
190
186
 
191
- func stopScan() throws {
187
+ override func stopScan() throws {
192
188
  centralManager?.stopScan()
193
189
  isScanning = false
194
190
  }
195
191
 
196
- func connect(deviceId: String) throws -> Promise<Void> {
192
+ override func connect(deviceId: String) throws -> Promise<Void> {
197
193
  return Promise { resolver in
198
194
  guard let peripheral = self.discoveredPeripherals[deviceId] else {
199
195
  resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Device not found"]))
@@ -206,13 +202,13 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
206
202
  }
207
203
  }
208
204
 
209
- func disconnect(deviceId: String) throws {
205
+ override func disconnect(deviceId: String) throws {
210
206
  guard let peripheral = connectedPeripherals[deviceId] else { return }
211
207
  centralManager?.cancelPeripheralConnection(peripheral)
212
208
  connectedPeripherals.removeValue(forKey: deviceId)
213
209
  }
214
210
 
215
- func discoverServices(deviceId: String) throws -> Promise<[GATTService]> {
211
+ override func discoverServices(deviceId: String) throws -> Promise<[GATTService]> {
216
212
  return Promise { resolver in
217
213
  guard let peripheral = self.connectedPeripherals[deviceId] else {
218
214
  resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Device not connected"]))
@@ -224,33 +220,33 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
224
220
  }
225
221
  }
226
222
 
227
- func readCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws -> Promise<CharacteristicValue> {
223
+ override func readCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws -> Promise<CharacteristicValue> {
228
224
  return Promise { resolver in
229
225
  resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Not implemented"]))
230
226
  }
231
227
  }
232
228
 
233
- func writeCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String, value: String, writeType: WriteType?) throws -> Promise<Void> {
229
+ override func writeCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String, value: String, writeType: WriteType?) throws -> Promise<Void> {
234
230
  return Promise { resolver in
235
231
  resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Not implemented"]))
236
232
  }
237
233
  }
238
234
 
239
- func subscribeToCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws {
235
+ override func subscribeToCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws {
240
236
  // Not implemented
241
237
  }
242
238
 
243
- func unsubscribeFromCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws {
239
+ override func unsubscribeFromCharacteristic(deviceId: String, serviceUUID: String, characteristicUUID: String) throws {
244
240
  // Not implemented
245
241
  }
246
242
 
247
- func getConnectedDevices() throws -> Promise<[String]> {
243
+ override func getConnectedDevices() throws -> Promise<[String]> {
248
244
  return Promise { resolver in
249
245
  resolver.resolve(Array(self.connectedPeripherals.keys))
250
246
  }
251
247
  }
252
248
 
253
- func readRSSI(deviceId: String) throws -> Promise<Double> {
249
+ override func readRSSI(deviceId: String) throws -> Promise<Double> {
254
250
  return Promise { resolver in
255
251
  guard let peripheral = self.connectedPeripherals[deviceId] else {
256
252
  resolver.reject(NSError(domain: "MunimBluetooth", code: 1, userInfo: [NSLocalizedDescriptionKey: "Device not connected"]))
@@ -262,11 +258,11 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
262
258
  }
263
259
  }
264
260
 
265
- func addListener(eventName: String) throws {
261
+ override func addListener(eventName: String) throws {
266
262
  // Event management
267
263
  }
268
264
 
269
- func removeListeners(count: Double) throws {
265
+ override func removeListeners(count: Double) throws {
270
266
  // Event management
271
267
  }
272
268
 
@@ -306,44 +302,40 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec, CBPeripheralManagerDelegat
306
302
  }
307
303
 
308
304
  // MARK: - CBPeripheralManagerDelegate Implementation
309
- extension HybridMunimBluetooth {
305
+ extension HybridMunimBluetooth: CBPeripheralManagerDelegate {
310
306
  func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
311
307
  // Handle state updates
312
308
  }
313
309
 
314
310
  func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
315
311
  if let error = error {
316
- eventEmitter?.emit("advertisingError", ["error": error.localizedDescription])
312
+ NSLog("Bluetooth event")
317
313
  } else {
318
- eventEmitter?.emit("advertisingStarted", [:])
314
+ NSLog("Bluetooth event")
319
315
  }
320
316
  }
321
317
 
322
318
  func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
323
319
  if let error = error {
324
- eventEmitter?.emit("serviceError", ["error": error.localizedDescription])
320
+ NSLog("Bluetooth event")
325
321
  } else {
326
- eventEmitter?.emit("serviceAdded", ["uuid": service.uuid.uuidString])
322
+ NSLog("Bluetooth event")
327
323
  }
328
324
  }
329
325
  }
330
326
 
331
327
  // MARK: - CBCentralManagerDelegate Implementation
332
- extension HybridMunimBluetooth {
328
+ extension HybridMunimBluetooth: CBCentralManagerDelegate {
333
329
  func centralManagerDidUpdateState(_ central: CBCentralManager) {
334
330
  let state = central.state
335
- eventEmitter?.emit("bluetoothStateChanged", ["state": state.rawValue])
331
+ NSLog("Bluetooth event")
336
332
  }
337
333
 
338
334
  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
339
335
  let deviceId = peripheral.identifier.uuidString
340
336
  discoveredPeripherals[deviceId] = peripheral
341
337
 
342
- eventEmitter?.emit("deviceFound", [
343
- "id": deviceId,
344
- "name": peripheral.name ?? "",
345
- "rssi": RSSI.intValue
346
- ])
338
+ NSLog("Bluetooth: deviceFound")
347
339
  }
348
340
 
349
341
  func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
@@ -351,7 +343,7 @@ extension HybridMunimBluetooth {
351
343
  connectedPeripherals[deviceId] = peripheral
352
344
  peripheral.delegate = self
353
345
 
354
- eventEmitter?.emit("deviceConnected", ["id": deviceId])
346
+ NSLog("Bluetooth event")
355
347
  }
356
348
 
357
349
  func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
@@ -359,20 +351,17 @@ extension HybridMunimBluetooth {
359
351
  connectedPeripherals.removeValue(forKey: deviceId)
360
352
  peripheralCharacteristics.removeValue(forKey: deviceId)
361
353
 
362
- eventEmitter?.emit("deviceDisconnected", ["id": deviceId])
354
+ NSLog("Bluetooth event")
363
355
  }
364
356
 
365
357
  func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
366
358
  let deviceId = peripheral.identifier.uuidString
367
- eventEmitter?.emit("connectionFailed", [
368
- "id": deviceId,
369
- "error": error?.localizedDescription ?? "Unknown error"
370
- ])
359
+ NSLog("Bluetooth: connectionFailed")
371
360
  }
372
361
  }
373
362
 
374
363
  // MARK: - CBPeripheralDelegate Implementation
375
- extension HybridMunimBluetooth {
364
+ extension HybridMunimBluetooth: CBPeripheralDelegate {
376
365
  func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
377
366
  let deviceId = peripheral.identifier.uuidString
378
367
 
@@ -382,7 +371,7 @@ extension HybridMunimBluetooth {
382
371
  peripheral.discoverCharacteristics(nil, for: service)
383
372
  }
384
373
 
385
- eventEmitter?.emit("servicesDiscovered", ["id": deviceId])
374
+ NSLog("Bluetooth event")
386
375
  }
387
376
 
388
377
  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
@@ -395,7 +384,7 @@ extension HybridMunimBluetooth {
395
384
  }
396
385
  peripheralCharacteristics[deviceId]?.append(contentsOf: characteristics)
397
386
 
398
- eventEmitter?.emit("characteristicsDiscovered", ["id": deviceId])
387
+ NSLog("Bluetooth event")
399
388
  }
400
389
 
401
390
  func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
@@ -405,24 +394,16 @@ extension HybridMunimBluetooth {
405
394
 
406
395
  let hexString = data.map { String(format: "%02x", $0) }.joined()
407
396
 
408
- eventEmitter?.emit("characteristicValueChanged", [
409
- "id": deviceId,
410
- "serviceUUID": characteristic.service?.uuid.uuidString ?? "",
411
- "characteristicUUID": characteristic.uuid.uuidString,
412
- "value": hexString
413
- ])
397
+ NSLog("Bluetooth: characteristicValueChanged")
414
398
  }
415
399
 
416
400
  func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
417
401
  let deviceId = peripheral.identifier.uuidString
418
402
 
419
403
  if let error = error {
420
- eventEmitter?.emit("writeError", [
421
- "id": deviceId,
422
- "error": error.localizedDescription
423
- ])
404
+ NSLog("Bluetooth: writeError")
424
405
  } else {
425
- eventEmitter?.emit("writeSuccess", ["id": deviceId])
406
+ NSLog("Bluetooth event")
426
407
  }
427
408
  }
428
409
 
@@ -430,40 +411,9 @@ extension HybridMunimBluetooth {
430
411
  let deviceId = peripheral.identifier.uuidString
431
412
 
432
413
  if let error = error {
433
- eventEmitter?.emit("rssiError", ["error": error.localizedDescription])
434
- } else {
435
- eventEmitter?.emit("rssiUpdated", ["id": deviceId, "rssi": RSSI.intValue])
436
- }
437
- }
438
- }
439
-
440
- // MARK: - Helper Classes
441
- class NitroEventEmitter {
442
- private let moduleName: String
443
-
444
- init(moduleName: String) {
445
- self.moduleName = moduleName
446
- }
447
-
448
- func emit(_ eventName: String, _ body: [String: Any]) {
449
- let sendEvent = {
450
- guard let bridge = RCTBridge.current() ?? RCTBridge.currentBridge() else {
451
- NSLog("[\(self.moduleName)] Unable to emit event \(eventName): missing bridge")
452
- return
453
- }
454
-
455
- bridge.enqueueJSCall(
456
- "RCTDeviceEventEmitter",
457
- method: "emit",
458
- args: [eventName, body],
459
- completion: nil
460
- )
461
- }
462
-
463
- if Thread.isMainThread {
464
- sendEvent()
414
+ NSLog("Bluetooth event")
465
415
  } else {
466
- DispatchQueue.main.async(execute: sendEvent)
416
+ NSLog("Bluetooth event")
467
417
  }
468
418
  }
469
419
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "munim-bluetooth",
3
- "version": "0.3.7",
3
+ "version": "0.3.10",
4
4
  "description": "A comprehensive React Native library for all your Bluetooth Low Energy (BLE) needs, supporting both peripheral and central roles with Expo support",
5
5
  "main": "./lib/commonjs/index.js",
6
6
  "module": "./lib/module/index.js",