| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
import dbus |
| 7 |
import dbus.mainloop.glib |
| 8 |
try: |
| 9 |
from gi.repository import GObject |
| 10 |
except ImportError: |
| 11 |
import gobject as GObject |
| 12 |
import bluezutils |
| 13 |
|
| 14 |
def interfaces_added(path, interfaces): |
| 15 |
if interfaces.get("org.bluez.Adapter1") != None: |
| 16 |
print("Adapter with path %s added" % (path)) |
| 17 |
|
| 18 |
def interfaces_removed(path, interfaces): |
| 19 |
if "org.bluez.Adapter1" in interfaces: |
| 20 |
print("Adapter with path %s removed" % (path)) |
| 21 |
|
| 22 |
if __name__ == "__main__": |
| 23 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 24 |
|
| 25 |
bus = dbus.SystemBus() |
| 26 |
|
| 27 |
bus.add_signal_receiver(interfaces_added, bus_name="org.bluez", |
| 28 |
dbus_interface="org.freedesktop.DBus.ObjectManager", |
| 29 |
signal_name="InterfacesAdded") |
| 30 |
|
| 31 |
bus.add_signal_receiver(interfaces_removed, bus_name="org.bluez", |
| 32 |
dbus_interface="org.freedesktop.DBus.ObjectManager", |
| 33 |
signal_name="InterfacesRemoved") |
| 34 |
|
| 35 |
try: |
| 36 |
path = bluezutils.find_adapter().object_path |
| 37 |
print("Adapter found at path %s" % (path)) |
| 38 |
except: |
| 39 |
print("No adapter found") |
| 40 |
|
| 41 |
mainloop = GObject.MainLoop() |
| 42 |
mainloop.run() |