3 from __future__ import absolute_import, print_function, unicode_literals
5 from gi.repository import GObject
9 import dbus.mainloop.glib
11 from optparse import OptionParser, make_option
13 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
14 bus = dbus.SystemBus()
15 mainloop = GObject.MainLoop()
17 manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
20 make_option("-i", "--device", action="store",
21 type="string", dest="dev_id"),
23 parser = OptionParser(option_list=option_list)
25 (options, args) = parser.parse_args()
28 adapter_path = manager.FindAdapter(options.dev_id)
30 adapter_path = manager.DefaultAdapter()
32 adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
36 print("Usage: %s <command>" % (sys.argv[0]))
39 print(" services <address>")
40 print(" create <address>")
41 print(" remove <address|path>")
42 print(" disconnect <address>")
43 print(" discover <address> [pattern]")
44 print(" class <address>")
45 print(" name <address>")
46 print(" alias <address> [alias]")
47 print(" trusted <address> [yes/no]")
48 print(" blocked <address> [yes/no]")
51 if (args[0] == "list"):
52 for path in adapter.ListDevices():
53 device = dbus.Interface(bus.get_object("org.bluez", path),
55 properties = device.GetProperties()
56 print("%s %s" % (properties["Address"], properties["Alias"]))
60 def create_device_reply(device):
61 print("New device (%s)" % device)
65 def create_device_error(error):
66 print("Creating device failed: %s" % error)
70 if (args[0] == "create"):
72 print("Need address parameter")
74 adapter.CreateDevice(args[1],
75 reply_handler=create_device_reply,
76 error_handler=create_device_error)
79 if (args[0] == "remove"):
81 print("Need address or object path parameter")
84 path = adapter.FindDevice(args[1])
87 adapter.RemoveDevice(path)
90 if (args[0] == "disconnect"):
92 print("Need address parameter")
94 path = adapter.FindDevice(args[1])
95 device = dbus.Interface(bus.get_object("org.bluez", path),
100 if (args[0] == "discover"):
102 print("Need address parameter")
104 path = adapter.FindDevice(args[1])
105 device = dbus.Interface(bus.get_object("org.bluez", path),
111 services = device.DiscoverServices(pattern);
112 for key in services.keys():
113 p = re.compile(">.*?<")
114 xml = p.sub("><", services[key].replace("\n", ""))
115 print("[ 0x%5x ]" % (key))
120 if (args[0] == "class"):
122 print("Need address parameter")
124 path = adapter.FindDevice(args[1])
125 device = dbus.Interface(bus.get_object("org.bluez", path),
127 properties = device.GetProperties()
128 print("0x%06x" % (properties["Class"]))
131 if (args[0] == "name"):
133 print("Need address parameter")
135 path = adapter.FindDevice(args[1])
136 device = dbus.Interface(bus.get_object("org.bluez", path),
138 properties = device.GetProperties()
139 print(properties["Name"])
142 if (args[0] == "alias"):
144 print("Need address parameter")
146 path = adapter.FindDevice(args[1])
147 device = dbus.Interface(bus.get_object("org.bluez", path),
150 properties = device.GetProperties()
151 print(properties["Alias"])
153 device.SetProperty("Alias", args[2])
156 if (args[0] == "trusted"):
158 print("Need address parameter")
160 path = adapter.FindDevice(args[1])
161 device = dbus.Interface(bus.get_object("org.bluez", path),
164 properties = device.GetProperties()
165 print(properties["Trusted"])
167 if (args[2] == "yes"):
168 value = dbus.Boolean(1)
169 elif (args[2] == "no"):
170 value = dbus.Boolean(0)
172 value = dbus.Boolean(args[2])
173 device.SetProperty("Trusted", value)
176 if (args[0] == "blocked"):
178 print("Need address parameter")
180 path = adapter.FindDevice(args[1])
181 device = dbus.Interface(bus.get_object("org.bluez", path),
184 properties = device.GetProperties()
185 print(properties["Blocked"])
187 if (args[2] == "yes"):
188 value = dbus.Boolean(1)
189 elif (args[2] == "no"):
190 value = dbus.Boolean(0)
192 value = dbus.Boolean(args[2])
193 device.SetProperty("Blocked", value)
196 if (args[0] == "services"):
198 print("Need address parameter")
200 path = adapter.FindDevice(args[1])
201 device = dbus.Interface(bus.get_object("org.bluez", path),
203 properties = device.GetProperties()
204 for path in properties["Services"]:
208 print("Unknown command")