| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser, make_option |
| 7 |
import sys |
| 8 |
import time |
| 9 |
import dbus |
| 10 |
import bluezutils |
| 11 |
|
| 12 |
bus = dbus.SystemBus() |
| 13 |
|
| 14 |
option_list = [ |
| 15 |
make_option("-i", "--device", action="store", |
| 16 |
type="string", dest="dev_id"), |
| 17 |
] |
| 18 |
parser = OptionParser(option_list=option_list) |
| 19 |
|
| 20 |
(options, args) = parser.parse_args() |
| 21 |
|
| 22 |
adapter_path = bluezutils.find_adapter(options.dev_id).object_path |
| 23 |
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), |
| 24 |
"org.freedesktop.DBus.Properties") |
| 25 |
|
| 26 |
if (len(args) < 1): |
| 27 |
print("Usage: %s <command>" % (sys.argv[0])) |
| 28 |
print("") |
| 29 |
print(" address") |
| 30 |
print(" list") |
| 31 |
print(" name") |
| 32 |
print(" alias [alias]") |
| 33 |
print(" powered [on/off]") |
| 34 |
print(" pairable [on/off]") |
| 35 |
print(" pairabletimeout [timeout]") |
| 36 |
print(" discoverable [on/off]") |
| 37 |
print(" discoverabletimeout [timeout]") |
| 38 |
print(" discovering") |
| 39 |
sys.exit(1) |
| 40 |
|
| 41 |
if (args[0] == "address"): |
| 42 |
addr = adapter.Get("org.bluez.Adapter1", "Address") |
| 43 |
print(addr) |
| 44 |
sys.exit(0) |
| 45 |
|
| 46 |
if (args[0] == "name"): |
| 47 |
name = adapter.Get("org.bluez.Adapter1", "Name") |
| 48 |
print(name) |
| 49 |
sys.exit(0) |
| 50 |
|
| 51 |
if (args[0] == "alias"): |
| 52 |
if (len(args) < 2): |
| 53 |
alias = adapter.Get("org.bluez.Adapter1", "Alias") |
| 54 |
print(alias) |
| 55 |
else: |
| 56 |
adapter.Set("org.bluez.Adapter1", "Alias", args[1]) |
| 57 |
sys.exit(0) |
| 58 |
|
| 59 |
if (args[0] == "list"): |
| 60 |
if (len(args) < 2): |
| 61 |
om = dbus.Interface(bus.get_object("org.bluez", "/"), |
| 62 |
"org.freedesktop.DBus.ObjectManager") |
| 63 |
objects = om.GetManagedObjects() |
| 64 |
for path, interfaces in objects.items(): |
| 65 |
if "org.bluez.Adapter1" not in interfaces: |
| 66 |
continue |
| 67 |
|
| 68 |
print(" [ %s ]" % (path)) |
| 69 |
|
| 70 |
props = interfaces["org.bluez.Adapter1"] |
| 71 |
|
| 72 |
for (key, value) in props.items(): |
| 73 |
if (key == "Class"): |
| 74 |
print(" %s = 0x%06x" % (key, value)) |
| 75 |
else: |
| 76 |
print(" %s = %s" % (key, value)) |
| 77 |
print() |
| 78 |
sys.exit(0) |
| 79 |
|
| 80 |
if (args[0] == "powered"): |
| 81 |
if (len(args) < 2): |
| 82 |
powered = adapter.Get("org.bluez.Adapter1", "Powered") |
| 83 |
print(powered) |
| 84 |
else: |
| 85 |
if (args[1] == "on"): |
| 86 |
value = dbus.Boolean(1) |
| 87 |
elif (args[1] == "off"): |
| 88 |
value = dbus.Boolean(0) |
| 89 |
else: |
| 90 |
value = dbus.Boolean(args[1]) |
| 91 |
adapter.Set("org.bluez.Adapter1", "Powered", value) |
| 92 |
sys.exit(0) |
| 93 |
|
| 94 |
if (args[0] == "pairable"): |
| 95 |
if (len(args) < 2): |
| 96 |
pairable = adapter.Get("org.bluez.Adapter1", "Pairable") |
| 97 |
print(pairable) |
| 98 |
else: |
| 99 |
if (args[1] == "on"): |
| 100 |
value = dbus.Boolean(1) |
| 101 |
elif (args[1] == "off"): |
| 102 |
value = dbus.Boolean(0) |
| 103 |
else: |
| 104 |
value = dbus.Boolean(args[1]) |
| 105 |
adapter.Set("org.bluez.Adapter1", "Pairable", value) |
| 106 |
sys.exit(0) |
| 107 |
|
| 108 |
if (args[0] == "pairabletimeout"): |
| 109 |
if (len(args) < 2): |
| 110 |
pt = adapter.Get("org.bluez.Adapter1", "PairableTimeout") |
| 111 |
print(pt) |
| 112 |
else: |
| 113 |
timeout = dbus.UInt32(args[1]) |
| 114 |
adapter.Set("org.bluez.Adapter1", "PairableTimeout", timeout) |
| 115 |
sys.exit(0) |
| 116 |
|
| 117 |
if (args[0] == "discoverable"): |
| 118 |
if (len(args) < 2): |
| 119 |
discoverable = adapter.Get("org.bluez.Adapter1", "Discoverable") |
| 120 |
print(discoverable) |
| 121 |
else: |
| 122 |
if (args[1] == "on"): |
| 123 |
value = dbus.Boolean(1) |
| 124 |
elif (args[1] == "off"): |
| 125 |
value = dbus.Boolean(0) |
| 126 |
else: |
| 127 |
value = dbus.Boolean(args[1]) |
| 128 |
adapter.Set("org.bluez.Adapter1", "Discoverable", value) |
| 129 |
sys.exit(0) |
| 130 |
|
| 131 |
if (args[0] == "discoverabletimeout"): |
| 132 |
if (len(args) < 2): |
| 133 |
dt = adapter.Get("org.bluez.Adapter1", "DiscoverableTimeout") |
| 134 |
print(dt) |
| 135 |
else: |
| 136 |
to = dbus.UInt32(args[1]) |
| 137 |
adapter.Set("org.bluez.Adapter1", "DiscoverableTimeout", to) |
| 138 |
sys.exit(0) |
| 139 |
|
| 140 |
if (args[0] == "discovering"): |
| 141 |
discovering = adapter.Get("org.bluez.Adapter1", "Discovering") |
| 142 |
print(discovering) |
| 143 |
sys.exit(0) |
| 144 |
|
| 145 |
print("Unknown command") |
| 146 |
sys.exit(1) |