Meshtastic-Apple/Meshtastic/Accessory/Accessory Manager/AccessoryManager+Discovery.swift
Garth Vander Houwen 3eef38926f
2.7.7 Working Changes (#1551)
* Bump version

* update the translations (#1540)

update the translations

* Don't alert (with sound: .default) when updating Live Activity (#1536)

* Fix adding channels (#1532)

* Full translation into Spanish (#1529)

* tapback with any emoji (#1538)

* Call clearStaleNodes at start of sendWantConfig (#1535)

* NFC Tag contact (#1537)

* Accessorymanager background discovery (#1542)

* Don't add new BLE  devices to the device list in the backgournd

* Bump version

* Update Meshtastic/MeshtasticApp.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Meshtastic/MeshtasticApp.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Revert "Full translation into Spanish (#1529)" (#1543)

This reverts commit f25fdfb89f.

* Revert "update the translations (#1540)" (#1544)

This reverts commit cb2fd8cc15.

* Revert "NFC Tag contact (#1537)" (#1545)

This reverts commit 5c22b8b6e0.

* Update Meshtastic/Views/Messages/TapbackInputView.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Meshtastic/Helpers/EmojiOnlyTextField.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Revert "Accessorymanager background discovery (#1542)" (#1553)

This reverts commit 487f24b99a.

* Update protobufs

* Remove UI Kit code, clean up waypoint form emoji picker

* Remove redundant nested Task in tapback emoji handler (#1552)

* Initial plan

* Remove nested Task block in tapback handler

Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>

* Delete empty file

* Handle nil for emoji keyboard type extension

* Remove UI kit method from waypoint form emoji picker

* Remove UI kit emoji picker from tapback

* Add Exchange User Info (#1550)

* Emoji keyboard (#1559)

* Add file missing from project, must have merged badly

* Remove ui kit emoji keyboard

* Discovery background fixes (#1561)

* Make BLE Transport an actor to fix background discovery crashes

* Protobufs

* Update Meshtastic/Accessory/Transports/Bluetooth Low Energy/BLETransport.swift

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Throw too many retries error again, remove return

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Increase connection timeout

* Update protobufs

* Revert "Fix adding channels (#1532)" (#1562)

This reverts commit bff8ca018b.

---------

Co-authored-by: MGJ <62177301+MGJ520@users.noreply.github.com>
Co-authored-by: Mike Robbins <mrobbins@alum.mit.edu>
Co-authored-by: Benjamin Faershtein <119711889+RCGV1@users.noreply.github.com>
Co-authored-by: Alvaro Samudio <alvarosamudio@protonmail.com>
Co-authored-by: Mathew Kamkar <578302+matkam@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com>
Co-authored-by: Brian Hardie <777730+bhardie@users.noreply.github.com>
2026-01-15 14:13:40 -08:00

89 lines
2.7 KiB
Swift

//
// AccessoryManager+Discovery.swift
// Meshtastic
//
// Created by Jake Bordens on 7/23/25.
//
import Foundation
import OSLog
extension AccessoryManager {
private func discoverAllDevices() -> AsyncStream<DiscoveryEvent> {
AsyncStream { continuation in
let tasks = transports.map { transport in
Task {
Logger.transport.info("🔎 [Discovery] Discovery stream started for transport \(String(describing: transport.type), privacy: .public)")
for await event in await transport.discoverDevices() {
continuation.yield(event)
}
Logger.transport.info("🔎 [Discovery] Discovery stream closed for transport \(String(describing: transport.type), privacy: .public)")
}
}
continuation.onTermination = { _ in
Logger.transport.info("🔎 [Discovery] Cancelling discovery for all transports.")
tasks.forEach { $0.cancel() }
}
}
}
func startDiscovery() {
if discoveryTask != nil {
Logger.transport.debug("🔎 [Discovery] Existing discovery task is active.")
return
}
updateState(.discovering)
discoveryTask = Task { @MainActor in
for await event in self.discoverAllDevices() {
do {
try Task.checkCancellation()
switch event {
case .deviceFound(let newDevice), .deviceUpdated(let newDevice):
// Update existing device or add new
if let index = self.devices.firstIndex(where: { $0.id == newDevice.id }) {
// This device already exists.
var existing = self.devices[index]
existing.name = newDevice.name
existing.transportType = newDevice.transportType
existing.identifier = newDevice.identifier
existing.connectionState = newDevice.connectionState
existing.rssi = newDevice.rssi
self.devices[index] = existing
} else {
// This is a new device, add it to our list
self.devices.append(newDevice)
}
if self.shouldAutomaticallyConnectToPreferredPeripheral,
UserDefaults.autoconnectOnDiscovery, UserDefaults.preferredPeripheralId == newDevice.id.uuidString {
Logger.transport.debug("🔎 [Discovery] Found preferred peripheral \(newDevice.name)")
self.connectToPreferredDevice()
}
// Update the list of discovered devices on the main thread for presentation
// in the user interface
self.devices = devices.sorted { $0.name < $1.name }
case .deviceLost(let deviceId):
devices = devices.filter { $0.id != deviceId }
case .deviceReportedRssi(let deviceId, let newRssi):
updateDevice(deviceId: deviceId, key: \.rssi, value: newRssi)
}
} catch {
break
}
}
}
}
func stopDiscovery() {
devices.removeAll()
discoveryTask?.cancel()
discoveryTask?.cancel()
discoveryTask = nil
}
}