Skip to content

Instantly share code, notes, and snippets.

@pizzimenti
Created December 26, 2025 00:11
Show Gist options
  • Select an option

  • Save pizzimenti/58cb524eb4e12094bc0165fe150c9e83 to your computer and use it in GitHub Desktop.

Select an option

Save pizzimenti/58cb524eb4e12094bc0165fe150c9e83 to your computer and use it in GitHub Desktop.
script to output bluetooth audio device name and current audio codec in use
#!/bin/bash
#
# bt-codec.sh - Display Bluetooth audio device names and active codecs
#
# Usage: ./bt-codec.sh
# Output: Device Name - Codec (e.g., "soundcore P31i - AAC")
#
# Requires: PulseAudio or PipeWire with pactl command
#
set -euo pipefail
# Check if pactl is available
if ! command -v pactl &>/dev/null; then
echo "Error: pactl not found. Install pulseaudio-utils or pipewire-pulse." >&2
exit 1
fi
# Get all card info from PulseAudio/PipeWire
cards_output=$(pactl list cards 2>/dev/null)
# Check if any Bluetooth cards exist
if ! echo "$cards_output" | grep -q "bluez_card"; then
echo "No Bluetooth audio devices connected."
exit 0
fi
# Process each Bluetooth card section
echo "$cards_output" | awk '
# Start tracking when we find a Bluetooth card
/Name:.*bluez_card/ {
in_bt_card = 1
device = ""
active_profile = ""
delete profiles # Clear profiles array for new card
}
# Capture the device description (friendly name)
in_bt_card && /device.description/ {
# Extract text between quotes
match($0, /"([^"]+)"/, arr)
device = arr[1]
}
# Capture the active profile name
in_bt_card && /Active Profile:/ {
active_profile = $3
}
# We are in the Profiles section - store profile descriptions
in_bt_card && /^\t\toff:/ { in_profiles = 1 }
# Match profile lines and extract codec info
# Format: "profile-name: Description (codec NAME)"
in_bt_card && in_profiles && /^\t\t[a-z]/ {
# Get profile name (everything before the colon)
match($0, /^\t\t([^:]+):/, name_arr)
profile_name = name_arr[1]
# Extract codec from description if present
if (match($0, /codec ([A-Za-z0-9_-]+)/, codec_arr)) {
profiles[profile_name] = codec_arr[1]
} else {
profiles[profile_name] = profile_name
}
}
# End of card section - output results
in_bt_card && /^Card #[0-9]+$/ && device != "" {
if (active_profile in profiles) {
print device " - " profiles[active_profile]
} else if (active_profile != "") {
print device " - " active_profile
}
in_bt_card = 0
in_profiles = 0
}
# Handle last card in output (no trailing "Card #" marker)
END {
if (in_bt_card && device != "" && active_profile != "") {
if (active_profile in profiles) {
print device " - " profiles[active_profile]
} else {
print device " - " active_profile
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment