munim-bluetooth 0.3.8 → 0.3.11

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 {
13
+ class HybridMunimBluetooth: NSObject, HybridMunimBluetoothSpec {
14
14
  // Peripheral Manager
15
15
  private var peripheralManager: CBPeripheralManager?
16
16
  private var peripheralServices: [CBMutableService] = []
@@ -24,14 +24,10 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
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
@@ -313,17 +309,17 @@ extension HybridMunimBluetooth: CBPeripheralManagerDelegate {
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
  }
@@ -332,18 +328,14 @@ extension HybridMunimBluetooth: CBPeripheralManagerDelegate {
332
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: CBCentralManagerDelegate {
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,15 +351,12 @@ extension HybridMunimBluetooth: CBCentralManagerDelegate {
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
 
@@ -382,7 +371,7 @@ extension HybridMunimBluetooth: CBPeripheralDelegate {
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: CBPeripheralDelegate {
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: CBPeripheralDelegate {
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: CBPeripheralDelegate {
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.8",
3
+ "version": "0.3.11",
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",