| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser, make_option |
| 7 |
import dbus |
| 8 |
import dbus.mainloop.glib |
| 9 |
try: |
| 10 |
from gi.repository import GObject |
| 11 |
except ImportError: |
| 12 |
import gobject as GObject |
| 13 |
import bluezutils |
| 14 |
|
| 15 |
compact = False |
| 16 |
devices = {} |
| 17 |
|
| 18 |
def print_compact(address, properties): |
| 19 |
name = "" |
| 20 |
address = "<unknown>" |
| 21 |
|
| 22 |
for key, value in properties.items(): |
| 23 |
if type(value) is dbus.String: |
| 24 |
value = unicode(value).encode('ascii', 'replace') |
| 25 |
if (key == "Name"): |
| 26 |
name = value |
| 27 |
elif (key == "Address"): |
| 28 |
address = value |
| 29 |
|
| 30 |
if "Logged" in properties: |
| 31 |
flag = "*" |
| 32 |
else: |
| 33 |
flag = " " |
| 34 |
|
| 35 |
print("%s%s %s" % (flag, address, name)) |
| 36 |
|
| 37 |
properties["Logged"] = True |
| 38 |
|
| 39 |
def print_normal(address, properties): |
| 40 |
print("[ " + address + " ]") |
| 41 |
|
| 42 |
for key in properties.keys(): |
| 43 |
value = properties[key] |
| 44 |
if type(value) is dbus.String: |
| 45 |
value = unicode(value).encode('ascii', 'replace') |
| 46 |
if (key == "Class"): |
| 47 |
print(" %s = 0x%06x" % (key, value)) |
| 48 |
else: |
| 49 |
print(" %s = %s" % (key, value)) |
| 50 |
|
| 51 |
print() |
| 52 |
|
| 53 |
properties["Logged"] = True |
| 54 |
|
| 55 |
def skip_dev(old_dev, new_dev): |
| 56 |
if not "Logged" in old_dev: |
| 57 |
return False |
| 58 |
if "Name" in old_dev: |
| 59 |
return True |
| 60 |
if not "Name" in new_dev: |
| 61 |
return True |
| 62 |
return False |
| 63 |
|
| 64 |
def interfaces_added(path, interfaces): |
| 65 |
properties = interfaces["org.bluez.Device1"] |
| 66 |
if not properties: |
| 67 |
return |
| 68 |
|
| 69 |
if path in devices: |
| 70 |
dev = devices[path] |
| 71 |
|
| 72 |
if compact and skip_dev(dev, properties): |
| 73 |
return |
| 74 |
devices[path] = dict(devices[path].items() + properties.items()) |
| 75 |
else: |
| 76 |
devices[path] = properties |
| 77 |
|
| 78 |
if "Address" in devices[path]: |
| 79 |
address = properties["Address"] |
| 80 |
else: |
| 81 |
address = "<unknown>" |
| 82 |
|
| 83 |
if compact: |
| 84 |
print_compact(address, devices[path]) |
| 85 |
else: |
| 86 |
print_normal(address, devices[path]) |
| 87 |
|
| 88 |
def properties_changed(interface, changed, invalidated, path): |
| 89 |
if interface != "org.bluez.Device1": |
| 90 |
return |
| 91 |
|
| 92 |
if path in devices: |
| 93 |
dev = devices[path] |
| 94 |
|
| 95 |
if compact and skip_dev(dev, changed): |
| 96 |
return |
| 97 |
devices[path] = dict(devices[path].items() + changed.items()) |
| 98 |
else: |
| 99 |
devices[path] = changed |
| 100 |
|
| 101 |
if "Address" in devices[path]: |
| 102 |
address = devices[path]["Address"] |
| 103 |
else: |
| 104 |
address = "<unknown>" |
| 105 |
|
| 106 |
if compact: |
| 107 |
print_compact(address, devices[path]) |
| 108 |
else: |
| 109 |
print_normal(address, devices[path]) |
| 110 |
|
| 111 |
if __name__ == '__main__': |
| 112 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 113 |
|
| 114 |
bus = dbus.SystemBus() |
| 115 |
|
| 116 |
option_list = [ |
| 117 |
make_option("-i", "--device", action="store", |
| 118 |
type="string", dest="dev_id"), |
| 119 |
make_option("-u", "--uuids", action="store", |
| 120 |
type="string", dest="uuids", |
| 121 |
help="Filtered service UUIDs [uuid1,uuid2,...]"), |
| 122 |
make_option("-r", "--rssi", action="store", |
| 123 |
type="int", dest="rssi", |
| 124 |
help="RSSI threshold value"), |
| 125 |
make_option("-p", "--pathloss", action="store", |
| 126 |
type="int", dest="pathloss", |
| 127 |
help="Pathloss threshold value"), |
| 128 |
make_option("-t", "--transport", action="store", |
| 129 |
type="string", dest="transport", |
| 130 |
help="Type of scan to run (le/bredr/auto)"), |
| 131 |
make_option("-c", "--compact", |
| 132 |
action="store_true", dest="compact"), |
| 133 |
] |
| 134 |
parser = OptionParser(option_list=option_list) |
| 135 |
|
| 136 |
(options, args) = parser.parse_args() |
| 137 |
|
| 138 |
adapter = bluezutils.find_adapter(options.dev_id) |
| 139 |
|
| 140 |
if options.compact: |
| 141 |
compact = True; |
| 142 |
|
| 143 |
bus.add_signal_receiver(interfaces_added, |
| 144 |
dbus_interface = "org.freedesktop.DBus.ObjectManager", |
| 145 |
signal_name = "InterfacesAdded") |
| 146 |
|
| 147 |
bus.add_signal_receiver(properties_changed, |
| 148 |
dbus_interface = "org.freedesktop.DBus.Properties", |
| 149 |
signal_name = "PropertiesChanged", |
| 150 |
arg0 = "org.bluez.Device1", |
| 151 |
path_keyword = "path") |
| 152 |
|
| 153 |
om = dbus.Interface(bus.get_object("org.bluez", "/"), |
| 154 |
"org.freedesktop.DBus.ObjectManager") |
| 155 |
objects = om.GetManagedObjects() |
| 156 |
for path, interfaces in objects.items(): |
| 157 |
if "org.bluez.Device1" in interfaces: |
| 158 |
devices[path] = interfaces["org.bluez.Device1"] |
| 159 |
|
| 160 |
scan_filter = dict() |
| 161 |
|
| 162 |
if options.uuids: |
| 163 |
uuids = [] |
| 164 |
uuid_list = options.uuids.split(',') |
| 165 |
for uuid in uuid_list: |
| 166 |
uuids.append(uuid) |
| 167 |
|
| 168 |
scan_filter.update({ "UUIDs": uuids }) |
| 169 |
|
| 170 |
if options.rssi: |
| 171 |
scan_filter.update({ "RSSI": dbus.Int16(options.rssi) }) |
| 172 |
|
| 173 |
if options.pathloss: |
| 174 |
scan_filter.update({ "Pathloss": dbus.UInt16(options.pathloss) }) |
| 175 |
|
| 176 |
if options.transport: |
| 177 |
scan_filter.update({ "Transport": options.transport }) |
| 178 |
|
| 179 |
adapter.SetDiscoveryFilter(scan_filter) |
| 180 |
adapter.StartDiscovery() |
| 181 |
|
| 182 |
mainloop = GObject.MainLoop() |
| 183 |
mainloop.run() |