diff options
Diffstat (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp index 5c70f1c1..3cc3354a 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_bluez.cpp @@ -125,7 +125,7 @@ QBluetoothDeviceDiscoveryAgent::DiscoveryMethods QBluetoothDeviceDiscoveryAgent: return (ClassicMethod | LowEnergyMethod); } -void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods /*methods*/) +void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods) { // Currently both BlueZ backends do not distinguish discovery methods. // The DBus API's always return both device types. Therefore we ignore @@ -139,7 +139,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent discoveredDevices.clear(); if (managerBluez5) { - startBluez5(); + startBluez5(methods); return; } @@ -228,11 +228,10 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent } } -void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5() +void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods) { Q_Q(QBluetoothDeviceDiscoveryAgent); - bool ok = false; const QString adapterPath = findAdapterForAddress(m_adapterAddress, &ok); if (!ok || adapterPath.isEmpty()) { @@ -257,6 +256,33 @@ void QBluetoothDeviceDiscoveryAgentPrivate::startBluez5() return; } + QVariantMap map; + if (methods == (QBluetoothDeviceDiscoveryAgent::LowEnergyMethod|QBluetoothDeviceDiscoveryAgent::ClassicMethod)) + map.insert(QStringLiteral("Transport"), QStringLiteral("auto")); + else if (methods & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod) + map.insert(QStringLiteral("Transport"), QStringLiteral("le")); + else + map.insert(QStringLiteral("Transport"), QStringLiteral("bredr")); + + // older BlueZ 5.x versions don't have this function + // filterReply returns UnknownMethod which we ignore + QDBusPendingReply<> filterReply = adapterBluez5->SetDiscoveryFilter(map); + filterReply.waitForFinished(); + if (filterReply.isError()) { + if (filterReply.error().type() == QDBusError::Other + && filterReply.error().name() == QStringLiteral("org.bluez.Error.Failed")) { + qCDebug(QT_BT_BLUEZ) << "Discovery method" << methods << "not supported"; + lastError = QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod; + errorString = QBluetoothDeviceDiscoveryAgent::tr("One or more device discovery methods " + "are not supported on this platform"); + delete adapterBluez5; + adapterBluez5 = 0; + emit q->error(lastError); + return; + } else if (filterReply.error().type() != QDBusError::UnknownMethod) { + qCDebug(QT_BT_BLUEZ) << "SetDiscoveryFilter failed:" << filterReply.error(); + } + } QtBluezDiscoveryManager::instance()->registerDiscoveryInterest(adapterBluez5->path()); QObject::connect(QtBluezDiscoveryManager::instance(), SIGNAL(discoveryInterrupted(QString)), @@ -403,18 +429,34 @@ void QBluetoothDeviceDiscoveryAgentPrivate::deviceFoundBluez5(const QString& dev // read information QBluetoothDeviceInfo deviceInfo(btAddress, btName, btClass); - - if (!btClass) - deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::LowEnergyCoreConfiguration); - else - deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::BaseRateCoreConfiguration); - deviceInfo.setRssi(device.rSSI()); + QList<QBluetoothUuid> uuids; - foreach (const QString &u, device.uUIDs()) - uuids.append(QBluetoothUuid(u)); + bool foundLikelyLowEnergyUuid = false; + for (const auto &u: device.uUIDs()) { + const QBluetoothUuid id(u); + if (id.isNull()) + continue; + + if (!foundLikelyLowEnergyUuid) { + //once we found one BTLE service we are done + bool ok = false; + quint16 shortId = id.toUInt16(&ok); + if (ok && ((shortId & QBluetoothUuid::GenericAccess) == QBluetoothUuid::GenericAccess)) + foundLikelyLowEnergyUuid = true; + } + uuids.append(id); + } deviceInfo.setServiceUuids(uuids, QBluetoothDeviceInfo::DataIncomplete); + if (!btClass) { + deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::LowEnergyCoreConfiguration); + } else { + deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::BaseRateCoreConfiguration); + if (foundLikelyLowEnergyUuid) + deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::BaseRateAndLowEnergyCoreConfiguration); + } + for (int i = 0; i < discoveredDevices.size(); i++) { if (discoveredDevices[i].address() == deviceInfo.address()) { if (discoveredDevices[i] == deviceInfo && lowEnergySearchTimeout > 0) { |