Send out the PairableDevice signal when we have a device
This commit is contained in:
parent
c073ff2f06
commit
aa1820e21c
9
tuhi.py
9
tuhi.py
|
@ -142,6 +142,7 @@ class Tuhi(GObject.Object):
|
|||
self.server.connect('pairing-stop-requested', self._on_stop_pairing_requested)
|
||||
self.bluez = BlueZDeviceManager()
|
||||
self.bluez.connect('device-added', self._on_bluez_device_added)
|
||||
self.bluez.connect('device-updated', self._on_bluez_device_updated)
|
||||
self.bluez.connect('discovery-started', self._on_bluez_discovery_started)
|
||||
self.bluez.connect('discovery-stopped', self._on_bluez_discovery_stopped)
|
||||
|
||||
|
@ -162,6 +163,7 @@ class Tuhi(GObject.Object):
|
|||
self._pairing_stop_handler(0)
|
||||
self._pairing_stop_handler = None
|
||||
self.bluez.stop_discovery()
|
||||
self._pairable_device_handler = None
|
||||
|
||||
@classmethod
|
||||
def _is_pairing_device(cls, bluez_device):
|
||||
|
@ -192,6 +194,13 @@ class Tuhi(GObject.Object):
|
|||
if self._pairing_stop_handler is not None:
|
||||
self._pairing_stop_handler(0)
|
||||
|
||||
def _on_bluez_device_updated(self, manager, bluez_device):
|
||||
if bluez_device.vendor_id != WACOM_COMPANY_ID:
|
||||
return
|
||||
|
||||
if Tuhi._is_pairing_device(bluez_device):
|
||||
self.server.notify_pairable_device(bluez_device)
|
||||
|
||||
|
||||
def main(args):
|
||||
desc = "Daemon to extract the pen stroke data from Wacom SmartPad devices"
|
||||
|
|
14
tuhi/ble.py
14
tuhi/ble.py
|
@ -95,6 +95,8 @@ class BlueZDevice(GObject.Object):
|
|||
(GObject.SIGNAL_RUN_FIRST, None, ()),
|
||||
"disconnected":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, ()),
|
||||
"updated":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, ()),
|
||||
}
|
||||
|
||||
def __init__(self, om, obj):
|
||||
|
@ -237,6 +239,8 @@ class BlueZDevice(GObject.Object):
|
|||
elif 'ServicesResolved' in properties:
|
||||
if properties['ServicesResolved']:
|
||||
self.emit('connected')
|
||||
elif 'RSSI' in properties:
|
||||
self.emit('updated')
|
||||
|
||||
def connect_gatt_value(self, uuid, callback):
|
||||
"""
|
||||
|
@ -262,6 +266,8 @@ class BlueZDeviceManager(GObject.Object):
|
|||
__gsignals__ = {
|
||||
"device-added":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
|
||||
"device-updated":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, (GObject.TYPE_PYOBJECT,)),
|
||||
"discovery-started":
|
||||
(GObject.SIGNAL_RUN_FIRST, None, ()),
|
||||
"discovery-stopped":
|
||||
|
@ -343,6 +349,12 @@ class BlueZDeviceManager(GObject.Object):
|
|||
|
||||
self.emit("discovery-stopped")
|
||||
|
||||
def _on_device_updated(self, device):
|
||||
"""Callback for Device's properties-changed"""
|
||||
logger.debug('Object updated: {}'.format(device.name))
|
||||
|
||||
self.emit("device-updated", device)
|
||||
|
||||
def _on_om_object_added(self, om, obj):
|
||||
"""Callback for ObjectManager's object-added"""
|
||||
objpath = obj.get_object_path()
|
||||
|
@ -376,11 +388,11 @@ class BlueZDeviceManager(GObject.Object):
|
|||
def _process_adapter(self, obj):
|
||||
objpath = obj.get_object_path()
|
||||
logger.debug('Adapter: {}'.format(objpath))
|
||||
# FIXME: call StartDiscovery if we want to pair
|
||||
|
||||
def _process_device(self, obj):
|
||||
dev = BlueZDevice(self._om, obj)
|
||||
self.devices.append(dev)
|
||||
dev.connect("updated", self._on_device_updated)
|
||||
self.emit("device-added", dev)
|
||||
|
||||
def _process_characteristic(self, obj):
|
||||
|
|
|
@ -36,6 +36,10 @@ INTROSPECTION_XML = """
|
|||
<arg name='status' type='i' />
|
||||
</signal>
|
||||
|
||||
<signal name='PairableDevice'>
|
||||
<arg name='info' type='a{sv}' />
|
||||
</signal>
|
||||
|
||||
</interface>
|
||||
|
||||
<interface name='org.freedesktop.tuhi1.Device'>
|
||||
|
@ -158,6 +162,7 @@ class TuhiDBusServer(GObject.Object):
|
|||
def __init__(self):
|
||||
GObject.Object.__init__(self)
|
||||
self._devices = []
|
||||
self._pairable_devices = {}
|
||||
self._dbus = Gio.bus_own_name(Gio.BusType.SESSION,
|
||||
BUS_NAME,
|
||||
Gio.BusNameOwnerFlags.NONE,
|
||||
|
@ -226,10 +231,44 @@ class TuhiDBusServer(GObject.Object):
|
|||
Called by whoever handles the pairing-start-requested signal
|
||||
"""
|
||||
logger.debug("Pairing has stopped")
|
||||
self._is_pairing = False
|
||||
status = GLib.Variant.new_int32(status)
|
||||
status = GLib.Variant.new_tuple(status)
|
||||
self._connection.emit_signal(None, BASE_PATH, INTF_MANAGER,
|
||||
"PairingStopped", status)
|
||||
self._pairable_devices = {}
|
||||
|
||||
def notify_pairable_device(self, device):
|
||||
"""
|
||||
Notify the client that a pairable device is available.
|
||||
"""
|
||||
if not self._is_pairing:
|
||||
return
|
||||
|
||||
logger.debug("Pairable device: {}".format(device))
|
||||
|
||||
address = device.address
|
||||
if address in self._pairable_devices:
|
||||
return
|
||||
|
||||
self._pairable_devices[address] = device
|
||||
|
||||
b = GLib.VariantBuilder(GLib.VariantType.new('a{sv}'))
|
||||
|
||||
key = GLib.Variant.new_string('name')
|
||||
value = GLib.Variant.new_variant(GLib.Variant.new_string(device.name))
|
||||
de = GLib.Variant.new_dict_entry(key, value)
|
||||
b.add_value(de)
|
||||
|
||||
key = GLib.Variant.new_string('address')
|
||||
value = GLib.Variant.new_variant(GLib.Variant.new_string(device.address))
|
||||
de = GLib.Variant.new_dict_entry(key, value)
|
||||
b.add_value(de)
|
||||
|
||||
array = b.end()
|
||||
self._connection.emit_signal(None, BASE_PATH, INTF_MANAGER,
|
||||
"PairableDevice",
|
||||
GLib.Variant.new_tuple(array))
|
||||
|
||||
def cleanup(self):
|
||||
Gio.bus_unown_name(self._dbus)
|
||||
|
|
Loading…
Reference in New Issue