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