Changeset 345e063 in subsurface


Ignore:
Timestamp:
Jul 3, 2017, 9:58:26 PM (5 months ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
bc1a313c
Parents:
bc864c3
git-author:
Dirk Hohndel <dirk@…> (07/03/17 17:28:27)
git-committer:
Dirk Hohndel <dirk@…> (07/03/17 21:58:26)
Message:

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@…>

Location:
core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • core/btdiscovery.cpp

    rbc864c3 r345e063  
    174174{
    175175        return btDCs;
    176 }
    177 
    178 QList <BTDiscovery::btVendorProduct> BTDiscovery::getBtAllDevices()
    179 {
    180         return btAllDevices;
    181176}
    182177
  • core/btdiscovery.h

    rc145cbd r345e063  
    4545#endif
    4646        QList<btVendorProduct> getBtDcs();
    47         QList<btVendorProduct> getBtAllDevices();
    4847#endif
    4948private:
  • core/downloadfromdcthread.cpp

    rd17bb63 r345e063  
    22#include "core/libdivecomputer.h"
    33#include <QDebug>
     4#include <QRegularExpression>
    45
    56QStringList vendorList;
     
    290291#if defined(BT_SUPPORT)
    291292        QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
    292         QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices();
    293293
    294294        // Pick the vendor of the first confirmed find of a DC (if any), but
     
    297297                qDebug() << "getDetectedVendorIndex" << currentText << btDCs.first().vendorIdx;
    298298                return btDCs.first().vendorIdx;
    299         }
    300 
    301         // When the above fails, just pick the (one and only) virtual vendor
    302         if (!btAllDevices.isEmpty() && currentText == QObject::tr("Paired Bluetooth Devices")) {
    303                 qDebug() << "getDetectedVendorIndex" << currentText << btAllDevices.first().vendorIdx;
    304                 return btAllDevices.first().vendorIdx;
    305299        }
    306300#endif
     
    335329{
    336330#if defined(BT_SUPPORT)
    337         QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
    338         QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices();
    339 
    340         // Pull the BT address from the first found dive computer that is been
    341         // detected as a possible real dive computer (and not some other paired
    342         // BT device
    343         if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) {
    344                 QString btAddr = btDCs.first().btpdi.address;
    345                 qDebug() << "getDetectedDeviceAddress" << btAddr;
    346                 return btAddr;
    347         }
    348 
    349         // if the above fails, pull the BT address from the selected paired device
    350         // unsure being a dive computer
    351331        if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) {
    352                 int i =  productList[currentVendorText].indexOf(currentProductText);
    353                 QString btAddr = btAllDevices[i].btpdi.address;
    354                 qDebug() << "getDetectedDeviceAddress" << btAddr;
    355                 return btAddr;
    356         }
    357 #endif
    358         return QString();
     332                // simply get the address from the product text
     333                QRegularExpression extractAddr(".*\\(([0-9A-FL:]*)\\)");
     334                QRegularExpressionMatch m = extractAddr.match(currentProductText);
     335                if (m.hasMatch()) {
     336                        qDebug() << "matched" << m.captured(1);
     337                        return m.captured(1);
     338                }
     339        }
     340        // Otherwise, pull the vendor from the found devices that are possible real dive computers
     341        // HACK: this assumes that dive computer names are unique across vendors
     342        //       and will only give you the first of multiple identically named dive computers - use
     343        //       the Paired Bluetooth Devices vendor in cases like that
     344        QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
     345        BTDiscovery::btVendorProduct btDC;
     346        Q_FOREACH(btDC, btDCs) {
     347                if (currentProductText.startsWith(dc_descriptor_get_product(btDC.dcDescriptor)))
     348                        return btDC.btpdi.address;
     349        }
     350#endif
     351        return QStringLiteral("cannot determine address of dive computer");
    359352}
    360353
     
    363356{
    364357#if defined(BT_SUPPORT)
    365         QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
    366         QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices();
    367 
    368         // Pull the vendor from the first found dive computer that is been
    369         // detected as a possible real dive computer (and not some other paired
    370         // BT device
    371         if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) {
    372                 QString dcVendor =  dc_descriptor_get_vendor(btDCs.first().dcDescriptor);
    373                 qDebug() << "getDeviceDescriptorVendor" << dcVendor;
    374                 return dcVendor;
    375         }
    376 
    377         // if the above fails, pull vendor from the selected paired device
    378         // unsure being a dive computer
    379         if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) {
    380                 int i =  productList[currentVendorText].indexOf(currentProductText);
    381                 if (i < 0 || btAllDevices.length() <= i)
    382                         return QString();
    383                 QString dcVendor = dc_descriptor_get_vendor(btAllDevices[i].dcDescriptor);
    384                 qDebug() << "getDeviceDescriptorVendor" << dcVendor;
    385                 return dcVendor;
    386         }
    387 #endif
    388         return NULL;
     358        if (currentVendorText != QObject::tr("Paired Bluetooth Devices"))
     359                return currentVendorText;
     360
     361        QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
     362
     363        // Pull the vendor from the found devices that are possible real dive computers
     364        // HACK: this assumes that dive computer names are unique across vendors
     365        BTDiscovery::btVendorProduct btDC;
     366        Q_FOREACH(btDC, btDCs) {
     367                if (currentProductText.startsWith(dc_descriptor_get_product(btDC.dcDescriptor)))
     368                        return dc_descriptor_get_vendor(btDC.dcDescriptor);
     369        }
     370#endif
     371        return QStringLiteral("failed to detect vendor");
    389372}
    390373
     
    393376{
    394377#if defined(BT_SUPPORT)
    395         QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
    396         QList<BTDiscovery::btVendorProduct> btAllDevices = BTDiscovery::instance()->getBtAllDevices();
    397 
    398         // Pull the product from the first found dive computer that is been
    399         // detected as a possible real dive computer (and not some other paired
    400         // BT device
    401         if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) {
    402                 QString dcProduct =  dc_descriptor_get_product(btDCs.first().dcDescriptor);
    403                 qDebug() << "getDeviceDescriptorProduct" << dcProduct;
    404                 return dcProduct;
    405         }
    406 
    407         // if the above fails, pull product from the selected paired device
    408         // unsure being a dive computer
    409         if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) {
    410                 int i =  productList[currentVendorText].indexOf(currentProductText);
    411                 if (i >= btAllDevices.length() || i < 0)
    412                         return QString();
    413                 QString dcProduct = dc_descriptor_get_product(btAllDevices[i].dcDescriptor);
    414                 qDebug() << "getDeviceDescriptorProduct" << dcProduct;
    415                 return dcProduct;
    416         }
    417 #endif
    418         return NULL;
    419 }
     378        if (currentVendorText != QObject::tr("Paired Bluetooth Devices"))
     379                return currentProductText;
     380
     381        QList<BTDiscovery::btVendorProduct> btDCs = BTDiscovery::instance()->getBtDcs();
     382
     383        // Pull the canonical product from the found devices that are possible real dive computers
     384        // HACK: this assumes that dive computer names are unique across vendors
     385        BTDiscovery::btVendorProduct btDC;
     386        Q_FOREACH(btDC, btDCs) {
     387                if (currentProductText.startsWith(dc_descriptor_get_product(btDC.dcDescriptor)))
     388                        return dc_descriptor_get_product(btDC.dcDescriptor);
     389        }
     390#endif
     391        return QStringLiteral("failed to detect product");
     392}
Note: See TracChangeset for help on using the changeset viewer.