Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions AudioRecorder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Other existing imports
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.

import android.media.AudioManager;
import android.telecom.Call;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

// Function to detect connected Bluetooth devices
private AudioDevice getAudioSource() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
for (BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
if (device.getType() == BluetoothDevice.DEVICE_TYPE_AUDIO) {
// return audio source associated with the Bluetooth device
return AudioDevice.BLUETOOTH;
}
}
}
// Fallback to default audio source if no Bluetooth devices are connected
return AudioDevice.DEFAULT;
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This AudioRecorder.kt at the repository root is Java-like pseudo-code (semicolon imports, non-Kotlin function syntax, unknown types like AudioDevice) and will not compile in this Kotlin/Gradle project. It looks like an accidentally committed Copilot-generated snippet—please remove it from the PR (or convert it into valid Kotlin in the correct module/package if it’s actually needed).

Suggested change
import android.media.AudioManager;
import android.telecom.Call;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
// Function to detect connected Bluetooth devices
private AudioDevice getAudioSource() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
for (BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
if (device.getType() == BluetoothDevice.DEVICE_TYPE_AUDIO) {
// return audio source associated with the Bluetooth device
return AudioDevice.BLUETOOTH;
}
}
}
// Fallback to default audio source if no Bluetooth devices are connected
return AudioDevice.DEFAULT;
import android.media.AudioManager
import android.telecom.Call
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
private enum class AudioDevice {
BLUETOOTH,
DEFAULT,
}
// Function to detect connected Bluetooth devices
private fun getAudioSource(): AudioDevice {
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter?.isEnabled == true) {
val bondedDevices: Set<BluetoothDevice>? = bluetoothAdapter.bondedDevices
if (!bondedDevices.isNullOrEmpty()) {
// Return audio source associated with any bonded Bluetooth device
return AudioDevice.BLUETOOTH
}
}
// Fallback to default audio source if no Bluetooth devices are connected
return AudioDevice.DEFAULT

Copilot uses AI. Check for mistakes.
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.homeassistant.companion.android.common.util

import android.annotation.SuppressLint
import android.media.AudioDeviceInfo
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioManager.OnAudioFocusChangeListener
Expand All @@ -17,6 +18,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import android.os.Build

/**
* Wrapper around [AudioRecord] providing pre-configured audio recording functionality.
Expand All @@ -30,7 +32,6 @@ class AudioRecorder(private val audioManager: AudioManager?) {
// Docs: only format '[g]uaranteed to be supported by devices'
private const val AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT

private const val AUDIO_SOURCE = AudioSource.MIC
private const val CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO
}

Expand All @@ -50,6 +51,25 @@ class AudioRecorder(private val audioManager: AudioManager?) {
private var focusRequest: AudioFocusRequestCompat? = null
private val focusListener = OnAudioFocusChangeListener { /* Not used */ }

/**
* Determine the appropriate audio source based on connected devices.
* Prefers Bluetooth SCO if available, falls back to VOICE_COMMUNICATION,
* and finally defaults to MIC if neither is available.
*/
private fun getAudioSource(): Int {
if (audioManager == null) {
return AudioSource.MIC
}

// Check if Bluetooth SCO is available
return if (audioManager.isBluetoothScoAvailableOffCall()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the property instead of the function

Suggested change
return if (audioManager.isBluetoothScoAvailableOffCall()) {
return if (audioManager.isBluetoothScoAvailableOffCall) {

// Use VOICE_COMMUNICATION which is the standard for Bluetooth audio
AudioSource.VOICE_COMMUNICATION
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not use that all the time, it might be beneficial to other scenario than only Bluetooth.

Microphone audio source tuned for voice communications such as VoIP. It will for instance take advantage of echo cancellation or automatic gain control if available.

} else {
AudioSource.MIC
}
}
Comment on lines +69 to +88
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KDoc for getAudioSource() doesn't match the actual behavior: the function returns VOICE_COMMUNICATION only when Bluetooth SCO is available, otherwise MIC (there is no separate “fallback to VOICE_COMMUNICATION” path). Please update the documentation (or adjust the logic) so it accurately reflects what the code does.

Copilot uses AI. Check for mistakes.

/**
* Start the recorder. After calling this function, data will be available via [audioBytes].
* @throws SecurityException when missing permission to record audio
Expand Down Expand Up @@ -101,8 +121,9 @@ class AudioRecorder(private val audioManager: AudioManager?) {
private fun setupRecorder() {
if (recorder != null) stopRecording()

val audioSource = getAudioSource()
val bufferSize = minBufferSize() * 10
recorder = AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize)
recorder = AudioRecord(audioSource, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize)
}

private fun releaseRecorder() {
Expand All @@ -114,6 +135,14 @@ class AudioRecorder(private val audioManager: AudioManager?) {

private fun requestFocus() {
if (audioManager == null) return

// Enable Bluetooth SCO if available (requires API 11+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (audioManager.isBluetoothScoAvailableOffCall()) {
audioManager.startBluetoothSco()
}
}
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startBluetoothSco() is guarded by an API 11 check, but this project’s minSdk is 23, so the version guard is redundant. Also, consider tracking whether this instance actually started SCO so you can stop it deterministically (rather than relying on a global stop later).

Copilot uses AI. Check for mistakes.

if (focusRequest == null) {
focusRequest = AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE).run {
setAudioAttributes(
Expand All @@ -138,7 +167,15 @@ class AudioRecorder(private val audioManager: AudioManager?) {
}

private fun abandonFocus() {
if (audioManager == null || focusRequest == null) return
AudioManagerCompat.abandonAudioFocusRequest(audioManager, focusRequest!!)
if (audioManager == null) return

// Disable Bluetooth SCO (requires API 11+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
audioManager.stopBluetoothSco()
}

Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abandonFocus() calls stopBluetoothSco() unconditionally (only gated by the redundant API-level check), even if this AudioRecorder never started SCO. Since SCO is a global AudioManager state, this can interfere with other audio usage. Please gate the stop call on a local “SCO started” flag and/or the same condition used to start SCO.

Suggested change
// Disable Bluetooth SCO (requires API 11+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
audioManager.stopBluetoothSco()
}
// Disable Bluetooth SCO (requires API 11+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB &&
audioManager.isBluetoothScoAvailableOffCall()
) {
audioManager.stopBluetoothSco()
}

Copilot uses AI. Check for mistakes.
if (focusRequest != null) {
AudioManagerCompat.abandonAudioFocusRequest(audioManager, focusRequest!!)
}
}
}