1 #!/usr/bin/python 2 3 import sys 4 import dbus 5 import re 6 7 bus = dbus.SystemBus() 8 9 manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager") 10 11 adapter = dbus.Interface(bus.get_object("org.bluez", manager.DefaultAdapter()), 12 "org.bluez.Adapter") 13 14 if (len(sys.argv) < 2): 15 print "Usage: %s <command>" % (sys.argv[0]) 16 print "" 17 print " list" 18 print " create <address>" 19 print " remove <address|path>" 20 print " discover <address> [pattern]" 21 print " class <address>" 22 print " name <address>" 23 print " alias <address> [alias]" 24 print " trusted <address> [yes/no]" 25 sys.exit(1) 26 27 if (sys.argv[1] == "list"): 28 list = adapter.ListDevices() 29 print list 30 sys.exit(0) 31 32 if (sys.argv[1] == "create"): 33 if (len(sys.argv) < 3): 34 print "Need address parameter" 35 else: 36 device = adapter.CreateDevice(sys.argv[2]) 37 print device 38 sys.exit(0) 39 40 if (sys.argv[1] == "remove"): 41 if (len(sys.argv) < 3): 42 print "Need address or object path parameter" 43 else: 44 try: 45 path = adapter.FindDevice(sys.argv[2]) 46 except: 47 path = sys.argv[2] 48 adapter.RemoveDevice(path) 49 sys.exit(0) 50 51 if (sys.argv[1] == "discover"): 52 if (len(sys.argv) < 3): 53 print "Need address parameter" 54 else: 55 path = adapter.FindDevice(sys.argv[2]) 56 device = dbus.Interface(bus.get_object("org.bluez", path), 57 "org.bluez.Device") 58 if (len(sys.argv) < 4): 59 pattern = "" 60 else: 61 pattern = sys.argv[3] 62 services = device.DiscoverServices(pattern); 63 for key in services.keys(): 64 p = re.compile(">.*?<") 65 xml = p.sub("><", services[key].replace("\n", "")) 66 print "[ 0x%5x ]" % (key) 67 print xml 68 print 69 sys.exit(0) 70 71 if (sys.argv[1] == "class"): 72 if (len(sys.argv) < 3): 73 print "Need address parameter" 74 else: 75 path = adapter.FindDevice(sys.argv[2]) 76 device = dbus.Interface(bus.get_object("org.bluez", path), 77 "org.bluez.Device") 78 properties = device.GetProperties() 79 print "0x%06x" % (properties["Class"]) 80 sys.exit(0) 81 82 if (sys.argv[1] == "name"): 83 if (len(sys.argv) < 3): 84 print "Need address parameter" 85 else: 86 path = adapter.FindDevice(sys.argv[2]) 87 device = dbus.Interface(bus.get_object("org.bluez", path), 88 "org.bluez.Device") 89 properties = device.GetProperties() 90 print properties["Name"] 91 sys.exit(0) 92 93 if (sys.argv[1] == "alias"): 94 if (len(sys.argv) < 3): 95 print "Need address parameter" 96 else: 97 path = adapter.FindDevice(sys.argv[2]) 98 device = dbus.Interface(bus.get_object("org.bluez", path), 99 "org.bluez.Device") 100 if (len(sys.argv) < 4): 101 properties = device.GetProperties() 102 print properties["Alias"] 103 else: 104 device.SetProperty("Alias", sys.argv[3]) 105 sys.exit(0) 106 107 if (sys.argv[1] == "trusted"): 108 if (len(sys.argv) < 3): 109 print "Need address parameter" 110 else: 111 path = adapter.FindDevice(sys.argv[2]) 112 device = dbus.Interface(bus.get_object("org.bluez", path), 113 "org.bluez.Device") 114 if (len(sys.argv) < 4): 115 properties = device.GetProperties() 116 print properties["Trusted"] 117 else: 118 if (sys.argv[3] == "yes"): 119 value = dbus.Boolean(1) 120 elif (sys.argv[3] == "no"): 121 value = dbus.Boolean(0) 122 else: 123 value = dbus.Boolean(sys.argv[3]) 124 device.SetProperty("Trusted", value) 125 sys.exit(0) 126 127 print "Unknown command" 128 sys.exit(1) 129