diff --git a/OmniBLE/PumpManager/PodCommsSession.swift b/OmniBLE/PumpManager/PodCommsSession.swift index a3c2e0d0..5d223ee2 100644 --- a/OmniBLE/PumpManager/PodCommsSession.swift +++ b/OmniBLE/PumpManager/PodCommsSession.swift @@ -74,6 +74,9 @@ extension PodCommsError: LocalizedError { let faultDescription = String(describing: fault.faultEventCode) return String(format: LocalizedString("Pod Fault: %1$@", comment: "Format string for pod fault code"), faultDescription) case .commsError(let error): + if isVerboseBluetoothCommsError(error) { + return LocalizedString("Possible Bluetooth issue", comment: "Error description for possible bluetooth issue") + } return error.localizedDescription case .unacknowledgedMessage(_, let error): return error.localizedDescription @@ -136,7 +139,10 @@ extension PodCommsError: LocalizedError { return LocalizedString("Resume delivery", comment: "Recovery suggestion when pod is suspended") case .podFault: return nil - case .commsError: + case .commsError(let error): + if isVerboseBluetoothCommsError(error) { + return LocalizedString("Try adjusting pod position or toggle Bluetooth off and then on in iPhone Settings.", comment: "Recovery suggestion for possible bluetooth issue") + } return nil case .unacknowledgedMessage: return nil @@ -173,6 +179,28 @@ extension PodCommsError: LocalizedError { return false } } + + public func isVerboseBluetoothCommsError(_ error: Error) -> Bool { + if let peripheralManagerError = error as? PeripheralManagerError { + switch peripheralManagerError { + case .cbPeripheralError: + print("### Verbose Bluetooth comms error: \(peripheralManagerError.localizedDescription)") + return true + default: + break + } + } + if let podProtocolError = error as? PodProtocolError { + switch podProtocolError { + case .invalidLTKKey, .pairingException, .messageIOException, .couldNotParseMessageException: + print("### Verbose Bluetooth comms error: \(podProtocolError.localizedDescription)") + return true + default: + break + } + } + return false + } } public protocol PodCommsSessionDelegate: AnyObject { @@ -247,7 +275,9 @@ public class PodCommsSession { /// - PodCommsError.unexpectedResponse /// - PodCommsError.rejectedMessage /// - PodCommsError.nonceResyncFailed - /// - MessageError + /// - PodCommsError.commsError.MessageError + /// - PodCommsError.commsError.PeripheralManagerError + /// - PodCommsError.commsError.PodProtocolError func send(_ messageBlocks: [MessageBlock], beepBlock: MessageBlock? = nil, expectFollowOnMessage: Bool = false) throws -> T { var triesRemaining = 2 // Retries only happen for nonce resync