diff --git a/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt b/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt index 8194710..c42430a 100644 --- a/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt +++ b/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt @@ -1051,7 +1051,12 @@ class MainActivity : AudioServiceActivity() { override fun onAudioDevicesRemoved(removedDevices: Array) { // Emit the new active device after something disconnects. - val device = getActiveAudioDevice() + // AudioManager.getDevices() can still momentarily report a + // just-removed sink (observed on Bluetooth A2DP), so the + // removed ids are excluded explicitly instead of trusting + // getDevices() to already be up to date. + val excludedIds = removedDevices.map { it.id }.toSet() + val device = getActiveAudioDevice(excludeIds = excludedIds) Log.d(tag, "audio_devices.onDevicesRemoved active=$device") mainHandler.post { audioDevicesSink?.success(device) } } @@ -1083,7 +1088,7 @@ class MainActivity : AudioServiceActivity() { * * Type int values sent to Dart match the AudioDeviceInfo.TYPE_* constants. */ - private fun getActiveAudioDevice(): Map { + private fun getActiveAudioDevice(excludeIds: Set = emptySet()): Map { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return mapOf("id" to "builtin_speaker", "type" to 2, "name" to "Speaker") } @@ -1099,7 +1104,7 @@ class MainActivity : AudioServiceActivity() { AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, ) val best = outputs - .filter { it.isSink } + .filter { it.isSink && it.id !in excludeIds } .sortedBy { device -> val idx = priorityOrder.indexOf(device.type) if (idx == -1) Int.MAX_VALUE else idx diff --git a/lib/estado/estado_ecualizador.dart b/lib/estado/estado_ecualizador.dart index 19c8dd7..2d9559d 100644 --- a/lib/estado/estado_ecualizador.dart +++ b/lib/estado/estado_ecualizador.dart @@ -274,6 +274,14 @@ class EstadoEcualizador extends ChangeNotifier { _eqMultiDeviceEnabled = habilitado; await servicio.guardarToggleMultiDispositivo(habilitado); _configurarSuscripcionDispositivo(); + + // Re-seed the active device from a fresh query instead of leaving a + // stale _dispositivoActualId from before the toggle flip (it would + // otherwise only self-correct on the next native device-change event). + if (_eqMultiDeviceEnabled) { + await _sembrarDispositivoActual(); + } + if (notificar) notifyListeners(); }