linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Diego Fernandez <aiguo.fernandez@gmail.com>
To: "linux-bluetooth@vger.kernel.org" <linux-bluetooth@vger.kernel.org>
Subject: bluetoothctl “Waiting to connect to bluetoothd…” in udev rule script
Date: Thu, 31 Aug 2017 11:32:11 -0400	[thread overview]
Message-ID: <1504193531.18331.1.camel@gmail.com> (raw)

Hi everyone,

I'm writing some scripts to automate fixing some issues with
PulseAudio, Bluetooth, and A2DP. I've found some useful examples
online, and I have a script that works when I run it manually. However,
when udev kicks off the rule, I get the error in $subject, which I
imagine means that bluetoothctl can't connect to bluetoothd. I also
tried using hcitool, but that doesn't fix my bluetooth issues.

I tried looking at the code for bluetoothctl, and it seems like it's
using dbus in the background. If that's the case, I would think that
setting XDG_RUNTIME_DIR, XAUTHORITY and DISPLAY should be enough.

I originally posted the question here: https://superuser.com/questions/
1245940/bluetoothctl-waiting-to-connect-to-bluetoothd-in-udev-rule-
script

Here are the scripts I have, which I believe should work with any
bluetooth headset as long as there's only 1 pulseaudio instance running
and only 1 headset connected:

/etc/udev/rules.d/80-bt-headset.rules
-------------------------------------------------
ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="/usr/local/bin/a2dp-fix-
wrapper"
-------------------------------------------------

/usr/local/bin/a2dp-fix-wrapper
-------------------------------------------------
#!/bin/bash -x
PID=$(pgrep pulseaudio)
USER=$(grep -z USER= /proc/$PID/environ | sed 's/.*=//')
USER_ID=$(id -u $USER)
HOME=$(echo $(getent passwd $USER )| cut -d : -f 6)

export XDG_RUNTIME_DIR=/run/user/$USER_ID
export XAUTHORITY=$HOME/.Xauthority
export DISPLAY=:0
export PULSE_RUNTIME_PATH=$XDG_RUNTIME_DIR/pulse/

sleep 5

sudo -u $USER -E /usr/local/bin/a2dp-fix &> /udev_output.txt
-------------------------------------------------

/usr/local/bin/a2dp-fix
-------------------------------------------------
#!/bin/bash -x
bt_device_addr=$(pacmd list-cards | grep -i 'name:.*bluez_card' | sed
-E 's/.*<?bluez_card\.([A-Z0-9_]+)>?/\1/')
device_mac=$(echo $bt_device_addr | sed 's/_/:/g')

a2dp_available=$(pacmd list-cards | grep -A30 bluez | grep "A2DP Sink"
| sed -E 's/.* available: ([a-z]+)\)/\1/g')

if [[ "$a2dp_available" == "no" ]]
then
    echo -e "connect $device_mac\nquit" | bluetoothctl
    sleep 5
    pacmd set-card-profile bluez_card.$bt_device_addr off
    pacmd set-card-profile bluez_card.$bt_device_addr a2dp_sink
    pacmd set-default-sink bluez_sink.$bt_device_addr.a2dp_sink
fi
-------------------------------------------------

The following things work:
* running `/usr/local/bin/a2dp-fix` as current user
* running `sudo /usr/local/bin/a2dp-fix-wrapper`

Any help would be greatly appreciated!

                 reply	other threads:[~2017-08-31 15:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1504193531.18331.1.camel@gmail.com \
    --to=aiguo.fernandez@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).