diff --git a/Example/xDripG5/AppDelegate.swift b/Example/xDripG5/AppDelegate.swift index f230f6d9..c7ec5255 100644 --- a/Example/xDripG5/AppDelegate.swift +++ b/Example/xDripG5/AppDelegate.swift @@ -54,7 +54,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, TransmitterDelegate { func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - transmitter?.resumeScanning() + if let transmitter = transmitter , !transmitter.stayConnected { + transmitter.resumeScanning() + } } func applicationWillTerminate(_ application: UIApplication) { diff --git a/xDripG5/BluetoothManager.swift b/xDripG5/BluetoothManager.swift index 9333b2c2..3cf4e67f 100644 --- a/xDripG5/BluetoothManager.swift +++ b/xDripG5/BluetoothManager.swift @@ -106,7 +106,7 @@ class BluetoothManager: NSObject { #endif if let peripheralID = self.peripheral?.identifier, let peripheral = manager.retrievePeripherals(withIdentifiers: [peripheralID]).first { - log.info("Re-connecting to known peripheral %{public}@ in %zds", peripheral.identifier.uuidString, delay) + log.info("Re-connecting to known peripheral %{public}@ in %.1f s", peripheral.identifier.uuidString, delay) self.peripheral = peripheral self.manager.connect(peripheral, options: connectOptions) } else if let peripheral = manager.retrieveConnectedPeripherals(withServices: [ diff --git a/xDripG5/PeripheralManager+G5.swift b/xDripG5/PeripheralManager+G5.swift index 915d309c..78631e33 100644 --- a/xDripG5/PeripheralManager+G5.swift +++ b/xDripG5/PeripheralManager+G5.swift @@ -65,12 +65,14 @@ extension PeripheralManager { peripheral.writeValue(value, for: characteristic, type: type) } - guard let value = characteristic.value else { + let value = characteristic.value + + guard !characteristic.isNotifying || value != nil else { // TODO: This is an "unknown value" issue, not a timeout throw PeripheralManagerError.timeout } - return value + return value ?? Data() } } diff --git a/xDripG5/Transmitter.swift b/xDripG5/Transmitter.swift index 1b48956c..6d2c0ad0 100644 --- a/xDripG5/Transmitter.swift +++ b/xDripG5/Transmitter.swift @@ -247,27 +247,17 @@ fileprivate extension PeripheralManager { throw TransmitterError.authenticationError("Error writing keep-alive for bond: \(error)") } - let data: Data do { - // Wait for the OS dialog to pop-up before continuing. _ = try writeValue(BondRequestTxMessage().data, for: .authentication) - data = try readValue(for: .authentication, timeout: 15, expectingFirstByte: AuthStatusRxMessage.opcode) } catch let error { throw TransmitterError.authenticationError("Error writing bond request: \(error)") } - - guard let response = AuthStatusRxMessage(data: data) else { - throw TransmitterError.authenticationError("Unable to parse auth status: \(data)") - } - - guard response.bonded == 0x1 else { - throw TransmitterError.authenticationError("Transmitter failed to bond") - } } func control() throws -> Glucose { do { - try setNotifyValue(true, for: .control) + // Wait for up to 15s for the user to respond to the pairing request. + try setNotifyValue(true, for: .control, timeout: 15) } catch let error { throw TransmitterError.controlError("Error enabling notification: \(error)") }