From d111a9537ff809f33d99b4d5d24997e5e2379496 Mon Sep 17 00:00:00 2001 From: jack Date: Thu, 10 Jul 2025 12:29:24 +0200 Subject: [PATCH] Add extensive Bluetooth debug logging to diagnose peer discovery issues - Add debug logs to centralManagerDidUpdateState and peripheralManagerDidUpdateState - Add logging to startServices to show manager states at initialization - Add logging when peripherals are discovered with RSSI values - Temporarily lower RSSI threshold from -90 to -100 dBm for testing - Add logging to show when scanning starts and if it's blocked --- bitchat.xcodeproj/project.pbxproj | 11 ++++------ bitchat/Services/BluetoothMeshService.swift | 24 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/bitchat.xcodeproj/project.pbxproj b/bitchat.xcodeproj/project.pbxproj index 9ee9abe3..9e4130c7 100644 --- a/bitchat.xcodeproj/project.pbxproj +++ b/bitchat.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 54; + objectVersion = 63; objects = { /* Begin PBXBuildFile section */ @@ -17,7 +17,6 @@ 2E71E320EA921498C57E023B /* BitchatMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FA8FF26ABDC1C642A8C7AE5 /* BitchatMessageTests.swift */; }; 31D147471B9F4E2815352DDA /* LinkPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AC141774F6671FCDC347DC7 /* LinkPreviewView.swift */; }; 4274B6016F755946FBF2513E /* MessageRetentionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67A85BFDDE65B4CD8BDF6DDB /* MessageRetentionService.swift */; }; - 46E1E1013CC18AB66105DB27 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 95F16C3A4A5621C74461D8D3 /* LaunchScreen.storyboard */; }; 4B747085D07A1BCE0F5BA612 /* BinaryProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2136C3E22D02D4A8DBE7EAB /* BinaryProtocol.swift */; }; 4E778E5A414571ACAC2A0F01 /* MessageRetentionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67A85BFDDE65B4CD8BDF6DDB /* MessageRetentionService.swift */; }; 5D95F2BFBE257A1225998389 /* BatteryOptimizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED176FF3B274E35C2D827894 /* BatteryOptimizer.swift */; }; @@ -112,14 +111,14 @@ 3FA8FF26ABDC1C642A8C7AE5 /* BitchatMessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitchatMessageTests.swift; sourceTree = ""; }; 527EB217EFDFAD4CF1C91F07 /* bitchat.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = bitchat.entitlements; sourceTree = ""; }; 53D535D9CE0B875F47402290 /* BinaryProtocolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryProtocolTests.swift; sourceTree = ""; }; - 61F92EBA29C47C0FCC482F1F /* bitchatShareExtension.appex */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "wrapper.app-extension"; path = bitchatShareExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; + 61F92EBA29C47C0FCC482F1F /* bitchatShareExtension.appex */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "wrapper.extensionkit-extension"; path = bitchatShareExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 67A85BFDDE65B4CD8BDF6DDB /* MessageRetentionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageRetentionService.swift; sourceTree = ""; }; 6DC1563390A15C042D059CF9 /* EncryptionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncryptionService.swift; sourceTree = ""; }; 763E0DBA9492A654FC0CDCB9 /* AppInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppInfoView.swift; sourceTree = ""; }; 8DE9CDF66D4E52D268851048 /* MessagePaddingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagePaddingTests.swift; sourceTree = ""; }; - 8F3A7C058C2C8E1A06C8CF8B /* bitchat_macOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = bitchat_macOS.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 8F3A7C058C2C8E1A06C8CF8B /* bitchat_macOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; name = bitchat_macOS.app; path = bitchat.app; sourceTree = BUILT_PRODUCTS_DIR; }; 95F16C3A4A5621C74461D8D3 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = ""; }; - 96D0D41CA19EE5A772AA8434 /* bitchat_iOS.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = bitchat_iOS.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 96D0D41CA19EE5A772AA8434 /* bitchat_iOS.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; name = bitchat_iOS.app; path = bitchat.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9AC141774F6671FCDC347DC7 /* LinkPreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkPreviewView.swift; sourceTree = ""; }; A08E03AA0C63E97C91749AEC /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; A2136C3E22D02D4A8DBE7EAB /* BinaryProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryProtocol.swift; sourceTree = ""; }; @@ -384,7 +383,6 @@ ); mainGroup = 18198ED912AAF495D8AF7763; minimizedProjectReferenceProxies = 1; - preferredProjectObjectVersion = 54; projectDirPath = ""; projectRoot = ""; targets = ( @@ -403,7 +401,6 @@ buildActionMask = 2147483647; files = ( 7DD72D928FF9DD3CA81B46B0 /* Assets.xcassets in Resources */, - 46E1E1013CC18AB66105DB27 /* LaunchScreen.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/bitchat/Services/BluetoothMeshService.swift b/bitchat/Services/BluetoothMeshService.swift index 8ea9dbbc..0efca2be 100644 --- a/bitchat/Services/BluetoothMeshService.swift +++ b/bitchat/Services/BluetoothMeshService.swift @@ -260,11 +260,13 @@ class BluetoothMeshService: NSObject { var randomBytes = [UInt8](repeating: 0, count: 4) _ = SecRandomCopyBytes(kSecRandomDefault, 4, &randomBytes) self.myPeerID = randomBytes.map { String(format: "%02x", $0) }.joined() + print("[BLUETOOTH DEBUG] BluetoothMeshService initializing with peer ID: \(self.myPeerID)") super.init() centralManager = CBCentralManager(delegate: self, queue: nil) peripheralManager = CBPeripheralManager(delegate: self, queue: nil) + print("[BLUETOOTH DEBUG] Central and Peripheral managers created") // Start bloom filter reset timer (reset every 5 minutes) bloomFilterResetTimer = Timer.scheduledTimer(withTimeInterval: 300.0, repeats: true) { [weak self] _ in @@ -362,13 +364,22 @@ class BluetoothMeshService: NSObject { func startServices() { // Starting services + print("[BLUETOOTH DEBUG] startServices called") + print("[BLUETOOTH DEBUG] Central state: \(centralManager?.state.rawValue ?? -1), Peripheral state: \(peripheralManager?.state.rawValue ?? -1)") + // Start both central and peripheral services if centralManager?.state == .poweredOn { + print("[BLUETOOTH DEBUG] Central already powered on, starting scan") startScanning() + } else { + print("[BLUETOOTH DEBUG] Central not yet powered on") } if peripheralManager?.state == .poweredOn { + print("[BLUETOOTH DEBUG] Peripheral already powered on, setting up") setupPeripheral() startAdvertising() + } else { + print("[BLUETOOTH DEBUG] Peripheral not yet powered on") } // Send initial announces after services are ready @@ -430,7 +441,9 @@ class BluetoothMeshService: NSObject { } func startScanning() { + print("[BLUETOOTH DEBUG] startScanning called, central state: \(centralManager?.state.rawValue ?? -1)") guard centralManager?.state == .poweredOn else { + print("[BLUETOOTH DEBUG] Cannot start scanning, central not powered on") return } @@ -2243,7 +2256,9 @@ class BluetoothMeshService: NSObject { extension BluetoothMeshService: CBCentralManagerDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) { // Central manager state updated + print("[BLUETOOTH DEBUG] Central manager state changed to: \(central.state.rawValue) (poweredOn = 5)") if central.state == .poweredOn { + print("[BLUETOOTH DEBUG] Central powered on, starting scan") startScanning() // Send announces when central manager is ready @@ -2256,9 +2271,14 @@ extension BluetoothMeshService: CBCentralManagerDelegate { func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { // Optimize for 300m range - only connect to strong enough signals let rssiValue = RSSI.intValue + print("[BLUETOOTH DEBUG] Discovered peripheral: \(peripheral.name ?? "Unknown") ID: \(peripheral.identifier) RSSI: \(rssiValue)") // Filter out very weak signals (below -90 dBm) to save battery - guard rssiValue > -90 else { return } + // TEMPORARILY LOWERED FOR DEBUGGING + guard rssiValue > -100 else { + print("[BLUETOOTH DEBUG] Ignoring peripheral due to very weak signal") + return + } // Throttle RSSI updates to save CPU let peripheralID = peripheral.identifier.uuidString @@ -2582,8 +2602,10 @@ extension BluetoothMeshService: CBPeripheralDelegate { extension BluetoothMeshService: CBPeripheralManagerDelegate { func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { // Peripheral manager state updated + print("[BLUETOOTH DEBUG] Peripheral manager state changed to: \(peripheral.state.rawValue) (poweredOn = 5)") switch peripheral.state { case .poweredOn: + print("[BLUETOOTH DEBUG] Peripheral powered on, setting up and advertising") setupPeripheral() startAdvertising()