@@ -129,16 +129,16 @@ class BluetoothConnectionManager(
129129 try {
130130 isActive = true
131131
132+ // Setup GATT server first
133+ setupGattServer()
134+
132135 // Start power manager and services
133136 connectionScope.launch {
134137 powerManager.start()
135-
136- // Setup GATT server after power manager is ready
137- setupGattServer()
138- delay(500 ) // Ensure GATT server is ready
138+ delay(300 ) // Brief delay to ensure GATT server is ready
139139
140140 startAdvertising()
141- delay(200 )
141+ delay(100 )
142142
143143 if (powerManager.shouldUseDutyCycle()) {
144144 Log .i(TAG , " Using power-aware duty cycling" )
@@ -410,10 +410,15 @@ class BluetoothConnectionManager(
410410 }
411411
412412 if (characteristic.uuid == CHARACTERISTIC_UUID ) {
413+ Log .d(TAG , " Server: Received packet from ${device.address} , size: ${value.size} bytes" )
413414 val packet = BitchatPacket .fromBinaryData(value)
414415 if (packet != null ) {
415416 val peerID = String (packet.senderID).replace(" \u0000 " , " " )
417+ Log .d(TAG , " Server: Parsed packet type ${packet.type} from $peerID " )
416418 delegate?.onPacketReceived(packet, peerID, device)
419+ } else {
420+ Log .w(TAG , " Server: Failed to parse packet from ${device.address} , size: ${value.size} bytes" )
421+ Log .w(TAG , " Server: Packet data: ${value.joinToString(" " ) { " %02x" .format(it) }} " )
417422 }
418423
419424 if (responseNeeded) {
@@ -458,48 +463,47 @@ class BluetoothConnectionManager(
458463 // Proper cleanup sequencing to prevent race conditions
459464 gattServer?.let { server ->
460465 Log .d(TAG , " Cleaning up existing GATT server" )
461- connectionScope.launch {
462- // Give time for pending callbacks to complete
463- delay(100 )
466+ try {
464467 server.close()
468+ } catch (e: Exception ) {
469+ Log .w(TAG , " Error closing existing GATT server: ${e.message} " )
465470 }
466471 }
467472
468- // Create new server after cleanup delay
469- connectionScope.launch {
470- delay(200 ) // Allow previous server to fully close
471-
472- if (! isActive) {
473- Log .d(TAG , " Service inactive, skipping GATT server creation" )
474- return @launch
475- }
476-
477- gattServer = bluetoothManager.openGattServer(context, serverCallback)
478-
479- // Create characteristic with notification support
480- characteristic = BluetoothGattCharacteristic (
481- CHARACTERISTIC_UUID ,
482- BluetoothGattCharacteristic .PROPERTY_READ or
483- BluetoothGattCharacteristic .PROPERTY_WRITE or
484- BluetoothGattCharacteristic .PROPERTY_WRITE_NO_RESPONSE or
485- BluetoothGattCharacteristic .PROPERTY_NOTIFY ,
486- BluetoothGattCharacteristic .PERMISSION_READ or
487- BluetoothGattCharacteristic .PERMISSION_WRITE
488- )
489-
490- val descriptor = BluetoothGattDescriptor (
491- UUID .fromString(" 00002902-0000-1000-8000-00805f9b34fb" ),
492- BluetoothGattDescriptor .PERMISSION_READ or BluetoothGattDescriptor .PERMISSION_WRITE
493- )
494- characteristic?.addDescriptor(descriptor)
495-
496- val service = BluetoothGattService (SERVICE_UUID , BluetoothGattService .SERVICE_TYPE_PRIMARY )
497- service.addCharacteristic(characteristic)
498-
499- gattServer?.addService(service)
500-
501- Log .i(TAG , " GATT server setup complete" )
473+ // Small delay to ensure cleanup is complete
474+ Thread .sleep(100 )
475+
476+ if (! isActive) {
477+ Log .d(TAG , " Service inactive, skipping GATT server creation" )
478+ return
502479 }
480+
481+ // Create new server
482+ gattServer = bluetoothManager.openGattServer(context, serverCallback)
483+
484+ // Create characteristic with notification support
485+ characteristic = BluetoothGattCharacteristic (
486+ CHARACTERISTIC_UUID ,
487+ BluetoothGattCharacteristic .PROPERTY_READ or
488+ BluetoothGattCharacteristic .PROPERTY_WRITE or
489+ BluetoothGattCharacteristic .PROPERTY_WRITE_NO_RESPONSE or
490+ BluetoothGattCharacteristic .PROPERTY_NOTIFY ,
491+ BluetoothGattCharacteristic .PERMISSION_READ or
492+ BluetoothGattCharacteristic .PERMISSION_WRITE
493+ )
494+
495+ val descriptor = BluetoothGattDescriptor (
496+ UUID .fromString(" 00002902-0000-1000-8000-00805f9b34fb" ),
497+ BluetoothGattDescriptor .PERMISSION_READ or BluetoothGattDescriptor .PERMISSION_WRITE
498+ )
499+ characteristic?.addDescriptor(descriptor)
500+
501+ val service = BluetoothGattService (SERVICE_UUID , BluetoothGattService .SERVICE_TYPE_PRIMARY )
502+ service.addCharacteristic(characteristic)
503+
504+ gattServer?.addService(service)
505+
506+ Log .i(TAG , " GATT server setup complete" )
503507 }
504508
505509 @Suppress(" DEPRECATION" )
@@ -836,10 +840,15 @@ class BluetoothConnectionManager(
836840
837841 override fun onCharacteristicChanged (gatt : BluetoothGatt , characteristic : BluetoothGattCharacteristic ) {
838842 val value = characteristic.value
843+ Log .d(TAG , " Client: Received packet from ${gatt.device.address} , size: ${value.size} bytes" )
839844 val packet = BitchatPacket .fromBinaryData(value)
840845 if (packet != null ) {
841846 val peerID = String (packet.senderID).replace(" \u0000 " , " " )
847+ Log .d(TAG , " Client: Parsed packet type ${packet.type} from $peerID " )
842848 delegate?.onPacketReceived(packet, peerID, gatt.device)
849+ } else {
850+ Log .w(TAG , " Client: Failed to parse packet from ${gatt.device.address} , size: ${value.size} bytes" )
851+ Log .w(TAG , " Client: Packet data: ${value.joinToString(" " ) { " %02x" .format(it) }} " )
843852 }
844853 }
845854 }
0 commit comments