diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2018-09-27 06:21:04 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-12 08:22:44 -0700 |
commit | c8fec97695cc2f71b59f3d8979b4203aefc6e6db (patch) | |
tree | 0340a5187be9e9558e0cdb39afd2efb29c2c1ba0 | |
parent | 7512a6e9155196a3ede34b8a552c7c8dfb955ad3 (diff) | |
download | subsurface-c8fec97695cc2f71b59f3d8979b4203aefc6e6db.tar.gz |
Bluetooth: add helper to separate BT name and address
And restructure the existing "isBtAddress()" function in the process.
Also add more tests.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | core/btdiscovery.cpp | 29 | ||||
-rw-r--r-- | core/btdiscovery.h | 2 | ||||
-rw-r--r-- | tests/testhelper.cpp | 18 | ||||
-rw-r--r-- | tests/testhelper.h | 1 |
4 files changed, 49 insertions, 1 deletions
diff --git a/core/btdiscovery.cpp b/core/btdiscovery.cpp index deef322e4..258f16e2c 100644 --- a/core/btdiscovery.cpp +++ b/core/btdiscovery.cpp @@ -305,6 +305,10 @@ bool BTDiscovery::checkException(const char* method, const QAndroidJniObject *ob void BTDiscovery::discoverAddress(QString address) { + // let's make sure there is no device name mixed in with the address + QString btAddress; + btAddress = extractBluetoothAddress(address); + #if defined(Q_OS_MACOS) // macOS appears to need a fresh scan if we want to switch devices static QString lastAddress; @@ -322,10 +326,33 @@ void BTDiscovery::discoverAddress(QString address) bool isBluetoothAddress(const QString &address) { + return extractBluetoothAddress(address) != QString(); +} +QString extractBluetoothAddress(const QString &address) +{ QRegularExpression re("(LE:)*([0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}|{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}})", QRegularExpression::CaseInsensitiveOption); QRegularExpressionMatch m = re.match(address); - return m.hasMatch(); + return m.captured(0); +} + +QString extractBluetoothNameAddress(const QString &address, QString &name) +{ + // sometimes our device text is of the form "name (address)", sometimes it's just "address" + // let's simply return the address + name = QString(); + QString extractedAddress = extractBluetoothAddress(address); + if (extractedAddress == address.trimmed()) + return address; + + QRegularExpression re("^([^()]+)\\(([^)]*\\))$"); + QRegularExpressionMatch m = re.match(address); + if (m.hasMatch()) { + name = m.captured(1).trimmed(); + return extractedAddress; + } + qDebug() << "can't parse address" << address; + return QString(); } void saveBtDeviceInfo(const QString &devaddr, QBluetoothDeviceInfo deviceInfo) diff --git a/core/btdiscovery.h b/core/btdiscovery.h index 17790e30c..679e0cae6 100644 --- a/core/btdiscovery.h +++ b/core/btdiscovery.h @@ -18,6 +18,8 @@ void saveBtDeviceInfo(const QString &devaddr, QBluetoothDeviceInfo deviceInfo); bool isBluetoothAddress(const QString &address); +QString extractBluetoothAddress(const QString &address); +QString extractBluetoothNameAddress(const QString &address, QString &name); QBluetoothDeviceInfo getBtDeviceInfo(const QString &devaddr); class BTDiscovery : public QObject { diff --git a/tests/testhelper.cpp b/tests/testhelper.cpp index 6d15ad3c4..83cd3176d 100644 --- a/tests/testhelper.cpp +++ b/tests/testhelper.cpp @@ -23,4 +23,22 @@ void TestHelper::recognizeBtAddress() QCOMPARE(isBluetoothAddress("LE:{6e50ff5d-cdd3-4c43-ag0a-1ed4c7d2d2a5}"), false); } +void TestHelper::parseNameAddress() +{ + QString name, address; + address = extractBluetoothNameAddress("01:a2:b3:c4:d5:06", name); + QCOMPARE(address, "01:a2:b3:c4:d5:06"); + QCOMPARE(name, QString()); + address = extractBluetoothNameAddress("somename (01:a2:b3:c4:d5:06)", name); + QCOMPARE(address, "01:a2:b3:c4:d5:06"); + QCOMPARE(name, QString("somename")); + address = extractBluetoothNameAddress("garbage", name); + QCOMPARE(address, QString()); + QCOMPARE(name, QString()); + address = extractBluetoothNameAddress("somename (LE:{6e50ff5d-cdd3-4c43-a80a-1ed4c7d2d2a5})", name); + QCOMPARE(address, "LE:{6e50ff5d-cdd3-4c43-a80a-1ed4c7d2d2a5}"); + QCOMPARE(name, QString("somename")); + +} + QTEST_GUILESS_MAIN(TestHelper) diff --git a/tests/testhelper.h b/tests/testhelper.h index e6af513dc..00a187c6f 100644 --- a/tests/testhelper.h +++ b/tests/testhelper.h @@ -10,6 +10,7 @@ class TestHelper : public QObject { private slots: void initTestCase(); void recognizeBtAddress(); + void parseNameAddress(); }; #endif |