Repository: splendo/kaluga
Issue: Automatic reconnection doesn't work

Using connection settings: `ConnectionSettings(ConnectionSettings.ReconnectionSettings.Limited(3))`
Should try to reconnect to device 3 times.

Actually on each reconnect attempt inside Reconnecting state attempt value is always 0.

Changed files:
bluetooth/src/commonMain/kotlin/device/DeviceState.kt, bluetooth/src/iosMain/kotlin/scanner/Scanner.kt, bluetooth/src/commonMain/kotlin/device/DeviceConnectionManager.kt, bluetooth/src/commonMain/kotlin/Bluetooth.kt

diff --git a/bluetooth/src/commonMain/kotlin/Bluetooth.kt b/bluetooth/src/commonMain/kotlin/Bluetooth.kt
index ede2a9023..06f2b10ca 100644
--- a/bluetooth/src/commonMain/kotlin/Bluetooth.kt
+++ b/bluetooth/src/commonMain/kotlin/Bluetooth.kt
@@ -111,7 +111,7 @@ class Bluetooth internal constructor(
 
                 when (scanMode) {
                     is ScanMode.Scan -> {
-                        d("devices: ${scanState.discovered}")
+                        // d("devices: ${scanState.discovered}")
                         if (scanState.discovered.filter == scanMode.filter)
                             scanState.discovered.devices
                         else {
diff --git a/bluetooth/src/commonMain/kotlin/device/DeviceConnectionManager.kt b/bluetooth/src/commonMain/kotlin/device/DeviceConnectionManager.kt
index 13e87fb47..6334217de 100644
--- a/bluetooth/src/commonMain/kotlin/device/DeviceConnectionManager.kt
+++ b/bluetooth/src/commonMain/kotlin/device/DeviceConnectionManager.kt
@@ -82,7 +82,6 @@ abstract class BaseDeviceConnectionManager(
         }
 
         stateRepo.takeAndChangeState { state ->
-
             when (state) {
                 is DeviceState.Reconnecting -> {
                     state.retry().also {
@@ -91,16 +90,12 @@ abstract class BaseDeviceConnectionManager(
                         }
                     }
                 }
-                is DeviceState.Connected -> {
-                    when (connectionSettings.reconnectionSettings) {
-                        is ConnectionSettings.ReconnectionSettings.Always,
-                        is ConnectionSettings.ReconnectionSettings.Limited -> {
-                            state.reconnect
-                        }
-                        else -> {
-                            clean()
-                            state.didDisconnect
-                        }
+                is DeviceState.Connected -> when (connectionSettings.reconnectionSettings) {
+                    is ConnectionSettings.ReconnectionSettings.Always,
+                    is ConnectionSettings.ReconnectionSettings.Limited -> state.reconnect
+                    is ConnectionSettings.ReconnectionSettings.Never -> {
+                        clean()
+                        state.didDisconnect
                     }
                 }
                 is DeviceState.Disconnected -> state.remain()
diff --git a/bluetooth/src/commonMain/kotlin/device/DeviceState.kt b/bluetooth/src/commonMain/kotlin/device/DeviceState.kt
index c9101641e..5630c585b 100644
--- a/bluetooth/src/commonMain/kotlin/device/DeviceState.kt
+++ b/bluetooth/src/commonMain/kotlin/device/DeviceState.kt
@@ -144,12 +144,8 @@ sealed class DeviceState(
             }
 
         val reconnect = suspend {
-            val services = when (this) {
-                is Idle -> this.services
-                is HandlingAction -> this.services
-                else -> null
-            }
-            Reconnecting(0, services, deviceInfo, connectionManager)
+            // All services, characteristics and descriptors become invalidated after it disconnects
+            Reconnecting(0, null, deviceInfo, connectionManager)
         }
 
         suspend fun readRssi() {
diff --git a/bluetooth/src/iosMain/kotlin/scanner/Scanner.kt b/bluetooth/src/iosMain/kotlin/scanner/Scanner.kt
index 5c05dfa10..ae64ac502 100644
--- a/bluetooth/src/iosMain/kotlin/scanner/Scanner.kt
+++ b/bluetooth/src/iosMain/kotlin/scanner/Scanner.kt
@@ -94,7 +94,7 @@ actual class Scanner internal constructor(
         }.invoke()
 
         override fun centralManager(central: CBCentralManager, didDiscoverPeripheral: CBPeripheral, advertisementData: Map<Any?, *>, RSSI: NSNumber) {
-            info(TAG, "Did Discover Peripheral ${didDiscoverPeripheral.identifier.UUIDString}")
+            // info(TAG, "Did Discover Peripheral ${didDiscoverPeripheral.identifier.UUIDString}")
             scanner.discoverPeripheral(central, didDiscoverPeripheral, advertisementData.typedMap(), RSSI.intValue)
         }