From 6f85faa6c0c044626ca6a1f36e8ecdf1f3d4b3a8 Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 4 Jul 2025 13:33:38 +0200 Subject: [PATCH] Fix BLE advertisement warnings and duplicate registrations - Remove 'Is Connectable' and 'Manufacturer Data' keys from BLE ads - Only use allowed keys: service UUIDs and local name - Prevent duplicate fingerprint registrations in favorites - Comment out unused fragment type string variable The warnings about advertisement keys should no longer appear. --- bitchat/Services/BluetoothMeshService.swift | 27 ++++++++------------- bitchat/ViewModels/ChatViewModel.swift | 9 +++++-- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/bitchat/Services/BluetoothMeshService.swift b/bitchat/Services/BluetoothMeshService.swift index bd7ba3d3..683b2582 100644 --- a/bitchat/Services/BluetoothMeshService.swift +++ b/bitchat/Services/BluetoothMeshService.swift @@ -395,18 +395,11 @@ class BluetoothMeshService: NSObject { // Use generic advertising to avoid identification // No identifying prefixes or app names for activist safety - // Include network size hint in manufacturer data for scaling decisions - var manufacturerData = Data() - manufacturerData.append(UInt8(estimatedNetworkSize)) // 1 byte network size - manufacturerData.append(UInt8(currentBatteryLevel * 100)) // 1 byte battery percentage - + // Only use allowed advertisement keys advertisementData = [ CBAdvertisementDataServiceUUIDsKey: [BluetoothMeshService.serviceUUID], // Use only peer ID without any identifying prefix - CBAdvertisementDataLocalNameKey: myPeerID, - CBAdvertisementDataIsConnectable: true, - // Custom manufacturer data (using Apple's ID to blend in) - CBAdvertisementDataManufacturerDataKey: manufacturerData + CBAdvertisementDataLocalNameKey: myPeerID ] isAdvertising = true @@ -876,7 +869,7 @@ class BluetoothMeshService: NSObject { // Send to connected peripherals (as central) var sentToPeripherals = 0 - for (peerID, peripheral) in connectedPeripherals { + for (_, peripheral) in connectedPeripherals { if let characteristic = peripheralCharacteristics[peripheral] { // Check if peripheral is connected before writing if peripheral.state == .connected { @@ -960,7 +953,7 @@ class BluetoothMeshService: NSObject { messageBloomFilter.reset() } - let _ = String(data: packet.senderID.trimmingNullBytes(), encoding: .utf8) ?? "unknown" + // let _ = String(data: packet.senderID.trimmingNullBytes(), encoding: .utf8) ?? "unknown" // Received packet type: \(packet.type) from \(peerID) @@ -1284,8 +1277,8 @@ class BluetoothMeshService: NSObject { } case .fragmentStart, .fragmentContinue, .fragmentEnd: - let fragmentTypeStr = packet.type == MessageType.fragmentStart.rawValue ? "START" : - (packet.type == MessageType.fragmentContinue.rawValue ? "CONTINUE" : "END") + // let fragmentTypeStr = packet.type == MessageType.fragmentStart.rawValue ? "START" : + // (packet.type == MessageType.fragmentContinue.rawValue ? "CONTINUE" : "END") // Handling fragment // Validate fragment has minimum required size @@ -1369,7 +1362,7 @@ class BluetoothMeshService: NSObject { } } - let totalTime = Double(fragments.count - 1) * delayBetweenFragments + let _ = Double(fragments.count - 1) * delayBetweenFragments // Total fragment send time } @@ -1524,7 +1517,7 @@ extension BluetoothMeshService: CBCentralManagerDelegate { } // Connection pooling with exponential backoff - let peripheralID = peripheral.identifier.uuidString + // peripheralID already declared above // Check if we should attempt connection (considering backoff) if let backoffTime = connectionBackoff[peripheralID], @@ -1536,7 +1529,7 @@ extension BluetoothMeshService: CBCentralManagerDelegate { // Check if we already have this peripheral in our pool if let pooledPeripheral = connectionPool[peripheralID] { // Reuse existing peripheral from pool - if pooledPeripheral.state == .disconnected { + if pooledPeripheral.state == CBPeripheralState.disconnected { // Reconnect if disconnected central.connect(pooledPeripheral, options: [ CBConnectPeripheralOptionNotifyOnConnectionKey: true, @@ -1723,7 +1716,7 @@ extension BluetoothMeshService: CBPeripheralDelegate { } // Use the sender ID from the packet, not our local mapping which might still be a temp ID - let localPeerID = connectedPeripherals.first(where: { $0.value == peripheral })?.key ?? "unknown" + let _ = connectedPeripherals.first(where: { $0.value == peripheral })?.key ?? "unknown" let packetSenderID = String(data: packet.senderID.trimmingNullBytes(), encoding: .utf8) ?? "unknown" // Received data from peer diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index d187ba1e..858dabb0 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -110,8 +110,13 @@ class ChatViewModel: ObservableObject { .prefix(16) // Use first 16 chars for brevity .lowercased() - peerIDToPublicKeyFingerprint[peerID] = String(fingerprint) - print("[FAVORITES] Registered fingerprint \(fingerprint) for peer \(peerID)") + let fingerprintStr = String(fingerprint) + + // Only register if not already registered + if peerIDToPublicKeyFingerprint[peerID] != fingerprintStr { + peerIDToPublicKeyFingerprint[peerID] = fingerprintStr + print("[FAVORITES] Registered fingerprint \(fingerprint) for peer \(peerID)") + } } func sendMessage(_ content: String) {