| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser, make_option |
| 7 |
import re |
| 8 |
import sys |
| 9 |
import dbus |
| 10 |
import dbus.mainloop.glib |
| 11 |
try: |
| 12 |
from gi.repository import GObject |
| 13 |
except ImportError: |
| 14 |
import gobject as GObject |
| 15 |
import bluezutils |
| 16 |
|
| 17 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 18 |
bus = dbus.SystemBus() |
| 19 |
mainloop = GObject.MainLoop() |
| 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 (len(args) < 1): |
| 30 |
print("Usage: %s <command>" % (sys.argv[0])) |
| 31 |
print("") |
| 32 |
print(" list") |
| 33 |
print(" create <address>") |
| 34 |
print(" remove <address|path>") |
| 35 |
print(" connect <address> [profile]") |
| 36 |
print(" disconnect <address> [profile]") |
| 37 |
print(" class <address>") |
| 38 |
print(" name <address>") |
| 39 |
print(" alias <address> [alias]") |
| 40 |
print(" trusted <address> [yes/no]") |
| 41 |
print(" blocked <address> [yes/no]") |
| 42 |
sys.exit(1) |
| 43 |
|
| 44 |
if (args[0] == "list"): |
| 45 |
adapter = bluezutils.find_adapter(options.dev_id) |
| 46 |
adapter_path = adapter.object_path |
| 47 |
|
| 48 |
om = dbus.Interface(bus.get_object("org.bluez", "/"), |
| 49 |
"org.freedesktop.DBus.ObjectManager") |
| 50 |
objects = om.GetManagedObjects() |
| 51 |
|
| 52 |
for path, interfaces in objects.items(): |
| 53 |
if "org.bluez.Device1" not in interfaces: |
| 54 |
continue |
| 55 |
properties = interfaces["org.bluez.Device1"] |
| 56 |
if properties["Adapter"] != adapter_path: |
| 57 |
continue; |
| 58 |
print("%s %s" % (properties["Address"], properties["Alias"])) |
| 59 |
|
| 60 |
sys.exit(0) |
| 61 |
|
| 62 |
def create_device_reply(device): |
| 63 |
print("New device (%s)" % device) |
| 64 |
mainloop.quit() |
| 65 |
sys.exit(0) |
| 66 |
|
| 67 |
def create_device_error(error): |
| 68 |
print("Creating device failed: %s" % error) |
| 69 |
mainloop.quit() |
| 70 |
sys.exit(1) |
| 71 |
|
| 72 |
if (args[0] == "create"): |
| 73 |
if (len(args) < 2): |
| 74 |
print("Need address parameter") |
| 75 |
else: |
| 76 |
adapter = bluezutils.find_adapter(options.dev_id) |
| 77 |
adapter.CreateDevice(args[1], |
| 78 |
reply_handler=create_device_reply, |
| 79 |
error_handler=create_device_error) |
| 80 |
mainloop.run() |
| 81 |
|
| 82 |
if (args[0] == "remove"): |
| 83 |
if (len(args) < 2): |
| 84 |
print("Need address or object path parameter") |
| 85 |
else: |
| 86 |
managed_objects = bluezutils.get_managed_objects() |
| 87 |
adapter = bluezutils.find_adapter_in_objects(managed_objects, |
| 88 |
options.dev_id) |
| 89 |
try: |
| 90 |
dev = bluezutils.find_device_in_objects(managed_objects, |
| 91 |
args[1], |
| 92 |
options.dev_id) |
| 93 |
path = dev.object_path |
| 94 |
except: |
| 95 |
path = args[1] |
| 96 |
adapter.RemoveDevice(path) |
| 97 |
sys.exit(0) |
| 98 |
|
| 99 |
if (args[0] == "connect"): |
| 100 |
if (len(args) < 2): |
| 101 |
print("Need address parameter") |
| 102 |
else: |
| 103 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 104 |
if (len(args) > 2): |
| 105 |
device.ConnectProfile(args[2]) |
| 106 |
else: |
| 107 |
device.Connect() |
| 108 |
sys.exit(0) |
| 109 |
|
| 110 |
if (args[0] == "disconnect"): |
| 111 |
if (len(args) < 2): |
| 112 |
print("Need address parameter") |
| 113 |
else: |
| 114 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 115 |
if (len(args) > 2): |
| 116 |
device.DisconnectProfile(args[2]) |
| 117 |
else: |
| 118 |
device.Disconnect() |
| 119 |
sys.exit(0) |
| 120 |
|
| 121 |
if (args[0] == "class"): |
| 122 |
if (len(args) < 2): |
| 123 |
print("Need address parameter") |
| 124 |
else: |
| 125 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 126 |
path = device.object_path |
| 127 |
props = dbus.Interface(bus.get_object("org.bluez", path), |
| 128 |
"org.freedesktop.DBus.Properties") |
| 129 |
cls = props.Get("org.bluez.Device1", "Class") |
| 130 |
print("0x%06x" % cls) |
| 131 |
sys.exit(0) |
| 132 |
|
| 133 |
if (args[0] == "name"): |
| 134 |
if (len(args) < 2): |
| 135 |
print("Need address parameter") |
| 136 |
else: |
| 137 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 138 |
path = device.object_path |
| 139 |
props = dbus.Interface(bus.get_object("org.bluez", path), |
| 140 |
"org.freedesktop.DBus.Properties") |
| 141 |
name = props.Get("org.bluez.Device1", "Name") |
| 142 |
print(name) |
| 143 |
sys.exit(0) |
| 144 |
|
| 145 |
if (args[0] == "alias"): |
| 146 |
if (len(args) < 2): |
| 147 |
print("Need address parameter") |
| 148 |
else: |
| 149 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 150 |
path = device.object_path |
| 151 |
props = dbus.Interface(bus.get_object("org.bluez", path), |
| 152 |
"org.freedesktop.DBus.Properties") |
| 153 |
if (len(args) < 3): |
| 154 |
alias = props.Get("org.bluez.Device1", "Alias") |
| 155 |
print(alias) |
| 156 |
else: |
| 157 |
props.Set("org.bluez.Device1", "Alias", args[2]) |
| 158 |
sys.exit(0) |
| 159 |
|
| 160 |
if (args[0] == "trusted"): |
| 161 |
if (len(args) < 2): |
| 162 |
print("Need address parameter") |
| 163 |
else: |
| 164 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 165 |
path = device.object_path |
| 166 |
props = dbus.Interface(bus.get_object("org.bluez", path), |
| 167 |
"org.freedesktop.DBus.Properties") |
| 168 |
if (len(args) < 3): |
| 169 |
trusted = props.Get("org.bluez.Device1", "Trusted") |
| 170 |
print(trusted) |
| 171 |
else: |
| 172 |
if (args[2] == "yes"): |
| 173 |
value = dbus.Boolean(1) |
| 174 |
elif (args[2] == "no"): |
| 175 |
value = dbus.Boolean(0) |
| 176 |
else: |
| 177 |
value = dbus.Boolean(args[2]) |
| 178 |
props.Set("org.bluez.Device1", "Trusted", value) |
| 179 |
sys.exit(0) |
| 180 |
|
| 181 |
if (args[0] == "blocked"): |
| 182 |
if (len(args) < 2): |
| 183 |
print("Need address parameter") |
| 184 |
else: |
| 185 |
device = bluezutils.find_device(args[1], options.dev_id) |
| 186 |
path = device.object_path |
| 187 |
props = dbus.Interface(bus.get_object("org.bluez", path), |
| 188 |
"org.freedesktop.DBus.Properties") |
| 189 |
if (len(args) < 3): |
| 190 |
blocked = props.Get("org.bluez.Device1", "Blocked") |
| 191 |
print(blocked) |
| 192 |
else: |
| 193 |
if (args[2] == "yes"): |
| 194 |
value = dbus.Boolean(1) |
| 195 |
elif (args[2] == "no"): |
| 196 |
value = dbus.Boolean(0) |
| 197 |
else: |
| 198 |
value = dbus.Boolean(args[2]) |
| 199 |
props.Set("org.bluez.Device1", "Blocked", value) |
| 200 |
sys.exit(0) |
| 201 |
|
| 202 |
print("Unknown command") |
| 203 |
sys.exit(1) |