#!/usr/bin/python # headset.py: a script for establishing a connection to a Bluetooth headset. # This is a modification to the version found on: # http://wiki.bluez.org/wiki/HOWTO/AudioDevices # The original author is unknown. # Modified by C.M. Brannon. # Last-modified: 01/14/2008. import dbus from sys import argv, exit, stderr from curses.ascii import isxdigit def usage(progname): print >>stderr, 'Usage: %s ' % (progname) print >>stderr, "For example: %s 00:01:23:45:67:89" % (progname) def validateBluetoothAddress(addr): components = addr.split(':') if len(components) != 6: print >>stderr, 'Address must have 6 components.' return False for comp in components: if not validateAddressComponent(comp): print >>stderr, '%s is not legal in a Bluetooth address.' % (comp) return False return True def validateAddressComponent(component): return isxdigit(component[0]) and isxdigit(component[1]) bt_addr = '' try: bt_addr = argv[1] if not validateBluetoothAddress(bt_addr): print >>stderr, "Invalid address. Address must be of the form 00:01:23:45:67:89." exit(1) except IndexError: usage(argv[0]) exit(1) bus = dbus.SystemBus() manager = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager') bus_id = manager.ActivateService('audio') audio = dbus.Interface(bus.get_object(bus_id, '/org/bluez/audio'), 'org.bluez.audio.Manager') path = audio.CreateHeadset(bt_addr) #audio.ChangeDefaultHeadset(path) #change the device to be used by default headset = dbus.Interface (bus.get_object(bus_id, path), 'org.bluez.audio.Headset') #Connect and Play are not required in PCM mode headset.Connect() headset.Play()