diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2017-07-03 17:28:27 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2017-07-03 21:58:26 -0700 |
commit | 345e063eb5ff76306e4aafc110351c1bee0e1335 (patch) | |
tree | ad649caa76be212ea55aad416d69e64d8f572591 /core/downloadfromdcthread.cpp | |
parent | bc864c3bce9a004f60879d33e61f97511ecbed93 (diff) | |
download | subsurface-345e063eb5ff76306e4aafc110351c1bee0e1335.tar.gz |
Rewrite the matching code for BT devices
This should be much more robust in getting us the correct Bluetooth address
and the correct vendor / product for our selection.
When we pick a paired device, we extract the address right from its name.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'core/downloadfromdcthread.cpp')
-rw-r--r-- | core/downloadfromdcthread.cpp | 105 |
1 files changed, 39 insertions, 66 deletions
diff --git a/core/downloadfromdcthread.cpp b/core/downloadfromdcthread.cpp index ad4f0e4d0..98550ad79 100644 --- a/core/downloadfromdcthread.cpp +++ b/core/downloadfromdcthread.cpp @@ -1,6 +1,7 @@ #include "downloadfromdcthread.h" #include "core/libdivecomputer.h" #include <QDebug> +#include <QRegularExpression> QStringList vendorList; QHash<QString, QStringList> productList; @@ -289,7 +290,6 @@ int DCDeviceData::getDetectedVendorIndex(const QString ¤tText) { #if defined(BT_SUPPORT) QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); - QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices(); // Pick the vendor of the first confirmed find of a DC (if any), but // only return a true vendor, and not our virtual one @@ -297,12 +297,6 @@ int DCDeviceData::getDetectedVendorIndex(const QString ¤tText) qDebug() << "getDetectedVendorIndex" << currentText << btDCs.first().vendorIdx; return btDCs.first().vendorIdx; } - - // When the above fails, just pick the (one and only) virtual vendor - if (!btAllDevices.isEmpty() && currentText == QObject::tr("Paired Bluetooth Devices")) { - qDebug() << "getDetectedVendorIndex" << currentText << btAllDevices.first().vendorIdx; - return btAllDevices.first().vendorIdx; - } #endif return -1; } @@ -334,86 +328,65 @@ QString DCDeviceData::getDetectedDeviceAddress(const QString ¤tVendorText, const QString ¤tProductText) { #if defined(BT_SUPPORT) - QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); - QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices(); - - // Pull the BT address from the first found dive computer that is been - // detected as a possible real dive computer (and not some other paired - // BT device - if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) { - QString btAddr = btDCs.first().btpdi.address; - qDebug() << "getDetectedDeviceAddress" << btAddr; - return btAddr; - } - - // if the above fails, pull the BT address from the selected paired device - // unsure being a dive computer if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) { - int i = productList[currentVendorText].indexOf(currentProductText); - QString btAddr = btAllDevices[i].btpdi.address; - qDebug() << "getDetectedDeviceAddress" << btAddr; - return btAddr; + // simply get the address from the product text + QRegularExpression extractAddr(".*\\(([0-9A-FL:]*)\\)"); + QRegularExpressionMatch m = extractAddr.match(currentProductText); + if (m.hasMatch()) { + qDebug() << "matched" << m.captured(1); + return m.captured(1); + } + } + // Otherwise, pull the vendor from the found devices that are possible real dive computers + // HACK: this assumes that dive computer names are unique across vendors + // and will only give you the first of multiple identically named dive computers - use + // the Paired Bluetooth Devices vendor in cases like that + QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); + BTDiscovery::btVendorProduct btDC; + Q_FOREACH(btDC, btDCs) { + if (currentProductText.startsWith(dc_descriptor_get_product(btDC.dcDescriptor))) + return btDC.btpdi.address; } #endif - return QString(); + return QStringLiteral("cannot determine address of dive computer"); } QString DCDeviceData::getDeviceDescriptorVendor(const QString ¤tVendorText, const QString ¤tProductText) { #if defined(BT_SUPPORT) - QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); - QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices(); + if (currentVendorText != QObject::tr("Paired Bluetooth Devices")) + return currentVendorText; - // Pull the vendor from the first found dive computer that is been - // detected as a possible real dive computer (and not some other paired - // BT device - if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) { - QString dcVendor = dc_descriptor_get_vendor(btDCs.first().dcDescriptor); - qDebug() << "getDeviceDescriptorVendor" << dcVendor; - return dcVendor; - } + QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); - // if the above fails, pull vendor from the selected paired device - // unsure being a dive computer - if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) { - int i = productList[currentVendorText].indexOf(currentProductText); - if (i < 0 || btAllDevices.length() <= i) - return QString(); - QString dcVendor = dc_descriptor_get_vendor(btAllDevices[i].dcDescriptor); - qDebug() << "getDeviceDescriptorVendor" << dcVendor; - return dcVendor; + // Pull the vendor from the found devices that are possible real dive computers + // HACK: this assumes that dive computer names are unique across vendors + BTDiscovery::btVendorProduct btDC; + Q_FOREACH(btDC, btDCs) { + if (currentProductText.startsWith(dc_descriptor_get_product(btDC.dcDescriptor))) + return dc_descriptor_get_vendor(btDC.dcDescriptor); } #endif - return NULL; + return QStringLiteral("failed to detect vendor"); } QString DCDeviceData::getDeviceDescriptorProduct(const QString ¤tVendorText, const QString ¤tProductText) { #if defined(BT_SUPPORT) - QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); - QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices(); + if (currentVendorText != QObject::tr("Paired Bluetooth Devices")) + return currentProductText; - // Pull the product from the first found dive computer that is been - // detected as a possible real dive computer (and not some other paired - // BT device - if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) { - QString dcProduct = dc_descriptor_get_product(btDCs.first().dcDescriptor); - qDebug() << "getDeviceDescriptorProduct" << dcProduct; - return dcProduct; - } + QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs(); - // if the above fails, pull product from the selected paired device - // unsure being a dive computer - if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) { - int i = productList[currentVendorText].indexOf(currentProductText); - if (i >= btAllDevices.length() || i < 0) - return QString(); - QString dcProduct = dc_descriptor_get_product(btAllDevices[i].dcDescriptor); - qDebug() << "getDeviceDescriptorProduct" << dcProduct; - return dcProduct; + // Pull the canonical product from the found devices that are possible real dive computers + // HACK: this assumes that dive computer names are unique across vendors + BTDiscovery::btVendorProduct btDC; + Q_FOREACH(btDC, btDCs) { + if (currentProductText.startsWith(dc_descriptor_get_product(btDC.dcDescriptor))) + return dc_descriptor_get_product(btDC.dcDescriptor); } #endif - return NULL; + return QStringLiteral("failed to detect product"); } |