mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 07:25:18 +00:00
Add debug logging to troubleshoot peer discovery issues
This commit is contained in:
@@ -33,6 +33,10 @@ class BluetoothMeshService: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startServices() {
|
func startServices() {
|
||||||
|
print("[DEBUG] Starting services...")
|
||||||
|
print("[DEBUG] Central state: \(centralManager.state.rawValue)")
|
||||||
|
print("[DEBUG] Peripheral state: \(peripheralManager.state.rawValue)")
|
||||||
|
|
||||||
// Start both central and peripheral services
|
// Start both central and peripheral services
|
||||||
if centralManager.state == .poweredOn {
|
if centralManager.state == .poweredOn {
|
||||||
startScanning()
|
startScanning()
|
||||||
@@ -45,6 +49,7 @@ class BluetoothMeshService: NSObject {
|
|||||||
|
|
||||||
func startAdvertising() {
|
func startAdvertising() {
|
||||||
guard peripheralManager.state == .poweredOn else {
|
guard peripheralManager.state == .poweredOn else {
|
||||||
|
print("[DEBUG] Cannot advertise - peripheral not powered on")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,13 +57,16 @@ class BluetoothMeshService: NSObject {
|
|||||||
CBAdvertisementDataServiceUUIDsKey: [BluetoothMeshService.serviceUUID],
|
CBAdvertisementDataServiceUUIDsKey: [BluetoothMeshService.serviceUUID],
|
||||||
CBAdvertisementDataLocalNameKey: "bitchat-\(myPeerID)"
|
CBAdvertisementDataLocalNameKey: "bitchat-\(myPeerID)"
|
||||||
]
|
]
|
||||||
|
print("[DEBUG] Starting advertising as: bitchat-\(myPeerID)")
|
||||||
peripheralManager.startAdvertising(advertisementData)
|
peripheralManager.startAdvertising(advertisementData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func startScanning() {
|
func startScanning() {
|
||||||
guard centralManager.state == .poweredOn else {
|
guard centralManager.state == .poweredOn else {
|
||||||
|
print("[DEBUG] Cannot scan - central not powered on")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
print("[DEBUG] Starting scan for peers...")
|
||||||
centralManager.scanForPeripherals(withServices: [BluetoothMeshService.serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
|
centralManager.scanForPeripherals(withServices: [BluetoothMeshService.serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +111,7 @@ class BluetoothMeshService: NSObject {
|
|||||||
ttl: self.maxTTL
|
ttl: self.maxTTL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
print("[DEBUG] Sending message: \(content)")
|
||||||
self.broadcastPacket(packet)
|
self.broadcastPacket(packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,6 +155,8 @@ class BluetoothMeshService: NSObject {
|
|||||||
processedMessages.removeAll()
|
processedMessages.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("[DEBUG] Received packet type: \(packet.type) from \(peerID)")
|
||||||
|
|
||||||
switch MessageType(rawValue: packet.type) {
|
switch MessageType(rawValue: packet.type) {
|
||||||
case .message:
|
case .message:
|
||||||
if let message = try? JSONDecoder().decode(BitchatMessage.self, from: packet.payload) {
|
if let message = try? JSONDecoder().decode(BitchatMessage.self, from: packet.payload) {
|
||||||
@@ -171,12 +182,14 @@ class BluetoothMeshService: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case .keyExchange:
|
case .keyExchange:
|
||||||
|
print("[DEBUG] Received key exchange from \(peerID)")
|
||||||
if let publicKeyData = try? JSONDecoder().decode(Data.self, from: packet.payload) {
|
if let publicKeyData = try? JSONDecoder().decode(Data.self, from: packet.payload) {
|
||||||
try? encryptionService.addPeerPublicKey(peerID, publicKeyData: publicKeyData)
|
try? encryptionService.addPeerPublicKey(peerID, publicKeyData: publicKeyData)
|
||||||
|
|
||||||
// Track this peer temporarily but don't announce until we get their name
|
// Track this peer temporarily but don't announce until we get their name
|
||||||
if peerID != "unknown" && peerID != myPeerID && !peerNicknames.keys.contains(peerID) {
|
if peerID != "unknown" && peerID != myPeerID && !peerNicknames.keys.contains(peerID) {
|
||||||
// Just track them, don't announce yet
|
// Just track them, don't announce yet
|
||||||
|
print("[DEBUG] Tracking peer \(peerID) after key exchange")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||||
}
|
}
|
||||||
@@ -185,6 +198,7 @@ class BluetoothMeshService: NSObject {
|
|||||||
// Send announce with our nickname immediately
|
// Send announce with our nickname immediately
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
if let self = self, let vm = self.delegate as? ChatViewModel {
|
if let self = self, let vm = self.delegate as? ChatViewModel {
|
||||||
|
print("[DEBUG] Sending announce to \(peerID)")
|
||||||
let announcePacket = BitchatPacket(
|
let announcePacket = BitchatPacket(
|
||||||
type: MessageType.announce.rawValue,
|
type: MessageType.announce.rawValue,
|
||||||
senderID: self.myPeerID.data(using: .utf8)!,
|
senderID: self.myPeerID.data(using: .utf8)!,
|
||||||
@@ -201,17 +215,21 @@ class BluetoothMeshService: NSObject {
|
|||||||
|
|
||||||
case .announce:
|
case .announce:
|
||||||
if let nickname = String(data: packet.payload, encoding: .utf8) {
|
if let nickname = String(data: packet.payload, encoding: .utf8) {
|
||||||
|
print("[DEBUG] Received announce from \(peerID): \(nickname)")
|
||||||
|
|
||||||
// Store the nickname
|
// Store the nickname
|
||||||
peerNicknames[peerID] = nickname
|
peerNicknames[peerID] = nickname
|
||||||
|
|
||||||
// Add to active peers if not already there
|
// Add to active peers if not already there
|
||||||
if peerID != "unknown" && peerID != myPeerID && !activePeers.contains(peerID) {
|
if peerID != "unknown" && peerID != myPeerID && !activePeers.contains(peerID) {
|
||||||
|
print("[DEBUG] Adding new peer \(peerID) to active peers")
|
||||||
activePeers.insert(peerID)
|
activePeers.insert(peerID)
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.delegate?.didConnectToPeer(nickname)
|
self.delegate?.didConnectToPeer(nickname)
|
||||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
print("[DEBUG] Peer \(peerID) already active or invalid")
|
||||||
// Just update the peer list to refresh nicknames
|
// Just update the peer list to refresh nicknames
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||||
@@ -227,14 +245,18 @@ class BluetoothMeshService: NSObject {
|
|||||||
|
|
||||||
extension BluetoothMeshService: CBCentralManagerDelegate {
|
extension BluetoothMeshService: CBCentralManagerDelegate {
|
||||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||||
|
print("[DEBUG] Central state changed to: \(central.state.rawValue)")
|
||||||
if central.state == .poweredOn {
|
if central.state == .poweredOn {
|
||||||
startScanning()
|
startScanning()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
||||||
|
print("[DEBUG] Discovered peripheral: \(peripheral.name ?? "unknown") RSSI: \(RSSI)")
|
||||||
|
|
||||||
// Connect to any device we discover - we'll filter by service later
|
// Connect to any device we discover - we'll filter by service later
|
||||||
if !discoveredPeripherals.contains(peripheral) {
|
if !discoveredPeripherals.contains(peripheral) {
|
||||||
|
print("[DEBUG] Connecting to peripheral...")
|
||||||
discoveredPeripherals.append(peripheral)
|
discoveredPeripherals.append(peripheral)
|
||||||
peripheral.delegate = self
|
peripheral.delegate = self
|
||||||
central.connect(peripheral, options: nil)
|
central.connect(peripheral, options: nil)
|
||||||
@@ -242,6 +264,7 @@ extension BluetoothMeshService: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||||
|
print("[DEBUG] Connected to peripheral: \(peripheral.name ?? "unknown")")
|
||||||
peripheral.delegate = self
|
peripheral.delegate = self
|
||||||
peripheral.discoverServices([BluetoothMeshService.serviceUUID])
|
peripheral.discoverServices([BluetoothMeshService.serviceUUID])
|
||||||
|
|
||||||
@@ -252,6 +275,8 @@ extension BluetoothMeshService: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connectedPeripherals[String(peerID)] = peripheral
|
connectedPeripherals[String(peerID)] = peripheral
|
||||||
|
print("[DEBUG] Connected to peer: \(peerID)")
|
||||||
|
print("[DEBUG] Active peers: \(activePeers)")
|
||||||
|
|
||||||
// Update peer list to show we're connecting
|
// Update peer list to show we're connecting
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
@@ -260,6 +285,8 @@ extension BluetoothMeshService: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
||||||
|
print("[DEBUG] Disconnected from peripheral: \(peripheral.name ?? "unknown"), error: \(error?.localizedDescription ?? "none")")
|
||||||
|
|
||||||
if let peerID = connectedPeripherals.first(where: { $0.value == peripheral })?.key {
|
if let peerID = connectedPeripherals.first(where: { $0.value == peripheral })?.key {
|
||||||
connectedPeripherals.removeValue(forKey: peerID)
|
connectedPeripherals.removeValue(forKey: peerID)
|
||||||
peripheralCharacteristics.removeValue(forKey: peripheral)
|
peripheralCharacteristics.removeValue(forKey: peripheral)
|
||||||
@@ -376,8 +403,10 @@ extension BluetoothMeshService: CBPeripheralDelegate {
|
|||||||
|
|
||||||
extension BluetoothMeshService: CBPeripheralManagerDelegate {
|
extension BluetoothMeshService: CBPeripheralManagerDelegate {
|
||||||
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
|
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
|
||||||
|
print("[DEBUG] Peripheral state changed to: \(peripheral.state.rawValue)")
|
||||||
switch peripheral.state {
|
switch peripheral.state {
|
||||||
case .poweredOn:
|
case .poweredOn:
|
||||||
|
print("[DEBUG] Peripheral powered on, setting up...")
|
||||||
setupPeripheral()
|
setupPeripheral()
|
||||||
startAdvertising()
|
startAdvertising()
|
||||||
default:
|
default:
|
||||||
@@ -390,11 +419,13 @@ extension BluetoothMeshService: CBPeripheralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
|
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
|
||||||
|
print("[DEBUG] Received write request")
|
||||||
for request in requests {
|
for request in requests {
|
||||||
if let data = request.value,
|
if let data = request.value,
|
||||||
let packet = BitchatPacket.from(data) {
|
let packet = BitchatPacket.from(data) {
|
||||||
// Try to identify peer from packet
|
// Try to identify peer from packet
|
||||||
let peerID = String(data: packet.senderID, encoding: .utf8) ?? "unknown"
|
let peerID = String(data: packet.senderID, encoding: .utf8) ?? "unknown"
|
||||||
|
print("[DEBUG] Write from peer: \(peerID)")
|
||||||
|
|
||||||
// Store the central for updates
|
// Store the central for updates
|
||||||
if !subscribedCentrals.contains(request.central) {
|
if !subscribedCentrals.contains(request.central) {
|
||||||
@@ -451,8 +482,10 @@ extension BluetoothMeshService: CBPeripheralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
|
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
|
||||||
|
print("[DEBUG] Central subscribed to notifications")
|
||||||
if !subscribedCentrals.contains(central) {
|
if !subscribedCentrals.contains(central) {
|
||||||
subscribedCentrals.append(central)
|
subscribedCentrals.append(central)
|
||||||
|
print("[DEBUG] New central subscribed, total: \(subscribedCentrals.count)")
|
||||||
|
|
||||||
// Send our public key to the newly connected central
|
// Send our public key to the newly connected central
|
||||||
let publicKeyData = encryptionService.publicKey.rawRepresentation
|
let publicKeyData = encryptionService.publicKey.rawRepresentation
|
||||||
@@ -490,6 +523,7 @@ extension BluetoothMeshService: CBPeripheralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update peer list to show we're connected (even without peer ID yet)
|
// Update peer list to show we're connected (even without peer ID yet)
|
||||||
|
print("[DEBUG] Updating peer list after subscription")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user