Skip to content

Instantly share code, notes, and snippets.

@e-orz
Created May 26, 2019 14:54
Show Gist options
  • Save e-orz/6ac40189cdbc1ea30cdea8cd1948e945 to your computer and use it in GitHub Desktop.
Save e-orz/6ac40189cdbc1ea30cdea8cd1948e945 to your computer and use it in GitHub Desktop.
connect to mdr-1000x bluetooth audio headphone (A2DP profile, set default, move current streams to the new one)
bluetooth-connect-a2dp.sh
#!/usr/bin/expect -f
set timeout 2
set prompt "#"
spawn "bluetoothctl"
set address [lindex $argv 0]
expect "Agent registered"
expect -re $prompt
send "connect $address\r"
expect {
"Connection successful" { send "quit\r"; exit 0 }
timeout { send_user "Couldn't connect\n"; send "quit\r"; exit 1 }
}
send_user "Unexpected happened\n"
send "quit\r"
exit 1
#!/bin/bash
DEV=$1
PULSE_DEV="${DEV//:/_}"
bluetooth-connect-a2dp.sh $DEV
if [ $? -ne 0 ]
then
notify-send -u critical -i gtk-dialog-warning "Error Connecting to Blutooth audio $DEV" "Couldn't connect to the device"
exit 1
fi
pactl set-card-profile bluez_card.$PULSE_DEV a2dp_sink
if [ $? -ne 0 ]
then
notify-send -u critical -i gtk-dialog-warning "Error Connecting to Blutooth audio $DEV" "Couldn't move the sink to A2DP profile"
exit 1
fi
pactl set-default-sink bluez_sink.$PULSE_DEV.a2dp_sink
if [ $? -ne 0 ]
then
notify-send -u critical -i gtk-dialog-warning "Error Connecting to Blutooth audio $DEV" "Couldn't set the device to be the default"
exit 1
fi
pactl list short sink-inputs | cut -f1 | while read streamId
do
pactl move-sink-input "$streamId" bluez_sink.$PULSE_DEV.a2dp_sink
if [ $? -ne 0 ]
then
notify-send -u critical -i gtk-dialog-warning "Error Connecting to Blutooth audio $DEV" "Couldn't move stream $streamId to the device"
exit 1
fi
done
#change the below MAC address to the actual one
bluetooth-connect-and-move-audio.sh 00:00:00:00:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment