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 option_list = [ 13 make_option("-i", "--device", action="store", 14 type="string", dest="dev_id"), 15 ] 16 parser = OptionParser(option_list=option_list) 17 18 (options, args) = parser.parse_args() 19 20 if options.dev_id: 21 adapter_path = manager.FindAdapter(options.dev_id) 22 else: 23 adapter_path = manager.DefaultAdapter() 24 25 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), 26 "org.bluez.Adapter") 27 28 if (len(args) < 1): 29 print "Usage: %s <address> [service]" % (sys.argv[0]) 30 sys.exit(1) 31 32 address = sys.argv[0] 33 34 if (len(args) < 2): 35 service = "spp" 36 else: 37 service = args[1] 38 39 path = adapter.FindDevice(address) 40 41 serial = dbus.Interface(bus.get_object("org.bluez", path), 42 "org.bluez.Serial") 43 44 node = serial.Connect(service) 45 46 print "Connected %s to %s" % (node, address) 47 48 print "Press CTRL-C to disconnect" 49 50 try: 51 time.sleep(1000) 52 print "Terminating connection" 53 except: 54 pass 55 56 serial.Disconnect(node) 57