Skip to content

Commit

Permalink
add new logic for scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Aleksandrov committed Sep 1, 2016
1 parent 2566b0c commit a43aefd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
81 changes: 40 additions & 41 deletions bluetool/bluetool.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from gi.repository import GObject
except ImportError:
import gobject as GObject
import threading
import bluezutils

class BluetoothError(Exception):
Expand All @@ -50,58 +51,48 @@ def __init__(self):
subprocess.check_output("rfkill unblock bluetooth", shell=True)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self.__bus = dbus.SystemBus()
self.mainloop = GObject.MainLoop()
self.scan_thread = None
self.timer = None

def get_devices_to_pair(self, timeout=10):
devices = self.scan(timeout)
def start_scanning(self, timeout=10):
self.scan_thread = threading.Thread(target=self.scan)
self.scan_thread.start()

try:
for key in self.get_devices("Paired").keys():
devices.pop(key)
except BluetoothError as error:
print error
return {}

return devices

def scan(self, timeout=10):
devices = {}
self.timer = threading.Timer(timeout, self.stop_scanning)
self.timer.start()

def scan(self):
try:
adapter = bluezutils.find_adapter()
except Exception as error:
print error
else:
try:
adapter.StartDiscovery()

mainloop = GObject.MainLoop()
GObject.timeout_add(timeout*1000, mainloop.quit)
mainloop.run()

self.mainloop.run()
adapter.StopDiscovery()

man = dbus.Interface(self.__bus.get_object("org.bluez", "/"),
"org.freedesktop.DBus.ObjectManager")
objects = man.GetManagedObjects()

for path, interfaces in objects.iteritems():
if "org.bluez.Device1" in interfaces:
dev = interfaces["org.bluez.Device1"]

if "Address" not in dev:
continue
if "Name" not in dev:
dev["Name"] = "<unknown>"

devices[str(dev["Name"])] = str(dev["Address"])

except dbus.exceptions.DBusException as error:
print error

def stop_scanning(self):
self.mainloop.quit()
self.scan_thread.join()

def get_devices_to_pair(self):
devices = self.get_devices("Scanned")

try:
for key in self.get_devices("Paired").keys():
devices.pop(key)
except BluetoothError as error:
print error
return {}

return devices

def get_devices(self, condition):
conditions = ["Paired", "Connected"]
conditions = ["Scanned", "Paired", "Connected"]

if condition not in conditions:
raise BluetoothError("get_devices: unknown condition - {}".\
Expand All @@ -118,17 +109,25 @@ def get_devices(self, condition):
if "org.bluez.Device1" in interfaces:
dev = interfaces["org.bluez.Device1"]

props = dbus.Interface(self.__bus.get_object("org.bluez",
path),
"org.freedesktop.DBus.Properties")

if props.Get("org.bluez.Device1", condition):
if condition == "Scanned":
if "Address" not in dev:
continue
if "Name" not in dev:
dev["Name"] = "<unknown>"

devices[str(dev["Name"])] = str(dev["Address"])
else:
props = dbus.Interface(self.__bus.get_object("org.bluez",
path),
"org.freedesktop.DBus.Properties")

if props.Get("org.bluez.Device1", condition):
if "Address" not in dev:
continue
if "Name" not in dev:
dev["Name"] = "<unknown>"

devices[str(dev["Name"])] = str(dev["Address"])

except dbus.exceptions.DBusException as error:
print error
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='bluetool',
version='0.1.1',
version='0.1.2',
license='BSD',
author='Aleksandr Aleksandrov',
author_email='[email protected]',
Expand Down

0 comments on commit a43aefd

Please sign in to comment.