Created
March 25, 2015 14:53
-
-
Save hyOzd/53748c81eaa73f1855ef to your computer and use it in GitHub Desktop.
Script to toggle bluetooth headset connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Toggles headset connection | |
import dbus | |
from dbus.mainloop.glib import DBusGMainLoop | |
# change this to your device name | |
device_to_connect = "BlueBuds X" | |
dbus_loop = DBusGMainLoop() | |
bus = dbus.SystemBus(mainloop=dbus_loop) | |
#Get dbus interface for headset | |
manager = bus.get_object('org.bluez', '/') | |
iface_m = dbus.Interface(manager, 'org.bluez.Manager') | |
adapterPath = iface_m.DefaultAdapter() | |
adapter = bus.get_object('org.bluez', adapterPath) | |
iface_a = dbus.Interface(adapter, 'org.bluez.Adapter') | |
for devicePath in iface_a.ListDevices(): | |
device = bus.get_object('org.bluez', devicePath) | |
iface_device = dbus.Interface(device, 'org.bluez.Device') | |
device_name = iface_device.GetProperties()["Name"] | |
if device_name == device_to_connect: | |
break | |
else: # for else | |
raise Exception("Device named %s not found!" % device_to_connect) | |
iface_a = dbus.Interface(device, 'org.bluez.AudioSink') | |
#Check state of connection | |
connected = iface_a.IsConnected() | |
print('Toggling connection. Please wait...') | |
# toggle connection | |
if not connected: | |
iface_a.Connect() | |
print('Connecting: ' + devicePath) | |
else: | |
iface_a.Disconnect() | |
print('Disconnecting: ' + devicePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment