1#!/usr/bin/python 2 3import sys 4import dbus 5from optparse import OptionParser, make_option 6 7bus = dbus.SystemBus() 8 9manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager") 10 11option_list = [ 12 make_option("-i", "--device", action="store", 13 type="string", dest="dev_id"), 14 ] 15parser = OptionParser(option_list=option_list) 16 17(options, args) = parser.parse_args() 18 19if options.dev_id: 20 adapter_path = manager.FindAdapter(options.dev_id) 21else: 22 adapter_path = manager.DefaultAdapter() 23 24adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), 25 "org.bluez.Adapter") 26 27if len(args) < 2: 28 print """Usage: %s <command> 29 30 connect <bdaddr> 31 disconnect <bdaddr> 32 """ % sys.argv[0] 33 sys.exit(1) 34 35device = adapter.FindDevice(args[1]) 36input = dbus.Interface(bus.get_object("org.bluez", device), 37 "org.bluez.Input") 38 39if args[0] == "connect": 40 input.Connect() 41elif args[0] == "disconnect": 42 input.Disconnect() 43else: 44 print "Unknown command" 45 sys.exit(1) 46