1 #!/usr/bin/python 2 3 import sys 4 import time 5 import dbus 6 from optparse import OptionParser, make_option 7 8 bus = dbus.SystemBus() 9 10 manager = dbus.Interface(bus.get_object("org.bluez", "/"), 11 "org.bluez.Manager") 12 13 option_list = [ 14 make_option("-i", "--device", action="store", 15 type="string", dest="dev_id"), 16 ] 17 parser = OptionParser(option_list=option_list) 18 19 (options, args) = parser.parse_args() 20 21 if options.dev_id: 22 adapter_path = manager.FindAdapter(options.dev_id) 23 else: 24 adapter_path = manager.DefaultAdapter() 25 26 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), 27 "org.bluez.Adapter") 28 29 if (len(args) < 1): 30 print "Usage: %s <address> [service]" % (sys.argv[0]) 31 sys.exit(1) 32 33 address = args[0] 34 35 if (len(args) < 2): 36 service = "panu" 37 else: 38 service = args[1] 39 40 device = adapter.FindDevice(address) 41 42 network = dbus.Interface(bus.get_object("org.bluez", device), 43 "org.bluez.Network") 44 45 iface = network.Connect(service) 46 47 print "Connected %s to %s" % (device, address) 48 49 print "Press CTRL-C to disconnect" 50 51 try: 52 time.sleep(1000) 53 print "Terminating connection" 54 except: 55 pass 56 57 network.Disconnect() 58