diff --git a/libpebble3/src/commonMain/kotlin/io/rebble/libpebblecommon/connection/bt/ble/pebble/ConnectivityWatcher.kt b/libpebble3/src/commonMain/kotlin/io/rebble/libpebblecommon/connection/bt/ble/pebble/ConnectivityWatcher.kt index f194c9daf..8c06fdd8b 100644 --- a/libpebble3/src/commonMain/kotlin/io/rebble/libpebblecommon/connection/bt/ble/pebble/ConnectivityWatcher.kt +++ b/libpebble3/src/commonMain/kotlin/io/rebble/libpebblecommon/connection/bt/ble/pebble/ConnectivityWatcher.kt @@ -88,14 +88,29 @@ class ConnectivityStatus(characteristicValue: ByteArray) { val pairingErrorCode: PairingErrorCode init { - val flags = characteristicValue[0] - connected = flags and 0b1 > 0 - paired = flags and 0b10 > 0 - encrypted = flags and 0b100 > 0 - hasBondedGateway = flags and 0b1000 > 0 - supportsPinningWithoutSlaveSecurity = flags and 0b10000 > 0 - hasRemoteAttemptedToUseStalePairing = flags and 0b100000 > 0 - pairingErrorCode = PairingErrorCode.getByValue(characteristicValue[3]) + // Handle empty array (can happen with recovery firmware) + if (characteristicValue.isEmpty()) { + connected = false + paired = false + encrypted = false + hasBondedGateway = false + supportsPinningWithoutSlaveSecurity = false + hasRemoteAttemptedToUseStalePairing = false + pairingErrorCode = PairingErrorCode.NO_ERROR + } else { + val flags = characteristicValue[0] + connected = flags and 0b1 > 0 + paired = flags and 0b10 > 0 + encrypted = flags and 0b100 > 0 + hasBondedGateway = flags and 0b1000 > 0 + supportsPinningWithoutSlaveSecurity = flags and 0b10000 > 0 + hasRemoteAttemptedToUseStalePairing = flags and 0b100000 > 0 + pairingErrorCode = if (characteristicValue.size > 3) { + PairingErrorCode.getByValue(characteristicValue[3]) + } else { + PairingErrorCode.NO_ERROR + } + } } override fun toString(): String = diff --git a/pebble/src/commonMain/kotlin/coredevices/pebble/ui/DebugFirmwareSideload.kt b/pebble/src/commonMain/kotlin/coredevices/pebble/ui/DebugFirmwareSideload.kt index 32cde1e69..30a2d34f6 100644 --- a/pebble/src/commonMain/kotlin/coredevices/pebble/ui/DebugFirmwareSideload.kt +++ b/pebble/src/commonMain/kotlin/coredevices/pebble/ui/DebugFirmwareSideload.kt @@ -84,7 +84,15 @@ private sealed class UiFirmwareUpdateStatus { "Couldn't start: ${status.error.message()}" ) - is FirmwareUpdater.FirmwareUpdateStatus.NotInProgress.Idle -> Idle + is FirmwareUpdater.FirmwareUpdateStatus.NotInProgress.Idle -> { + // Check if there was a failure + val failure = status.lastFailure + if (failure != null) { + Error(failure, failure.message ?: "Unknown error") + } else { + Idle + } + } } } }