1 #!/usr/bin/python 2 # Script for testing the Attribute D-Bus API 3 4 import sys 5 from optparse import OptionParser, OptionValueError 6 from binascii import hexlify, unhexlify 7 8 import gobject 9 10 import sys 11 import dbus 12 import dbus.mainloop.glib 13 from optparse import OptionParser, make_option 14 15 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 16 bus = dbus.SystemBus() 17 mainloop = gobject.MainLoop() 18 19 manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager") 20 21 option_list = [ 22 make_option("-i", "--device", action="store", 23 type="string", dest="dev_id"), 24 ] 25 parser = OptionParser(option_list=option_list) 26 27 (options, args) = parser.parse_args() 28 29 if options.dev_id: 30 adapter_path = manager.FindAdapter(options.dev_id) 31 else: 32 adapter_path = manager.DefaultAdapter() 33 34 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), 35 "org.bluez.Adapter") 36 37 if (len(args) < 1): 38 print "Usage: %s <command>" % (sys.argv[0]) 39 print "" 40 print " list" 41 print " services <address>" 42 print " discover <service path>" 43 print " chars <service path>" 44 sys.exit(1) 45 46 if (args[0] == "list"): 47 for path in adapter.ListDevices(): 48 device = dbus.Interface(bus.get_object("org.bluez", path), 49 "org.bluez.Device") 50 devprop = device.GetProperties() 51 print "[ %s ]" % devprop["Address"] 52 for path in devprop["Services"]: 53 54 service = dbus.Interface(bus.get_object("org.bluez", path), 55 "org.bluez.Characteristic") 56 srvprop = service.GetProperties() 57 print " * %s" % (path) 58 print " UUID: %s" % srvprop["UUID"] 59 print " Chars: ", 60 for char in srvprop["Characteristics"]: 61 print "%s " % char, 62 print 63 print 64 print 65 sys.exit(0) 66 67 if (args[0] == "services"): 68 if (len(args) < 2): 69 print "Need address parameter" 70 else: 71 path = adapter.FindDevice(args[1]) 72 device = dbus.Interface(bus.get_object("org.bluez", path), 73 "org.bluez.Device") 74 properties = device.GetProperties() 75 for path in properties["Services"]: 76 print path 77 sys.exit(0) 78 79 if (args[0] == "discover"): 80 if (len(args) < 2): 81 print "Need service path parameter" 82 else: 83 service = dbus.Interface(bus.get_object("org.bluez", args[1]), 84 "org.bluez.Characteristic") 85 for path in service.DiscoverCharacteristics(): 86 print path 87 sys.exit(0) 88 89 if (args[0] == "chars"): 90 if (len(args) < 2): 91 print "Need service path parameter" 92 else: 93 service = dbus.Interface(bus.get_object("org.bluez", args[1]), 94 "org.bluez.Characteristic") 95 srvprop = service.GetProperties() 96 for path in srvprop["Characteristics"]: 97 print "[ %s ]" % (path) 98 char = dbus.Interface(bus.get_object("org.bluez", path), 99 "org.bluez.Characteristic") 100 charprop = char.GetProperties() 101 print " Name: %s" % charprop["Name"] 102 print " UUID: %s" % charprop["UUID"] 103 print 104 print 105 sys.exit(0) 106 107 print "Unknown command" 108 sys.exit(1) 109