Increase peripheral connecting timeout to 8 seconds
Add retry logic if an operation fails Add CBError.operationNotSupported error handling
This commit is contained in:
parent
0052e9072a
commit
cef045de24
|
@ -18,7 +18,7 @@ import CBerkananSDK
|
||||||
extension TimeInterval {
|
extension TimeInterval {
|
||||||
|
|
||||||
public static let peripheralDiscoveryTimeout: TimeInterval = 12
|
public static let peripheralDiscoveryTimeout: TimeInterval = 12
|
||||||
public static let peripheralConnectingTimeout: TimeInterval = 5
|
public static let peripheralConnectingTimeout: TimeInterval = 8
|
||||||
public static let peripheralConnectionTimeout: TimeInterval = 3
|
public static let peripheralConnectionTimeout: TimeInterval = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -524,17 +524,8 @@ class BluetoothController: NSObject {
|
||||||
peripheral.name ?? ""
|
peripheral.name ?? ""
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
#if targetEnvironment(macCatalyst)
|
|
||||||
if !(self.servicesOfPeripherals[peripheral]?.isEmpty ?? true) {
|
|
||||||
self.flushPeripheral(peripheral)
|
self.flushPeripheral(peripheral)
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
self.cancelConnectionIfNeeded(for: peripheral)
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
self.flushPeripheral(peripheral)
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -565,6 +556,8 @@ class BluetoothController: NSObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func flushPeripheral(_ peripheral: CBPeripheral) {
|
private func flushPeripheral(_ peripheral: CBPeripheral) {
|
||||||
|
self.peripheralsToReadConfigurationsFrom.remove(peripheral)
|
||||||
|
self.messagesForPeripherals.removeValue(forKey: peripheral)
|
||||||
peripheral.delegate = nil
|
peripheral.delegate = nil
|
||||||
self.servicesOfPeripherals[peripheral]?.forEach {
|
self.servicesOfPeripherals[peripheral]?.forEach {
|
||||||
$0.rssi = nil
|
$0.rssi = nil
|
||||||
|
@ -602,8 +595,6 @@ class BluetoothController: NSObject {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.peripheralsToReadConfigurationsFrom.remove(peripheral)
|
|
||||||
self.messagesForPeripherals.removeValue(forKey: peripheral)
|
|
||||||
self.connectedPeripherals.remove(peripheral)
|
self.connectedPeripherals.remove(peripheral)
|
||||||
self.connectPeripheralsIfNeeded()
|
self.connectPeripheralsIfNeeded()
|
||||||
}
|
}
|
||||||
|
@ -762,6 +753,14 @@ extension BluetoothController: CBCentralManagerDelegate {
|
||||||
error as CVarArg? ?? ""
|
error as CVarArg? ?? ""
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
if #available(iOS 12.0, macOS 10.14, macCatalyst 13.0, tvOS 12.0,
|
||||||
|
watchOS 5.0, *) {
|
||||||
|
if let error = error as? CBError,
|
||||||
|
error.code == CBError.operationNotSupported {
|
||||||
|
self.peripheralsToReadConfigurationsFrom.remove(peripheral)
|
||||||
|
self.messagesForPeripherals.removeValue(forKey: peripheral)
|
||||||
|
}
|
||||||
|
}
|
||||||
self.cancelConnectionIfNeeded(for: peripheral)
|
self.cancelConnectionIfNeeded(for: peripheral)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -831,8 +830,13 @@ extension BluetoothController: CBPeripheralDelegate {
|
||||||
_ peripheral: CBPeripheral,
|
_ peripheral: CBPeripheral,
|
||||||
didDiscoverServices error: Error?
|
didDiscoverServices error: Error?
|
||||||
) {
|
) {
|
||||||
guard error == nil, let services = peripheral.services,
|
guard error == nil else {
|
||||||
services.count > 0 else {
|
self.cancelConnectionIfNeeded(for: peripheral)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard let services = peripheral.services, services.count > 0 else {
|
||||||
|
self.peripheralsToReadConfigurationsFrom.remove(peripheral)
|
||||||
|
self.messagesForPeripherals.removeValue(forKey: peripheral)
|
||||||
self.cancelConnectionIfNeeded(for: peripheral)
|
self.cancelConnectionIfNeeded(for: peripheral)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1099,6 +1103,7 @@ extension BluetoothController: CBPeripheralDelegate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
self.cancelConnectionIfNeeded(for: peripheral)
|
||||||
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
|
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
|
||||||
os_log(
|
os_log(
|
||||||
"Processing value failed=%@",
|
"Processing value failed=%@",
|
||||||
|
|
Loading…
Reference in New Issue