-
-
Save dghodgson/8406352 to your computer and use it in GitHub Desktop.
#!/usr/bin/python | |
# based on monitor-bluetooth | |
# Changes by Domen Puncer <[email protected]> | |
import gobject | |
import dbus | |
import dbus.mainloop.glib | |
import os | |
def property_changed(name, value, path, interface): | |
iface = interface[interface.rfind(".") + 1:] | |
val = str(value) | |
print "{%s.PropertyChanged} [%s] %s = %s" % (iface, path, name, val) | |
# we want this event: {Control.PropertyChanged} [/org/bluez/16797/hci0/dev_00_24_7E_51_F7_52] Connected = true | |
# and when that happens: pactl load-module module-loopback source=bluez_source.00_24_7E_51_F7_52 | |
if iface == "Control" and name == "Connected" and val == "1": | |
bt_addr = "_".join(path.split('/')[-1].split('_')[1:]) | |
cmd = "pactl load-module module-loopback source=bluez_source.%s" % bt_addr | |
os.system(cmd) | |
# here we want this event: {Control.PropertyChanged} [/org/bluez/16797/hci0/dev_00_24_7E_51_F7_52] Connected = false | |
# and when that happens, we unload all loopback modules whose source is our bluetooth device | |
elif iface == "Control" and name == "Connected" and val == "0": | |
bt_addr = "_".join(path.split('/')[-1].split('_')[1:]) | |
cmd = "for i in $(pactl list short modules | grep module-loopback | grep source=bluez_source.%s | cut -f 1); do pactl unload-module $i; done" % bt_addr | |
os.system(cmd) | |
def object_signal(value, path, interface, member): | |
iface = interface[interface.rfind(".") + 1:] | |
val = str(value) | |
print "{%s.%s} [%s] Path = %s" % (iface, member, path, val) | |
if __name__ == '__main__': | |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
bus = dbus.SystemBus() | |
bus.add_signal_receiver(property_changed, bus_name="org.bluez", signal_name = "PropertyChanged", path_keyword="path", interface_keyword="interface") | |
mainloop = gobject.MainLoop() | |
mainloop.run() |
An alternative might be to monitor the AudioSource State = "connecting" signal, and load the loop-back module only on the first State = "connected" signal after a "connecting" state
Thanks a lot for this script! Very useful. I use it in my Debian server and it works just perfectly for me now. Love being able to hook up any smartphone, tablet, or laptop to my central server that's connected to the stereo. If anyone is ever interested I wrote my own little recipe for how I set things up (mainly for my own memory, should I ever have to do it again):
https://gist.github.com/boulund/8949499e17493e1c00db
Please,
For me work only it was changing the line 22 to:
alsaloop --rate=44100 --format=S16_LE --cdevice=pulse --buffer=10000 --tlatency=500000
Because alsaloop worked but the load-module module-loopback generate exception error the type "Failed load module"?
I actually had a similar problem. Disconnecting the bluetooth device properly triggered the removal of all loopback modules for the bluetooth devices, but pausing and restarting an audio stream would layer on more loopback modules. You had to disconnect to remove the modules.
I just revised the script though, and it seems to work for me. The problem was that the org.bluez.AudioSource interface would spit out "state=connected" every time an audio stream was re-started. The solution was to switch to org.bluez.Control and check for "Connected=true" instead.
I tried org.bluez.Device instead, but that causes the script to attempt loading the loopback module before PulseAudio could load the bluetooth-device module, so the loopback module would fail to load. Monitoring the Control interface solved that issue, and it's just as consistent as the Device interface.
Let me know if you have any problems with it though. I only have two android devices to test with.
EDIT: Actually, I'm curious as to what the output of
dbus-monitor sender=org.bluez
is on your system when you add/remove a bluetooth device.