Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): correctly handle ble enabled behaviour #125

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions library/ios/BleDidcomm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class BleDidcomm: React.RCTEventEmitter {
resolve: RCTPromiseResolveBlock,
reject _: RCTPromiseRejectBlock
) {
let cm = CBCentralManager(delegate: nil, queue: .main)
Thread.sleep(forTimeInterval: 0.05)
resolve(cm.state == .poweredOn)
let cm = CentralManager(sendEvent: self.sendEvent)
resolve(cm.centralManager.state == .poweredOn)
}

@objc func startPeripheral(
Expand Down Expand Up @@ -51,9 +50,9 @@ class BleDidcomm: React.RCTEventEmitter {
return
}
peripheralManager.setService(
serviceUUID: serviceUUID,
writeCharacteristicUUID: writeCharacteristicUUID,
indicationCharacteristicUUID: indicationCharacteristicUUID)
serviceUUID: serviceUUID,
writeCharacteristicUUID: writeCharacteristicUUID,
indicationCharacteristicUUID: indicationCharacteristicUUID)
resolve(nil)

}
Expand Down Expand Up @@ -159,7 +158,7 @@ class BleDidcomm: React.RCTEventEmitter {
} catch CentralManager.CentralManagerError.noDefinedService {
reject("error", "Set the service before calling the scan function", nil)
} catch {
reject("error", "unexpected error", nil)
reject("error", "unexpected error", nil)
}
}

Expand Down
3 changes: 2 additions & 1 deletion library/ios/BleDidcommEventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ extension BleDidcomm {
override open func supportedEvents() -> [String] {
[
"onDiscoverPeripheral", "onConnectedPeripheral", "onReceivedWriteWithoutResponse",
"onReceivedNotification", "onDisconnectedPeripheral", "onDisconnectedCentral", "onConnectedCentral"
"onReceivedNotification", "onDisconnectedPeripheral", "onDisconnectedCentral",
"onConnectedCentral",
]
}
}
7 changes: 3 additions & 4 deletions library/ios/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class CentralManager: NSObject {
case noDefinedService
}

var isPoweredOn = false
var serviceUUID: CBUUID?
var writeCharacteristicUUID: CBUUID?
var indicationCharacteristicUUID: CBUUID?
Expand All @@ -36,7 +35,7 @@ class CentralManager: NSObject {
queue: .main
)

while !isPoweredOn { Thread.sleep(forTimeInterval: 0.05) }
while centralManager.state == .unknown { Thread.sleep(forTimeInterval: 0.05) }
}

func shutdownCentral() {
Expand Down Expand Up @@ -85,8 +84,8 @@ class CentralManager: NSObject {
}

self.centralManager.scanForPeripherals(
withServices: [serviceUUID],
options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
withServices: [serviceUUID],
options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])

}

Expand Down
9 changes: 1 addition & 8 deletions library/ios/CentralManagerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import Foundation
import os

extension CentralManager: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
isPoweredOn = true
default:
os_log("Unknown state")
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {}

func centralManager(
_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData _: [String: Any],
Expand Down
2 changes: 1 addition & 1 deletion library/ios/CentralManagerPeripheralDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension CentralManager: CBPeripheralDelegate {
peripheral.discoverCharacteristics(
[
writeCharacteristicUUID.unsafelyUnwrapped,
indicationCharacteristicUUID.unsafelyUnwrapped
indicationCharacteristicUUID.unsafelyUnwrapped,
], for: service)
}
}
Expand Down
4 changes: 2 additions & 2 deletions library/ios/Constants.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
enum Constants {
/* The number of bytes we adjust for in the chunking algorithm to account for the data header
/* The number of bytes we adjust for in the chunking algorithm to account for the data header
(3 bytes) + 9 extra bytes to be sure = 12 bytes */
static let numberOfBytesForHeader: Int = 12
static let numberOfBytesForHeader: Int = 12
}
7 changes: 4 additions & 3 deletions library/ios/PeripheralManagerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ extension PeripheralManager: CBPeripheralManagerDelegate {
}

func peripheralManager(
_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic
_ peripheral: CBPeripheralManager, central: CBCentral,
didUnsubscribeFrom characteristic: CBCharacteristic
) {
connectedCentral = nil
sendEvent("onDisconnectedCentral", ["identifier": central.identifier.uuidString ])
sendEvent("onDisconnectedCentral", ["identifier": central.identifier.uuidString])
}

func peripheralManager(
Expand All @@ -32,7 +33,7 @@ extension PeripheralManager: CBPeripheralManagerDelegate {
os_log("Error already connected to a single central")
return
}
sendEvent("onConnectedCentral", ["identifier": central.identifier.uuidString ])
sendEvent("onConnectedCentral", ["identifier": central.identifier.uuidString])
connectedCentral = central
}

Expand Down
Loading