Skip to content

Instantly share code, notes, and snippets.

@ntpeters
Last active August 29, 2015 14:16
Show Gist options
  • Save ntpeters/6b5647d1f820c775fda0 to your computer and use it in GitHub Desktop.
Save ntpeters/6b5647d1f820c775fda0 to your computer and use it in GitHub Desktop.
Configures bluetooth headphones with PulseAudio
#!/usr/bin/env bash
# A simple script to enable A2DP mode on bluetooth headphones with PulseAudio
# MAC address of the target bluetooth device
device_mac="57:D3:98:0A:26:05"
echo "Setting up bluetooth headphones. You will be prompted for password if required."
echo "Restarting bluetooth service..."
sudo systemctl restart bluetooth
sleep 1
echo "Attempting to reconnect device..."
try_connect=$(
{
sleep 2
echo connect "$device_mac"
sleep 5
echo quit
} | bluetoothctl
)
sleep 1
try_connect=${try_connect,,}
if [[ "$try_connect" = *"connection successful"* ]]; then
echo "Connection successful!"
echo "Identifying regisered audio device..."
translated_mac=$(echo "$device_mac" | tr ":" "_")
device_index=$(pactl list cards short | grep -i "$translated_mac" | cut -d $'\t' -f 1)
if [[ "$device_index" != "" ]]; then
echo "Audio device found."
echo "Updating device audio profile to A2DP..."
pactl set-card-profile $device_index a2dp_sink
if [ $? = 0 ]; then
echo "Audio profile updated successfully!"
echo "Identifying new output device..."
sink_index=$(pactl list sinks short | grep -i "$translated_mac" | cut -d $'\t' -f 1)
if [[ "$sink_index" != "" ]]; then
echo "Output device found."
echo "Setting to device for output..."
pactl set-default-sink "$sink_index"
if [ $? = 0 ]; then
echo "Output device set successfully!"
else
echo "Failed to set as output device!"
fi
else
echo "Device not registered as output device!"
fi
else
echo "Failed to update audio profile..."
fi
else
echo "Device not regiserted as audio device!"
fi
else
echo "Failed to connect device!"
if [[ "$try_connect" != *"$device_mac"* ]]; then
echo "Device not found!"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment