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