Skip to content

Instantly share code, notes, and snippets.

@RavuAlHemio
Created December 8, 2013 03:21
Show Gist options
  • Save RavuAlHemio/7853008 to your computer and use it in GitHub Desktop.
Save RavuAlHemio/7853008 to your computer and use it in GitHub Desktop.
Powers on the bluetooth adapter, connects to the headset and sets it as the default PulseAudio sink.
#!/usr/bin/env python3
# Powers on the bluetooth adapter, connects to the headset and sets it as
# the default PulseAudio sink.
#
# Released into the public domain.
# http://creativecommons.org/publicdomain/zero/1.0/
from sys import stderr
from gi.repository import GLib, Gio
from subprocess import Popen
CONTROLLER_MAC = None
HEADSET_MAC = "00:11:22:33:44:55"
OBJMAN = "org.freedesktop.DBus.ObjectManager"
DBPROP = "org.freedesktop.DBus.Properties"
BLZDEV = "org.bluez.Device1"
ADPDEV = "org.bluez.Adapter1"
def BTHeadsetRaindance():
headsetMacUnderscores = HEADSET_MAC.replace(":", "_")
headsetMacEnd = "/dev_" + headsetMacUnderscores
headsetPath = None
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
bluezObjman = Gio.DBusProxy.new_sync(
bus, Gio.DBusProxyFlags.NONE, None, 'org.bluez', '/', OBJMAN,
None
)
managedObjects = bluezObjman.GetManagedObjects('()')
for obj in managedObjects.keys():
if obj.endswith(headsetMacEnd):
headsetPath = obj
if headsetPath is None:
print("Headset device not found!", file=stderr)
return
# power to the controller
controllerPath = headsetPath[:headsetPath.rindex("/")]
controllerProxy = Gio.DBusProxy.new_sync(
bus, Gio.DBusProxyFlags.NONE, None, 'org.bluez', controllerPath,
DBPROP, None
)
controllerProxy.Set(
'(ssv)', ADPDEV, 'Powered', GLib.Variant.new_boolean(True)
)
# connect the headset
headsetProxy = Gio.DBusProxy.new_sync(
bus, Gio.DBusProxyFlags.NONE, None, 'org.bluez', headsetPath,
BLZDEV, None
)
headsetProxy.Connect('()')
# tell PulseAudio to use it by default
Popen([
"pactl", "set-default-sink",
"bluez_sink.{0}".format(headsetMacUnderscores)
]).wait()
if __name__ == '__main__':
BTHeadsetRaindance()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment