munim-bluetooth 0.3.23 → 0.3.24
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/ios/HybridMunimBluetooth.swift +99 -27
- package/package.json +1 -1
|
@@ -10,6 +10,82 @@ import CoreBluetooth
|
|
|
10
10
|
import NitroModules
|
|
11
11
|
import React
|
|
12
12
|
|
|
13
|
+
private final class PeripheralManagerDelegateProxy: NSObject, CBPeripheralManagerDelegate {
|
|
14
|
+
weak var owner: HybridMunimBluetooth?
|
|
15
|
+
|
|
16
|
+
init(owner: HybridMunimBluetooth) {
|
|
17
|
+
self.owner = owner
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
|
|
21
|
+
owner?.handlePeripheralManagerDidUpdateState(peripheral)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
|
|
25
|
+
owner?.handlePeripheralManagerDidStartAdvertising(peripheral, error: error)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
|
|
29
|
+
owner?.handlePeripheralManagerDidAddService(peripheral, service: service, error: error)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private final class CentralManagerDelegateProxy: NSObject, CBCentralManagerDelegate {
|
|
34
|
+
weak var owner: HybridMunimBluetooth?
|
|
35
|
+
|
|
36
|
+
init(owner: HybridMunimBluetooth) {
|
|
37
|
+
self.owner = owner
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
41
|
+
owner?.handleCentralManagerDidUpdateState(central)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
|
|
45
|
+
owner?.handleCentralManagerDidDiscover(central, peripheral: peripheral, advertisementData: advertisementData, rssi: RSSI)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
|
49
|
+
owner?.handleCentralManagerDidConnect(central, peripheral: peripheral)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
|
53
|
+
owner?.handleCentralManagerDidDisconnectPeripheral(central, peripheral: peripheral, error: error)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
|
|
57
|
+
owner?.handleCentralManagerDidFailToConnect(central, peripheral: peripheral, error: error)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private final class PeripheralDelegateProxy: NSObject, CBPeripheralDelegate {
|
|
62
|
+
weak var owner: HybridMunimBluetooth?
|
|
63
|
+
|
|
64
|
+
init(owner: HybridMunimBluetooth) {
|
|
65
|
+
self.owner = owner
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
|
69
|
+
owner?.handlePeripheralDidDiscoverServices(peripheral, error: error)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
|
73
|
+
owner?.handlePeripheralDidDiscoverCharacteristics(peripheral, service: service, error: error)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
|
77
|
+
owner?.handlePeripheralDidUpdateValue(peripheral, characteristic: characteristic, error: error)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
|
|
81
|
+
owner?.handlePeripheralDidWriteValue(peripheral, characteristic: characteristic, error: error)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
|
|
85
|
+
owner?.handlePeripheralDidReadRSSI(peripheral, rssi: RSSI, error: error)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
13
89
|
class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
14
90
|
// Peripheral Manager
|
|
15
91
|
private var peripheralManager: CBPeripheralManager?
|
|
@@ -23,11 +99,14 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
23
99
|
private var peripheralCharacteristics: [String: [CBCharacteristic]] = [:]
|
|
24
100
|
private var scanOptions: ScanOptions?
|
|
25
101
|
private var isScanning = false
|
|
102
|
+
private lazy var peripheralManagerDelegateProxy = PeripheralManagerDelegateProxy(owner: self)
|
|
103
|
+
private lazy var centralManagerDelegateProxy = CentralManagerDelegateProxy(owner: self)
|
|
104
|
+
private lazy var peripheralDelegateProxy = PeripheralDelegateProxy(owner: self)
|
|
26
105
|
|
|
27
106
|
override init() {
|
|
28
107
|
super.init()
|
|
29
|
-
peripheralManager = CBPeripheralManager(delegate:
|
|
30
|
-
centralManager = CBCentralManager(delegate:
|
|
108
|
+
peripheralManager = CBPeripheralManager(delegate: peripheralManagerDelegateProxy, queue: nil)
|
|
109
|
+
centralManager = CBCentralManager(delegate: centralManagerDelegateProxy, queue: nil)
|
|
31
110
|
}
|
|
32
111
|
|
|
33
112
|
// MARK: - Event Emission
|
|
@@ -406,15 +485,14 @@ class HybridMunimBluetooth: HybridMunimBluetoothSpec {
|
|
|
406
485
|
advertisingData[CBAdvertisementDataTxPowerLevelKey] = txPowerLevel
|
|
407
486
|
}
|
|
408
487
|
}
|
|
409
|
-
}
|
|
410
488
|
|
|
411
|
-
// MARK: -
|
|
412
|
-
|
|
413
|
-
func
|
|
489
|
+
// MARK: - CoreBluetooth Delegate Forwarding
|
|
490
|
+
|
|
491
|
+
func handlePeripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
|
|
414
492
|
// Handle state updates
|
|
415
493
|
}
|
|
416
494
|
|
|
417
|
-
func
|
|
495
|
+
func handlePeripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
|
|
418
496
|
if let error = error {
|
|
419
497
|
NSLog("Bluetooth event")
|
|
420
498
|
} else {
|
|
@@ -422,23 +500,20 @@ extension HybridMunimBluetooth: CBPeripheralManagerDelegate {
|
|
|
422
500
|
}
|
|
423
501
|
}
|
|
424
502
|
|
|
425
|
-
func
|
|
503
|
+
func handlePeripheralManagerDidAddService(_ peripheral: CBPeripheralManager, service: CBService, error: Error?) {
|
|
426
504
|
if let error = error {
|
|
427
505
|
NSLog("Bluetooth event")
|
|
428
506
|
} else {
|
|
429
507
|
NSLog("Bluetooth event")
|
|
430
508
|
}
|
|
431
509
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
// MARK: - CBCentralManagerDelegate Implementation
|
|
435
|
-
extension HybridMunimBluetooth: CBCentralManagerDelegate {
|
|
436
|
-
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
510
|
+
|
|
511
|
+
func handleCentralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
437
512
|
let state = central.state
|
|
438
513
|
NSLog("Bluetooth event")
|
|
439
514
|
}
|
|
440
515
|
|
|
441
|
-
func
|
|
516
|
+
func handleCentralManagerDidDiscover(_ central: CBCentralManager, peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
|
|
442
517
|
let deviceId = peripheral.identifier.uuidString
|
|
443
518
|
discoveredPeripherals[deviceId] = peripheral
|
|
444
519
|
|
|
@@ -448,15 +523,15 @@ extension HybridMunimBluetooth: CBCentralManagerDelegate {
|
|
|
448
523
|
NSLog("Bluetooth: deviceFound - %@", deviceId)
|
|
449
524
|
}
|
|
450
525
|
|
|
451
|
-
func
|
|
526
|
+
func handleCentralManagerDidConnect(_ central: CBCentralManager, peripheral: CBPeripheral) {
|
|
452
527
|
let deviceId = peripheral.identifier.uuidString
|
|
453
528
|
connectedPeripherals[deviceId] = peripheral
|
|
454
|
-
peripheral.delegate =
|
|
529
|
+
peripheral.delegate = peripheralDelegateProxy
|
|
455
530
|
|
|
456
531
|
NSLog("Bluetooth event")
|
|
457
532
|
}
|
|
458
533
|
|
|
459
|
-
func
|
|
534
|
+
func handleCentralManagerDidDisconnectPeripheral(_ central: CBCentralManager, peripheral: CBPeripheral, error: Error?) {
|
|
460
535
|
let deviceId = peripheral.identifier.uuidString
|
|
461
536
|
connectedPeripherals.removeValue(forKey: deviceId)
|
|
462
537
|
peripheralCharacteristics.removeValue(forKey: deviceId)
|
|
@@ -464,15 +539,12 @@ extension HybridMunimBluetooth: CBCentralManagerDelegate {
|
|
|
464
539
|
NSLog("Bluetooth event")
|
|
465
540
|
}
|
|
466
541
|
|
|
467
|
-
func
|
|
542
|
+
func handleCentralManagerDidFailToConnect(_ central: CBCentralManager, peripheral: CBPeripheral, error: Error?) {
|
|
468
543
|
let deviceId = peripheral.identifier.uuidString
|
|
469
544
|
NSLog("Bluetooth: connectionFailed")
|
|
470
545
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
// MARK: - CBPeripheralDelegate Implementation
|
|
474
|
-
extension HybridMunimBluetooth: CBPeripheralDelegate {
|
|
475
|
-
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
|
546
|
+
|
|
547
|
+
func handlePeripheralDidDiscoverServices(_ peripheral: CBPeripheral, error: Error?) {
|
|
476
548
|
let deviceId = peripheral.identifier.uuidString
|
|
477
549
|
|
|
478
550
|
guard let services = peripheral.services else { return }
|
|
@@ -484,7 +556,7 @@ extension HybridMunimBluetooth: CBPeripheralDelegate {
|
|
|
484
556
|
NSLog("Bluetooth event")
|
|
485
557
|
}
|
|
486
558
|
|
|
487
|
-
func
|
|
559
|
+
func handlePeripheralDidDiscoverCharacteristics(_ peripheral: CBPeripheral, service: CBService, error: Error?) {
|
|
488
560
|
let deviceId = peripheral.identifier.uuidString
|
|
489
561
|
|
|
490
562
|
guard let characteristics = service.characteristics else { return }
|
|
@@ -497,7 +569,7 @@ extension HybridMunimBluetooth: CBPeripheralDelegate {
|
|
|
497
569
|
NSLog("Bluetooth event")
|
|
498
570
|
}
|
|
499
571
|
|
|
500
|
-
func
|
|
572
|
+
func handlePeripheralDidUpdateValue(_ peripheral: CBPeripheral, characteristic: CBCharacteristic, error: Error?) {
|
|
501
573
|
let deviceId = peripheral.identifier.uuidString
|
|
502
574
|
|
|
503
575
|
guard let data = characteristic.value else { return }
|
|
@@ -507,7 +579,7 @@ extension HybridMunimBluetooth: CBPeripheralDelegate {
|
|
|
507
579
|
NSLog("Bluetooth: characteristicValueChanged")
|
|
508
580
|
}
|
|
509
581
|
|
|
510
|
-
func
|
|
582
|
+
func handlePeripheralDidWriteValue(_ peripheral: CBPeripheral, characteristic: CBCharacteristic, error: Error?) {
|
|
511
583
|
let deviceId = peripheral.identifier.uuidString
|
|
512
584
|
|
|
513
585
|
if let error = error {
|
|
@@ -517,7 +589,7 @@ extension HybridMunimBluetooth: CBPeripheralDelegate {
|
|
|
517
589
|
}
|
|
518
590
|
}
|
|
519
591
|
|
|
520
|
-
func
|
|
592
|
+
func handlePeripheralDidReadRSSI(_ peripheral: CBPeripheral, rssi RSSI: NSNumber, error: Error?) {
|
|
521
593
|
let deviceId = peripheral.identifier.uuidString
|
|
522
594
|
|
|
523
595
|
if let error = error {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "munim-bluetooth",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.24",
|
|
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",
|