BLE announces: delay after subscribe, queue announce on notify buffer full, and raise announce throttle (min interval + forced min)

This commit is contained in:
jack
2025-08-20 11:54:43 +02:00
parent eb16d128f2
commit 78917fa7c9
+10 -8
View File
@@ -66,7 +66,7 @@ final class BLEService: NSObject {
// Simple announce throttling // Simple announce throttling
private var lastAnnounceSent = Date.distantPast private var lastAnnounceSent = Date.distantPast
private let announceMinInterval: TimeInterval = 0.5 private let announceMinInterval: TimeInterval = 1.0
// Application state tracking (thread-safe) // Application state tracking (thread-safe)
#if os(iOS) #if os(iOS)
@@ -851,13 +851,14 @@ final class BLEService: NSObject {
// Handshake broadcast to centrals // Handshake broadcast to centrals
} }
} else { } else {
// Notification queue full - queue for retry on handshake packets // Notification queue full - queue for retry on handshake and announce packets
if packet.type == MessageType.noiseHandshake.rawValue { if packet.type == MessageType.noiseHandshake.rawValue || packet.type == MessageType.announce.rawValue {
collectionsQueue.async(flags: .barrier) { [weak self] in collectionsQueue.async(flags: .barrier) { [weak self] in
guard let self = self else { return } guard let self = self else { return }
if self.pendingNotifications.count < 20 { if self.pendingNotifications.count < 20 {
self.pendingNotifications.append((data: data, centrals: nil)) self.pendingNotifications.append((data: data, centrals: nil))
SecureLogger.log("📋 Queued handshake packet for retry (notification queue full)", let kind = packet.type == MessageType.announce.rawValue ? "announce" : "handshake"
SecureLogger.log("📋 Queued \(kind) packet for retry (notification queue full)",
category: SecureLogger.session, level: .debug) category: SecureLogger.session, level: .debug)
} }
} }
@@ -1343,7 +1344,7 @@ final class BLEService: NSObject {
let timeSinceLastAnnounce = now.timeIntervalSince(lastAnnounceSent) let timeSinceLastAnnounce = now.timeIntervalSince(lastAnnounceSent)
// Even forced sends should respect a minimum interval to avoid overwhelming BLE // Even forced sends should respect a minimum interval to avoid overwhelming BLE
let minInterval = forceSend ? 0.1 : announceMinInterval // Reduced from 0.2 for faster reconnection let minInterval = forceSend ? 0.2 : announceMinInterval
if timeSinceLastAnnounce < minInterval { if timeSinceLastAnnounce < minInterval {
// Skipping announce (rate limited) // Skipping announce (rate limited)
@@ -1989,9 +1990,10 @@ extension BLEService: CBPeripheralManagerDelegate {
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) { func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
SecureLogger.log("📥 Central subscribed: \(central.identifier.uuidString)", category: SecureLogger.session, level: .debug) SecureLogger.log("📥 Central subscribed: \(central.identifier.uuidString)", category: SecureLogger.session, level: .debug)
subscribedCentrals.append(central) subscribedCentrals.append(central)
// Send announce to the newly subscribed central after a delay to avoid overwhelming // Send announce to the newly subscribed central after a small delay to avoid overwhelming
// Sending announce to new subscriber messageQueue.asyncAfter(deadline: .now() + 0.4) { [weak self] in
sendAnnounce(forceSend: true) self?.sendAnnounce(forceSend: true)
}
} }
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) { func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {