From d8817cebf11c9212f85a5359262d321f3c205c02 Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Fri, 10 Feb 2017 17:26:01 +0100 Subject: Match connected device changes based on local device instance A new device connection is only ever for one particular device. Prior to this patch all local device instances (for different adapters) assumed the new connection request would be theirs. Task-number: QTBUG-57417 Change-Id: I0e7ced15cb83df4321284a297ac42b8786f9314b Reviewed-by: Christian Kandeler Reviewed-by: Oliver Wolff Reviewed-by: Alex Blasche --- src/bluetooth/qbluetoothlocaldevice_bluez.cpp | 61 +++++++++++++++++---------- src/bluetooth/qbluetoothlocaldevice_p.h | 1 + 2 files changed, 39 insertions(+), 23 deletions(-) (limited to 'src/bluetooth') diff --git a/src/bluetooth/qbluetoothlocaldevice_bluez.cpp b/src/bluetooth/qbluetoothlocaldevice_bluez.cpp index a439a04e..8da29a36 100644 --- a/src/bluetooth/qbluetoothlocaldevice_bluez.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_bluez.cpp @@ -672,6 +672,11 @@ QBluetoothLocalDevicePrivate::QBluetoothLocalDevicePrivate(QBluetoothLocalDevice connectDeviceChanges(); } +bool objectPathIsForThisDevice(const QString &adapterPath, const QString &objectPath) +{ + return (!adapterPath.isEmpty() && objectPath.startsWith(adapterPath)); +} + void QBluetoothLocalDevicePrivate::connectDeviceChanges() { if (adapter) { // invalid QBluetoothLocalDevice due to wrong local adapter address @@ -696,6 +701,10 @@ void QBluetoothLocalDevicePrivate::connectDeviceChanges() const QDBusObjectPath &path = it.key(); const InterfaceList &ifaceList = it.value(); + // don't track connected devices from other adapters but the current + if (!objectPathIsForThisDevice(deviceAdapterPath, path.path())) + continue; + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { const QString &iface = jt.key(); const QVariantMap &ifaceValues = jt.value(); @@ -817,6 +826,7 @@ void QBluetoothLocalDevicePrivate::initializeAdapterBluez5() if (!ok || adapterPath.isEmpty()) return; + deviceAdapterPath = adapterPath; adapterBluez5 = new OrgBluezAdapter1Interface(QStringLiteral("org.bluez"), adapterPath, QDBusConnection::systemBus(), this); @@ -901,19 +911,22 @@ void QBluetoothLocalDevicePrivate::InterfacesAdded(const QDBusObjectPath &object if (interfaces_and_properties.contains(QStringLiteral("org.bluez.Device1")) && !deviceChangeMonitors.contains(object_path.path())) { // a new device was added which we need to add to list of known devices - OrgFreedesktopDBusPropertiesInterface *monitor = new OrgFreedesktopDBusPropertiesInterface( - QStringLiteral("org.bluez"), - object_path.path(), - QDBusConnection::systemBus()); - connect(monitor, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList)), - SLOT(PropertiesChanged(QString,QVariantMap,QStringList))); - deviceChangeMonitors.insert(object_path.path(), monitor); - const QVariantMap ifaceValues = interfaces_and_properties.value(QStringLiteral("org.bluez.Device1")); - if (ifaceValues.value(QStringLiteral("Connected"), false).toBool()) { - QBluetoothAddress address(ifaceValues.value(QStringLiteral("Address")).toString()); - connectedDevicesSet.insert(address); - emit q_ptr->deviceConnected(address); + if (objectPathIsForThisDevice(deviceAdapterPath, object_path.path())) { + OrgFreedesktopDBusPropertiesInterface *monitor = new OrgFreedesktopDBusPropertiesInterface( + QStringLiteral("org.bluez"), + object_path.path(), + QDBusConnection::systemBus()); + connect(monitor, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList)), + SLOT(PropertiesChanged(QString,QVariantMap,QStringList))); + deviceChangeMonitors.insert(object_path.path(), monitor); + + const QVariantMap ifaceValues = interfaces_and_properties.value(QStringLiteral("org.bluez.Device1")); + if (ifaceValues.value(QStringLiteral("Connected"), false).toBool()) { + QBluetoothAddress address(ifaceValues.value(QStringLiteral("Address")).toString()); + connectedDevicesSet.insert(address); + emit q_ptr->deviceConnected(address); + } } } @@ -933,17 +946,19 @@ void QBluetoothLocalDevicePrivate::InterfacesRemoved(const QDBusObjectPath &obje if (deviceChangeMonitors.contains(object_path.path()) && interfaces.contains(QLatin1String("org.bluez.Device1"))) { - //a device was removed - delete deviceChangeMonitors.take(object_path.path()); - - //the path contains the address (e.g.: /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX) - //-> use it to update current list of connected devices - QString addressString = object_path.path().right(17); - addressString.replace(QStringLiteral("_"), QStringLiteral(":")); - const QBluetoothAddress address(addressString); - bool found = connectedDevicesSet.remove(address); - if (found) - emit q_ptr->deviceDisconnected(address); + if (objectPathIsForThisDevice(deviceAdapterPath, object_path.path())) { + //a device was removed + delete deviceChangeMonitors.take(object_path.path()); + + //the path contains the address (e.g.: /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX) + //-> use it to update current list of connected devices + QString addressString = object_path.path().right(17); + addressString.replace(QStringLiteral("_"), QStringLiteral(":")); + const QBluetoothAddress address(addressString); + bool found = connectedDevicesSet.remove(address); + if (found) + emit q_ptr->deviceDisconnected(address); + } } if (adapterBluez5 diff --git a/src/bluetooth/qbluetoothlocaldevice_p.h b/src/bluetooth/qbluetoothlocaldevice_p.h index 1848a073..eecc0d1c 100644 --- a/src/bluetooth/qbluetoothlocaldevice_p.h +++ b/src/bluetooth/qbluetoothlocaldevice_p.h @@ -200,6 +200,7 @@ private: QDBusMessage msgConfirmation; QDBusConnection *msgConnection; + QString deviceAdapterPath; QBluetoothLocalDevice *q_ptr; -- cgit v1.2.3