3
from optparse import OptionParser, make_option
7
import dbus.mainloop.glib
9
from gi.repository import GObject
11
import gobject as GObject
14
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
15
bus = dbus.SystemBus()
16
mainloop = GObject.MainLoop()
19
make_option("-i", "--device", action="store",
20
type="string", dest="dev_id"),
22
parser = OptionParser(option_list=option_list)
24
(options, args) = parser.parse_args()
27
print("Usage: %s <command>" % (sys.argv[0]))
30
print(" create <address>")
31
print(" remove <address|path>")
32
print(" connect <address> [profile]")
33
print(" disconnect <address> [profile]")
34
print(" class <address>")
35
print(" name <address>")
36
print(" alias <address> [alias]")
37
print(" trusted <address> [yes/no]")
38
print(" blocked <address> [yes/no]")
41
if (args[0] == "list"):
42
adapter = bluezutils.find_adapter(options.dev_id)
43
adapter_path = adapter.object_path
45
om = dbus.Interface(bus.get_object("org.bluez", "/"),
46
"org.freedesktop.DBus.ObjectManager")
47
objects = om.GetManagedObjects()
49
for path, interfaces in objects.items():
50
if "org.bluez.Device1" not in interfaces:
52
properties = interfaces["org.bluez.Device1"]
53
if properties["Adapter"] != adapter_path:
55
print("%s %s" % (properties["Address"], properties["Alias"]))
59
def create_device_reply(device):
60
print("New device (%s)" % device)
64
def create_device_error(error):
65
print("Creating device failed: %s" % error)
69
if (args[0] == "create"):
71
print("Need address parameter")
73
adapter = bluezutils.find_adapter(options.dev_id)
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")
83
managed_objects = bluezutils.get_managed_objects()
84
adapter = bluezutils.find_adapter_in_objects(managed_objects,
87
dev = bluezutils.find_device_in_objects(managed_objects,
90
path = dev.object_path
93
adapter.RemoveDevice(path)
96
if (args[0] == "connect"):
98
print("Need address parameter")
100
device = bluezutils.find_device(args[1], options.dev_id)
102
device.ConnectProfile(args[2])
107
if (args[0] == "disconnect"):
109
print("Need address parameter")
111
device = bluezutils.find_device(args[1], options.dev_id)
113
device.DisconnectProfile(args[2])
118
if (args[0] == "class"):
120
print("Need address parameter")
122
device = bluezutils.find_device(args[1], options.dev_id)
123
path = device.object_path
124
props = dbus.Interface(bus.get_object("org.bluez", path),
125
"org.freedesktop.DBus.Properties")
126
cls = props.Get("org.bluez.Device1", "Class")
127
print("0x%06x" % cls)
130
if (args[0] == "name"):
132
print("Need address parameter")
134
device = bluezutils.find_device(args[1], options.dev_id)
135
path = device.object_path
136
props = dbus.Interface(bus.get_object("org.bluez", path),
137
"org.freedesktop.DBus.Properties")
138
name = props.Get("org.bluez.Device1", "Name")
142
if (args[0] == "alias"):
144
print("Need address parameter")
146
device = bluezutils.find_device(args[1], options.dev_id)
147
path = device.object_path
148
props = dbus.Interface(bus.get_object("org.bluez", path),
149
"org.freedesktop.DBus.Properties")
151
alias = props.Get("org.bluez.Device1", "Alias")
154
props.Set("org.bluez.Device1", "Alias", args[2])
157
if (args[0] == "trusted"):
159
print("Need address parameter")
161
device = bluezutils.find_device(args[1], options.dev_id)
162
path = device.object_path
163
props = dbus.Interface(bus.get_object("org.bluez", path),
164
"org.freedesktop.DBus.Properties")
166
trusted = props.Get("org.bluez.Device1", "Trusted")
169
if (args[2] == "yes"):
170
value = dbus.Boolean(1)
171
elif (args[2] == "no"):
172
value = dbus.Boolean(0)
174
value = dbus.Boolean(args[2])
175
props.Set("org.bluez.Device1", "Trusted", value)
178
if (args[0] == "blocked"):
180
print("Need address parameter")
182
device = bluezutils.find_device(args[1], options.dev_id)
183
path = device.object_path
184
props = dbus.Interface(bus.get_object("org.bluez", path),
185
"org.freedesktop.DBus.Properties")
187
blocked = props.Get("org.bluez.Device1", "Blocked")
190
if (args[2] == "yes"):
191
value = dbus.Boolean(1)
192
elif (args[2] == "no"):
193
value = dbus.Boolean(0)
195
value = dbus.Boolean(args[2])
196
props.Set("org.bluez.Device1", "Blocked", value)
199
print("Unknown command")