Skip to content

Linux + iPhone Continuity / iMessage / SMS

mobile reconnect and rescan

Zack Bartel9723d68f414e
diff --git a/apple/Tether/Services/BonjourDiscovery.swift b/apple/Tether/Services/BonjourDiscovery.swift
index d975094..bc869c9 100644
--- a/apple/Tether/Services/BonjourDiscovery.swift
+++ b/apple/Tether/Services/BonjourDiscovery.swift
@@ -29,11 +29,16 @@ final class BonjourDiscovery {
private(set) var isScanning = false
private var browser: NWBrowser?
+ var onHostsChanged: (([DiscoveredHost]) -> Void)?
// MARK: - Public
/// Start scanning for `_tether._tcp` services.
- func startScanning() {
+ func startScanning(forceRestart: Bool = false) {
+ if forceRestart {
+ stopScanning(clearHosts: true)
+ }
+
guard !isScanning else { return }
let params = NWParameters()
@@ -51,8 +56,10 @@ final class BonjourDiscovery {
case .failed(let error):
print("BonjourDiscovery: Browser failed: \(error)")
self?.isScanning = false
+ self?.browser = nil
case .cancelled:
self?.isScanning = false
+ self?.browser = nil
default:
break
}
@@ -67,10 +74,14 @@ final class BonjourDiscovery {
}
/// Stop scanning.
- func stopScanning() {
+ func stopScanning(clearHosts: Bool = false) {
browser?.cancel()
browser = nil
isScanning = false
+ if clearHosts {
+ hosts = []
+ onHostsChanged?(hosts)
+ }
}
// MARK: - Private
@@ -88,6 +99,7 @@ final class BonjourDiscovery {
fingerprint: fingerprint
)
}
+ onHostsChanged?(hosts)
}
/// Extract the `fp=<sha256>` value from the Bonjour TXT record.
diff --git a/apple/Tether/TetherApp.swift b/apple/Tether/TetherApp.swift
index 29d9b24..a6b72ed 100644
--- a/apple/Tether/TetherApp.swift
+++ b/apple/Tether/TetherApp.swift
@@ -10,6 +10,7 @@ import SwiftUI
@main
struct TetherApp: App {
@State private var viewModel = TetherViewModel()
+ @Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
@@ -19,6 +20,9 @@ struct TetherApp: App {
.onAppear {
viewModel.initialize()
}
+ .onChange(of: scenePhase) { _, newPhase in
+ viewModel.handleScenePhase(newPhase)
+ }
}
}
}
diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swift
index 0584276..e8cf6fb 100644
--- a/apple/Tether/ViewModels/TetherViewModel.swift
+++ b/apple/Tether/ViewModels/TetherViewModel.swift
@@ -112,6 +112,10 @@ final class TetherViewModel {
/// The chunk size for file transfers (48KB raw → 64KB base64).
private let fileChunkSize = 48 * 1024
+ private var hasInitialized = false
+ private var pendingReconnectTask: Task<Void, Never>?
+ private var autoConnectingFingerprint: String?
+ private var manualDisconnect = false
private struct IncomingTransferBuffer {
let filename: String
@@ -125,6 +129,9 @@ final class TetherViewModel {
/// Call once at app startup.
func initialize() {
+ guard !hasInitialized else { return }
+ hasInitialized = true
+
if UserDefaults.standard.object(forKey: Self.autoSyncClipboardKey) != nil {
autoSyncClipboard = UserDefaults.standard.bool(forKey: Self.autoSyncClipboardKey)
}
@@ -132,19 +139,38 @@ final class TetherViewModel {
certificateManager.initialize()
setupConnectionHandlers()
startDiscovery()
+ scheduleAutoReconnectAttempt()
}
// MARK: - Discovery
- func startDiscovery() {
- discovery.startScanning()
+ func startDiscovery(forceRestart: Bool = false) {
+ discovery.startScanning(forceRestart: forceRestart)
if appState == .disconnected {
appState = .discovering
}
}
- func stopDiscovery() {
- discovery.stopScanning()
+ func stopDiscovery(clearHosts: Bool = false) {
+ discovery.stopScanning(clearHosts: clearHosts)
+ }
+
+ func refreshDiscovery() {
+ startDiscovery(forceRestart: true)
+ scheduleAutoReconnectAttempt()
+ }
+
+ func handleScenePhase(_ phase: ScenePhase) {
+ switch phase {
+ case .active:
+ manualDisconnect = false
+ refreshDiscovery()
+ case .background:
+ pendingReconnectTask?.cancel()
+ pendingReconnectTask = nil
+ default:
+ break
+ }
}
// MARK: - Connection
@@ -156,6 +182,7 @@ final class TetherViewModel {
return
}
+ manualDisconnect = false
appState = .connecting
connectedDeviceName = host.name
connection.connect(to: host.endpoint, identity: identity)
@@ -168,6 +195,7 @@ final class TetherViewModel {
return
}
+ manualDisconnect = false
appState = .connecting
connectedDeviceName = host
connection.connect(host: host, port: port, identity: identity)
@@ -175,6 +203,10 @@ final class TetherViewModel {
/// Disconnect from the daemon.
func disconnect() {
+ manualDisconnect = true
+ pendingReconnectTask?.cancel()
+ pendingReconnectTask = nil
+ autoConnectingFingerprint = nil
connection.disconnect()
appState = .disconnected
connectedDeviceName = nil
@@ -307,16 +339,30 @@ final class TetherViewModel {
// MARK: - Private — Connection Handlers
private func setupConnectionHandlers() {
+ discovery.onHostsChanged = { [weak self] _ in
+ self?.scheduleAutoReconnectAttempt()
+ }
+
connection.onStateChange = { [weak self] state in
guard let self else { return }
switch state {
case .connected:
+ self.pendingReconnectTask?.cancel()
+ self.pendingReconnectTask = nil
+ self.autoConnectingFingerprint = nil
self.handleConnected()
case .disconnected:
+ self.autoConnectingFingerprint = nil
self.appState = .disconnected
+ self.scheduleAutoReconnectAttempt()
case .failed(let msg):
+ let wasAutoReconnect = self.autoConnectingFingerprint != nil
+ self.autoConnectingFingerprint = nil
self.appState = .disconnected
- self.errorMessage = "Connection failed: \(msg)"
+ if !wasAutoReconnect {
+ self.errorMessage = "Connection failed: \(msg)"
+ }
+ self.scheduleAutoReconnectAttempt()
case .connecting:
self.appState = .connecting
}
@@ -346,6 +392,44 @@ final class TetherViewModel {
}
}
+ private func scheduleAutoReconnectAttempt() {
+ guard shouldAutoReconnect else { return }
+ guard autoConnectingFingerprint == nil else { return }
+
+ pendingReconnectTask?.cancel()
+ pendingReconnectTask = Task { [weak self] in
+ try? await Task.sleep(for: .milliseconds(700))
+ await MainActor.run {
+ self?.attemptAutoReconnect()
+ }
+ }
+ }
+
+ private var shouldAutoReconnect: Bool {
+ if manualDisconnect {
+ return false
+ }
+
+ switch appState {
+ case .disconnected, .discovering:
+ return true
+ case .connecting, .pairing, .connected:
+ return false
+ }
+ }
+
+ private func attemptAutoReconnect() {
+ guard shouldAutoReconnect else { return }
+ guard certificateManager.knownHosts.count == 1,
+ let fingerprint = certificateManager.knownHosts.keys.first else { return }
+
+ let matchingHosts = discovery.hosts.filter { $0.fingerprint == fingerprint }
+ guard matchingHosts.count == 1 else { return }
+
+ autoConnectingFingerprint = fingerprint
+ connectTo(host: matchingHosts[0])
+ }
+
private func handleMessage(_ message: TetherMessage) {
switch message.parsedCommand {
case .clipboardUpdated: