#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2013 Agustin Zubiaga # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import bluez from gi.repository import GObject SEARCH_TIME = 20 # seconds class Bluetooth(GObject.GObject): __gsignals__ = { 'device-found': (GObject.SignalFlags.RUN_FIRST, None, [object]), 'search-finished': (GObject.SignalFlags.RUN_FIRST, None, [])} def __init__(self): super(Bluetooth, self).__init__() manager = bluez.Manager('gobject') self._adapter = manager.DefaultAdapter() def device_found(address, properties): already_found = False for i in self.nearby_devices: if properties['Address'] == i['Address']: already_found = True if not already_found: self.nearby_devices.append(properties) self.emit('device-found', properties) self._adapter.HandleSignal(device_found, 'DeviceFound') self.nearby_devices = [] self._paired_devices = self._adapter.ListDevices() def get_paired_devices(self): return [i.GetProperties() for i in self._paired_devices] def find_devices(self): self._adapter.StartDiscovery() self.nearby_devices = [] def stop_search(self): self._adapter.StopDiscovery()