I have used a script and service file to automate this process to the best of my ability (grok helped) however they arent doing what is expected. No errors after creating and testing the service file.
I would also like the audio to default from 100 to 0 after boot - I havent attempted this yet.
/usr/local/bin/bt-audio-setup.sh
Code: Select all
#!/bin/bash
# Bluetooth device MAC address
DEVICE_MAC="E9:A7:26:03:1E:82"
DEVICE_MAC_UNDERSCORE="${DEVICE_MAC//:/_}"
CARD_NAME="bluez_card.${DEVICE_MAC_UNDERSCORE}"
SINK_NAME="bluez_sink.${DEVICE_MAC_UNDERSCORE}.a2dp_sink"
echo "[$(date)] Starting Bluetooth audio setup for ${DEVICE_MAC}"
# Wait for PipeWire/WirePlumber to be fully active (user services)
for i in {1..40}; do
if systemctl --user is-active --quiet pipewire pipewire-pulse wireplumber; then
echo "PipeWire and WirePlumber are active"
break
fi
sleep 1
done
if ! systemctl --user is-active --quiet pipewire; then
echo "ERROR: PipeWire failed to start"
exit 1
fi
# Wait for the Bluetooth device to be connected
for i in {1..20}; do
if bluetoothctl info "${DEVICE_MAC}" 2>/dev/null | grep -q "Connected: yes"; then
echo "Device ${DEVICE_MAC} is connected"
break
fi
sleep 3
done
if ! bluetoothctl info "${DEVICE_MAC}" | grep -q "Connected: yes"; then
echo "ERROR: Device ${DEVICE_MAC} not connected after timeout"
exit 1
fi
# Set A2DP profile
pactl set-card-profile "${CARD_NAME}" a2dp_sink
sleep 2
# Set as default sink
if pactl list sinks short | grep -q "${SINK_NAME}"; then
pactl set-default-sink "${SINK_NAME}"
echo "SUCCESS: ${SINK_NAME} is now the default audio output"
else
echo "ERROR: A2DP sink not available (wrong profile or device issue)"
exit 1
fiCode: Select all
[Unit]
Description=Setup Bluetooth Audio Output After PipeWire
After=pipewire.service pipewire-pulse.service wireplumber.service graphical-session.target
Requires=pipewire.service pipewire-pulse.service wireplumber.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/bt-audio-setup.sh
RemainAfterExit=true
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=default.targetCode: Select all
[Unit]
Description=Timer for Bluetooth Audio Setup
Requires=bt-audio-setup.service
[Timer]
OnBootSec=30sec
Persistent=true
[Install]
WantedBy=timers.target