Skip to content

Commit

Permalink
Add initial Bluetooth discovery script (issue #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Jun 22, 2020
1 parent 8ed7e28 commit 7099651
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions openbose/scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import gi

from gi.repository import GLib

import dbus
import dbus.mainloop.glib


dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()


BLUEZ_BUS = "org.bluez"
OBJECT_MANAGER_INTERFACE = "org.freedesktop.DBus.ObjectManager"
PROPERTIES_INTERFACE = "org.freedesktop.DBus.Properties"
DEVICE_INTERFACE = "org.bluez.Device1"
ADAPTER_INTERFACE = "org.bluez.Adapter1"


def properties_changed(interface, properties, removed_properties, object_path):
if interface == DEVICE_INTERFACE:
print(object_path, properties.get("ManufacturerData"))


def interfaces_added(object_path, interfaces):
if DEVICE_INTERFACE in interfaces:
device_properties = interfaces[DEVICE_INTERFACE]
print(object_path, device_properties.get("ManufacturerData"))


def property_changed(name, value):
if name == "Discovering" and not value:
main_loop.quit()


system_bus.add_signal_receiver(properties_changed, "PropertiesChanged", PROPERTIES_INTERFACE, BLUEZ_BUS, arg0=DEVICE_INTERFACE, path_keyword="object_path")
system_bus.add_signal_receiver(interfaces_added, "InterfacesAdded", OBJECT_MANAGER_INTERFACE, BLUEZ_BUS)

system_bus.add_signal_receiver(property_changed, "PropertyChanged", ADAPTER_INTERFACE)

adapter = dbus.Interface(system_bus.get_object(BLUEZ_BUS, "/org/bluez/hci0"), ADAPTER_INTERFACE)
adapter.StartDiscovery()

main_loop = GLib.MainLoop()
main_loop.run()

0 comments on commit 7099651

Please sign in to comment.