2017-06-06 02:41:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "btdiscovery.h"
|
|
|
|
#include "downloadfromdcthread.h"
|
2017-06-10 12:22:28 +00:00
|
|
|
#include "core/libdivecomputer.h"
|
2017-07-18 21:49:42 +00:00
|
|
|
#include <QTimer>
|
2017-06-06 02:41:57 +00:00
|
|
|
#include <QDebug>
|
2018-09-29 21:16:28 +00:00
|
|
|
#include <QLoggingCategory>
|
2018-09-25 00:39:33 +00:00
|
|
|
#include <QRegularExpression>
|
2018-09-26 03:06:05 +00:00
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <QCoreApplication>
|
2017-06-06 02:41:57 +00:00
|
|
|
|
2017-06-10 12:22:28 +00:00
|
|
|
extern QMap<QString, dc_descriptor_t *> descriptorLookup;
|
|
|
|
|
2018-09-26 02:59:27 +00:00
|
|
|
namespace {
|
|
|
|
QHash<QString, QBluetoothDeviceInfo> btDeviceInfo;
|
|
|
|
}
|
2019-09-27 23:26:54 +00:00
|
|
|
BTDiscovery *BTDiscovery::m_instance = NULL;
|
2017-06-06 02:41:57 +00:00
|
|
|
|
2017-06-10 12:22:28 +00:00
|
|
|
static dc_descriptor_t *getDeviceType(QString btName)
|
|
|
|
// central function to convert a BT name to a Subsurface known vendor/model pair
|
|
|
|
{
|
|
|
|
QString vendor, product;
|
|
|
|
|
|
|
|
if (btName.startsWith("OSTC")) {
|
|
|
|
vendor = "Heinrichs Weikamp";
|
2018-10-01 16:52:47 +00:00
|
|
|
if (btName.mid(4,1) == "3") product = "OSTC Plus";
|
2017-06-10 12:22:28 +00:00
|
|
|
else if (btName.mid(4,2) == "s#") product = "OSTC Sport";
|
2017-07-13 18:48:52 +00:00
|
|
|
else if (btName.mid(4,2) == "s ") product = "OSTC Sport";
|
2017-06-10 12:22:28 +00:00
|
|
|
else if (btName.mid(4,2) == "4-") product = "OSTC 4";
|
|
|
|
else if (btName.mid(4,2) == "2-") product = "OSTC 2N";
|
2018-07-02 16:24:26 +00:00
|
|
|
else if (btName.mid(4,2) == "+ ") product = "OSTC 2";
|
2019-11-21 11:38:35 +00:00
|
|
|
// all BT/BLE enabled OSTCs are HW_FAMILY_OSTC_3, so when we do not know,
|
|
|
|
// just use a default product that allows the codoe to download from the
|
|
|
|
// user's dive computer
|
|
|
|
else product = "OSTC 2";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("Predator") ||
|
|
|
|
btName.startsWith("Petrel") ||
|
|
|
|
btName.startsWith("Perdix") ||
|
|
|
|
btName.startsWith("Teric") ||
|
2020-08-06 17:24:20 +00:00
|
|
|
btName.startsWith("Peregrine") ||
|
2020-04-10 21:51:27 +00:00
|
|
|
btName.startsWith("NERD")) {
|
2017-06-12 18:29:19 +00:00
|
|
|
vendor = "Shearwater";
|
2020-04-10 21:47:36 +00:00
|
|
|
// both the Petrel and Petrel 2 identify as "Petrel" as BT/BLE device
|
|
|
|
// but only the Petrel 2 is listed as available dive computer on iOS (which requires BLE support)
|
|
|
|
// so always pick the "Petrel 2" as product when seeing a Petrel
|
|
|
|
if (btName.startsWith("Petrel")) product = "Petrel 2";
|
2017-06-12 18:29:19 +00:00
|
|
|
if (btName.startsWith("Perdix")) product = "Perdix";
|
2017-07-17 14:42:22 +00:00
|
|
|
if (btName.startsWith("Predator")) product = "Predator";
|
2018-09-09 00:54:25 +00:00
|
|
|
if (btName.startsWith("Teric")) product = "Teric";
|
2020-08-06 17:24:20 +00:00
|
|
|
if (btName.startsWith("Peregrine")) product = "Peregrine";
|
2019-07-20 04:50:36 +00:00
|
|
|
if (btName.startsWith("NERD")) product = "Nerd"; // next line might override this
|
|
|
|
if (btName.startsWith("NERD 2")) product = "Nerd 2";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("EON Steel")) {
|
2017-06-24 04:29:34 +00:00
|
|
|
vendor = "Suunto";
|
|
|
|
product = "EON Steel";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("EON Core")) {
|
2018-05-12 05:18:16 +00:00
|
|
|
vendor = "Suunto";
|
|
|
|
product = "EON Core";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("Suunto D5")) {
|
2019-05-16 21:44:26 +00:00
|
|
|
vendor = "Suunto";
|
|
|
|
product = "D5";
|
2020-07-13 21:05:11 +00:00
|
|
|
} else if (btName.startsWith("G2") || btName.startsWith("Aladin") || btName.startsWith("HUD") || btName.startsWith("A1")) {
|
2017-07-03 01:31:40 +00:00
|
|
|
vendor = "Scubapro";
|
2017-11-05 10:47:36 +00:00
|
|
|
if (btName.startsWith("G2")) product = "G2";
|
2019-04-27 21:13:03 +00:00
|
|
|
if (btName.startsWith("HUD")) product = "G2 HUD";
|
2017-11-05 10:47:36 +00:00
|
|
|
if (btName.startsWith("Aladin")) product = "Aladin Sport Matrix";
|
2020-07-13 21:05:11 +00:00
|
|
|
if (btName.startsWith("A1")) product = "Aladin A1";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("Mares")) {
|
2018-05-12 05:18:16 +00:00
|
|
|
vendor = "Mares";
|
|
|
|
// we don't know which of the dive computers it is,
|
|
|
|
// so let's just randomly pick one
|
|
|
|
product = "Quad";
|
2019-10-25 11:25:03 +00:00
|
|
|
// Some we can pick out directly
|
2019-10-26 10:27:08 +00:00
|
|
|
if (btName.startsWith("Mares Genius"))
|
2019-10-25 11:25:03 +00:00
|
|
|
product = "Genius";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("CARTESIO_")) {
|
2020-02-16 16:57:00 +00:00
|
|
|
vendor = "Cressi";
|
|
|
|
product = "Cartesio";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.startsWith("GOA_")) {
|
2020-02-16 16:57:00 +00:00
|
|
|
vendor = "Cressi";
|
|
|
|
product = "Goa";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^FI\\d{6}$"))) {
|
|
|
|
// The Pelagic dive computers (generally branded as Oceanic or Aqualung)
|
|
|
|
// show up with a two-byte model code followed by six bytes of serial
|
|
|
|
// number. The model code matches the hex model (so "FQ" is 0x4651,
|
|
|
|
// where 'F' is 46h and 'Q' is 51h in ASCII).
|
2019-08-24 21:10:25 +00:00
|
|
|
vendor = "Aqualung";
|
2020-05-14 19:15:24 +00:00
|
|
|
product = "i200C";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^FH\\d{6}$"))) {
|
2019-08-24 21:10:25 +00:00
|
|
|
vendor = "Aqualung";
|
2020-05-14 19:15:24 +00:00
|
|
|
product = "i300C";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^FQ\\d{6}$"))) {
|
2018-10-12 17:10:03 +00:00
|
|
|
vendor = "Aqualung";
|
|
|
|
product = "i770R";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^FR\\d{6}$"))) {
|
2019-08-24 21:10:25 +00:00
|
|
|
vendor = "Aqualung";
|
2020-05-14 19:15:24 +00:00
|
|
|
product = "i550C";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^FS\\d{6}$"))) {
|
2019-05-31 17:09:14 +00:00
|
|
|
vendor = "Oceanic";
|
|
|
|
product = "Geo 4.0";
|
2020-08-18 19:55:02 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^FT\\d{6}$"))) {
|
|
|
|
vendor = "Oceanic";
|
|
|
|
product = "Veo 4.0";
|
|
|
|
} else if (btName.contains(QRegularExpression("^FU\\d{6}$"))) {
|
|
|
|
vendor = "Sherwood";
|
|
|
|
product = "Wisdom 4";
|
|
|
|
} else if (btName.contains(QRegularExpression("^FV\\d{6}$"))) {
|
|
|
|
vendor = "Oceanic";
|
|
|
|
product = "ProPlus 4";
|
|
|
|
} else if (btName.contains(QRegularExpression("^ER\\d{6}$"))) {
|
|
|
|
vendor = "Oceanic";
|
|
|
|
product = "Pro Plus X";
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^DS\\d{6}"))) {
|
|
|
|
// The Ratio bluetooth name looks like the Pelagic ones,
|
|
|
|
// but that seems to be just happenstance.
|
2019-07-15 18:18:32 +00:00
|
|
|
vendor = "Ratio";
|
|
|
|
product = "iX3M GPS Easy"; // we don't know which of the GPS models, so set one
|
2020-04-10 21:51:27 +00:00
|
|
|
} else if (btName == "COSMIQ") {
|
2019-08-17 22:17:50 +00:00
|
|
|
vendor = "Deepblu";
|
|
|
|
product = "Cosmiq+";
|
2020-06-25 18:57:35 +00:00
|
|
|
} else if (btName.startsWith("S1")) {
|
|
|
|
vendor = "Oceans";
|
|
|
|
product = "S1";
|
2020-07-27 14:50:54 +00:00
|
|
|
} else if (btName.startsWith("McLean Extreme")) {
|
|
|
|
vendor = "McLean";
|
|
|
|
product = "Extreme";
|
2020-08-18 19:55:02 +00:00
|
|
|
} else if (btName.startsWith("DiveComputer")) {
|
|
|
|
vendor = "Tecdiving";
|
|
|
|
product = "DiveComputer.eu";
|
2019-08-17 22:17:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 21:51:27 +00:00
|
|
|
// check if we found a known dive computer
|
|
|
|
if (!vendor.isEmpty() && !product.isEmpty()) {
|
2020-05-14 19:15:24 +00:00
|
|
|
dc_descriptor_t *lookup = descriptorLookup.value(vendor.toLower() + product.toLower());
|
2020-04-10 21:51:27 +00:00
|
|
|
if (!lookup)
|
|
|
|
qWarning("known dive computer %s not found in descriptorLookup", qPrintable(QString(vendor + product)));
|
|
|
|
return lookup;
|
|
|
|
}
|
2017-06-10 12:22:28 +00:00
|
|
|
|
2019-02-12 15:35:35 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool matchesKnownDiveComputerNames(QString btName)
|
|
|
|
{
|
|
|
|
return getDeviceType(btName) != nullptr;
|
2017-06-10 12:22:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:36:04 +00:00
|
|
|
BTDiscovery::BTDiscovery(QObject*) : m_btValid(false),
|
2019-02-14 06:48:50 +00:00
|
|
|
m_showNonDiveComputers(false),
|
2018-09-25 00:28:55 +00:00
|
|
|
discoveryAgent(nullptr)
|
2017-06-06 02:41:57 +00:00
|
|
|
{
|
2019-09-27 23:26:54 +00:00
|
|
|
if (m_instance) {
|
|
|
|
qDebug() << "trying to create an additional BTDiscovery object";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_instance = this;
|
2017-06-06 02:41:57 +00:00
|
|
|
#if defined(BT_SUPPORT)
|
2018-09-29 21:16:28 +00:00
|
|
|
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
|
2017-10-12 07:43:40 +00:00
|
|
|
BTDiscoveryReDiscover();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-14 06:48:50 +00:00
|
|
|
void BTDiscovery::showNonDiveComputers(bool show)
|
|
|
|
{
|
|
|
|
m_showNonDiveComputers = show;
|
|
|
|
}
|
|
|
|
|
2017-10-12 07:43:40 +00:00
|
|
|
void BTDiscovery::BTDiscoveryReDiscover()
|
|
|
|
{
|
2017-07-18 21:49:14 +00:00
|
|
|
#if !defined(Q_OS_IOS)
|
2018-12-23 14:35:21 +00:00
|
|
|
qDebug() << "BTDiscoveryReDiscover: localBtDevice.isValid()" << localBtDevice.isValid();
|
2017-06-06 02:41:57 +00:00
|
|
|
if (localBtDevice.isValid() &&
|
2018-12-23 14:35:21 +00:00
|
|
|
localBtDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff) {
|
2017-06-06 02:41:57 +00:00
|
|
|
btPairedDevices.clear();
|
2018-12-23 14:35:21 +00:00
|
|
|
qDebug() << "BTDiscoveryReDiscover: localDevice " + localBtDevice.name() + " is powered on, starting discovery";
|
2017-07-18 21:49:14 +00:00
|
|
|
#else
|
2017-09-23 22:05:08 +00:00
|
|
|
// for iOS we can't use the localBtDevice as iOS is BLE only
|
|
|
|
// we need to find some other way to test if Bluetooth is enabled, though
|
|
|
|
// for now just hard-code it
|
|
|
|
if (1) {
|
2017-07-18 21:49:14 +00:00
|
|
|
#endif
|
2017-09-23 22:05:08 +00:00
|
|
|
m_btValid = true;
|
2020-09-29 21:58:13 +00:00
|
|
|
|
2018-09-25 00:28:55 +00:00
|
|
|
if (discoveryAgent == nullptr) {
|
|
|
|
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
|
2020-09-29 21:50:05 +00:00
|
|
|
discoveryAgent->setLowEnergyDiscoveryTimeout(3 * 60 * 1000); // search for three minutes
|
2018-09-25 00:28:55 +00:00
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BTDiscovery::btDeviceDiscovered);
|
2020-09-29 21:50:05 +00:00
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BTDiscovery::btDeviceDiscoveryFinished);
|
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &BTDiscovery::btDeviceDiscoveryFinished);
|
|
|
|
connect(discoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error),
|
|
|
|
[this](QBluetoothDeviceDiscoveryAgent::Error error){
|
|
|
|
qDebug() << "device discovery received error" << discoveryAgent->errorString();
|
|
|
|
});
|
|
|
|
qDebug() << "discovery methods" << (int)QBluetoothDeviceDiscoveryAgent::supportedDiscoveryMethods();
|
2018-09-25 00:28:55 +00:00
|
|
|
}
|
2020-09-29 21:58:13 +00:00
|
|
|
#if defined(Q_OS_ANDROID)
|
|
|
|
// on Android, we cannot scan for classic devices - we just get the paired ones
|
2017-07-18 21:49:14 +00:00
|
|
|
qDebug() << "starting BLE discovery";
|
2020-09-29 21:58:13 +00:00
|
|
|
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
|
2017-06-06 02:41:57 +00:00
|
|
|
getBluetoothDevices();
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
// and add the paired devices to the internal data
|
|
|
|
// So behaviour is same on Linux/Bluez stack and
|
|
|
|
// Android/Java stack with respect to discovery
|
2018-09-25 00:28:55 +00:00
|
|
|
for (int i = 0; i < btPairedDevices.length(); i++)
|
2020-09-29 21:46:47 +00:00
|
|
|
btDeviceDiscoveredMain(btPairedDevices[i], true);
|
2020-09-29 21:58:13 +00:00
|
|
|
#else
|
|
|
|
qDebug() << "starting BT/BLE discovery";
|
|
|
|
discoveryAgent->start();
|
2018-09-25 00:28:55 +00:00
|
|
|
for (int i = 0; i < btPairedDevices.length(); i++)
|
2017-06-30 02:47:51 +00:00
|
|
|
qDebug() << "Paired =" << btPairedDevices[i].name << btPairedDevices[i].address;
|
2020-09-29 21:58:13 +00:00
|
|
|
#endif
|
2018-09-25 00:28:55 +00:00
|
|
|
|
2017-07-18 21:49:42 +00:00
|
|
|
#if defined(Q_OS_IOS) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
connect(&timer, &QTimer::timeout, discoveryAgent, &QBluetoothDeviceDiscoveryAgent::stop);
|
|
|
|
timer.start(3000);
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2017-09-23 22:05:08 +00:00
|
|
|
qDebug() << "localBtDevice isn't valid or not connectable";
|
2017-07-12 11:13:25 +00:00
|
|
|
m_btValid = false;
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BTDiscovery::~BTDiscovery()
|
|
|
|
{
|
2019-09-27 23:26:54 +00:00
|
|
|
m_instance = NULL;
|
2017-06-06 02:41:57 +00:00
|
|
|
#if defined(BT_SUPPORT)
|
2019-03-17 03:59:18 +00:00
|
|
|
delete discoveryAgent;
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-27 23:26:54 +00:00
|
|
|
BTDiscovery *BTDiscovery::instance()
|
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new BTDiscovery();
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
#if defined(BT_SUPPORT)
|
|
|
|
extern void addBtUuid(QBluetoothUuid uuid);
|
|
|
|
extern QHash<QString, QStringList> productList;
|
|
|
|
extern QStringList vendorList;
|
|
|
|
|
2017-11-13 21:46:42 +00:00
|
|
|
QString btDeviceAddress(const QBluetoothDeviceInfo *device, bool isBle)
|
|
|
|
{
|
|
|
|
QString address = device->address().isNull() ?
|
|
|
|
device->deviceUuid().toString() : device->address().toString();
|
|
|
|
const char *prefix = isBle ? "LE:" : "";
|
|
|
|
return prefix + address;
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:43:00 +00:00
|
|
|
QString markBLEAddress(const QBluetoothDeviceInfo *device)
|
|
|
|
{
|
2017-11-13 21:46:42 +00:00
|
|
|
QBluetoothDeviceInfo::CoreConfigurations flags = device->coreConfigurations();
|
|
|
|
bool isBle = flags == QBluetoothDeviceInfo::LowEnergyCoreConfiguration;
|
|
|
|
|
|
|
|
return btDeviceAddress(device, isBle);
|
2017-06-30 02:43:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:50:05 +00:00
|
|
|
void BTDiscovery::btDeviceDiscoveryFinished()
|
|
|
|
{
|
|
|
|
qDebug() << "BT/BLE finished discovery";
|
|
|
|
QList<QBluetoothDeviceInfo> devList = discoveryAgent->discoveredDevices();
|
|
|
|
for (QBluetoothDeviceInfo device: devList) {
|
|
|
|
qDebug() << device.name() << device.address().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device)
|
|
|
|
{
|
|
|
|
btPairedDevice this_d;
|
2017-06-30 02:47:51 +00:00
|
|
|
this_d.address = markBLEAddress(&device);
|
2017-06-06 02:41:57 +00:00
|
|
|
this_d.name = device.name();
|
|
|
|
btPairedDevices.append(this_d);
|
|
|
|
|
2019-04-01 20:15:19 +00:00
|
|
|
const auto serviceUuids = device.serviceUuids();
|
|
|
|
for (QBluetoothUuid id: serviceUuids) {
|
2017-06-06 02:41:57 +00:00
|
|
|
addBtUuid(id);
|
|
|
|
qDebug() << id.toByteArray();
|
|
|
|
}
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
|
2018-09-26 02:36:34 +00:00
|
|
|
#if defined(Q_OS_IOS) || defined(Q_OS_MACOS) || defined(Q_OS_WIN)
|
|
|
|
// on Windows, macOS and iOS we need to scan in order to be able to access a device;
|
|
|
|
// let's remember the information we scanned on this run so we can at least
|
|
|
|
// refer back to it and don't need to open the separate scanning dialog every
|
|
|
|
// time we try to download from a BT/BLE dive computer.
|
2018-02-24 23:17:33 +00:00
|
|
|
saveBtDeviceInfo(btDeviceAddress(&device, false), device);
|
2017-12-02 06:49:36 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-29 21:46:47 +00:00
|
|
|
btDeviceDiscoveredMain(this_d, false);
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:46:47 +00:00
|
|
|
void BTDiscovery::btDeviceDiscoveredMain(const btPairedDevice &device, bool fromPaired)
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
{
|
|
|
|
btVendorProduct btVP;
|
|
|
|
|
|
|
|
QString newDevice;
|
|
|
|
dc_descriptor_t *newDC = getDeviceType(device.name);
|
|
|
|
if (newDC)
|
|
|
|
newDevice = dc_descriptor_get_product(newDC);
|
2017-07-17 04:49:11 +00:00
|
|
|
else
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
newDevice = device.name;
|
|
|
|
|
2020-09-29 21:46:47 +00:00
|
|
|
qDebug() << (fromPaired ? "Paired device" : "Discovered new device:") << newDevice << device.address;
|
2017-07-17 19:40:30 +00:00
|
|
|
if (newDC) {
|
|
|
|
QString vendor = dc_descriptor_get_vendor(newDC);
|
|
|
|
qDebug() << "this could be a " + vendor + " " + newDevice;
|
|
|
|
btVP.btpdi = device;
|
|
|
|
btVP.dcDescriptor = newDC;
|
|
|
|
btVP.vendorIdx = vendorList.indexOf(vendor);
|
|
|
|
btVP.productIdx = productList[vendor].indexOf(newDevice);
|
|
|
|
btDCs << btVP;
|
2017-12-07 13:40:23 +00:00
|
|
|
connectionListModel.addAddress(newDevice + " " + device.address);
|
2017-07-17 19:40:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-14 06:48:50 +00:00
|
|
|
// Do we want only devices we recognize as dive computers?
|
2020-05-15 00:37:24 +00:00
|
|
|
if (m_showNonDiveComputers) {
|
|
|
|
if (!newDevice.isEmpty())
|
|
|
|
newDevice += " ";
|
|
|
|
connectionListModel.addAddress(newDevice + device.address);
|
|
|
|
}
|
2017-07-04 00:25:51 +00:00
|
|
|
qDebug() << "Not recognized as dive computer";
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
QList<BTDiscovery::btVendorProduct> BTDiscovery::getBtDcs()
|
2017-06-06 02:41:57 +00:00
|
|
|
{
|
|
|
|
return btDCs;
|
|
|
|
}
|
|
|
|
|
2017-07-12 11:13:25 +00:00
|
|
|
bool BTDiscovery::btAvailable() const
|
|
|
|
{
|
|
|
|
return m_btValid;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
// Android: As Qt is not able to pull the pairing data from a device, i
|
|
|
|
// a lengthy discovery process is needed to see what devices are paired. On
|
|
|
|
// https://forum.qt.io/topic/46075/solved-bluetooth-list-paired-devices
|
|
|
|
// user s.frings74 does, however, present a solution to this using JNI.
|
|
|
|
// Currently, this code is taken "as is".
|
|
|
|
|
|
|
|
#if defined(Q_OS_ANDROID)
|
|
|
|
void BTDiscovery::getBluetoothDevices()
|
|
|
|
{
|
|
|
|
struct BTDiscovery::btPairedDevice result;
|
|
|
|
// Query via Android Java API.
|
|
|
|
|
|
|
|
// returns a BluetoothAdapter
|
|
|
|
QAndroidJniObject adapter=QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter","getDefaultAdapter","()Landroid/bluetooth/BluetoothAdapter;");
|
|
|
|
if (checkException("BluetoothAdapter.getDefaultAdapter()", &adapter)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// returns a Set<BluetoothDevice>
|
|
|
|
QAndroidJniObject pairedDevicesSet=adapter.callObjectMethod("getBondedDevices","()Ljava/util/Set;");
|
|
|
|
if (checkException("BluetoothAdapter.getBondedDevices()", &pairedDevicesSet)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jint size=pairedDevicesSet.callMethod<jint>("size");
|
|
|
|
checkException("Set<BluetoothDevice>.size()", &pairedDevicesSet);
|
|
|
|
if (size > 0) {
|
|
|
|
// returns an Iterator<BluetoothDevice>
|
|
|
|
QAndroidJniObject iterator=pairedDevicesSet.callObjectMethod("iterator","()Ljava/util/Iterator;");
|
|
|
|
if (checkException("Set<BluetoothDevice>.iterator()", &iterator)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
// returns a BluetoothDevice
|
|
|
|
QAndroidJniObject dev=iterator.callObjectMethod("next","()Ljava/lang/Object;");
|
|
|
|
if (checkException("Iterator<BluetoothDevice>.next()", &dev)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-30 05:53:07 +00:00
|
|
|
jint btType = dev.callMethod<jint>("getType", "()I");
|
2018-04-27 22:04:50 +00:00
|
|
|
// 1 means Classic. 2 means BLE, 3 means dual stack
|
2017-06-30 02:47:51 +00:00
|
|
|
result.address = dev.callObjectMethod("getAddress","()Ljava/lang/String;").toString();
|
2017-06-06 02:41:57 +00:00
|
|
|
result.name = dev.callObjectMethod("getName", "()Ljava/lang/String;").toString();
|
2018-04-27 22:04:50 +00:00
|
|
|
if (btType & 1) { // DEVICE_TYPE_CLASSIC
|
|
|
|
qDebug() << "paired BT classic device type" << btType << "with address" << result.address;
|
|
|
|
btPairedDevices.append(result);
|
|
|
|
}
|
|
|
|
if (btType & 2) { // DEVICE_TYPE_LE
|
2017-07-03 09:16:18 +00:00
|
|
|
result.address = QString("LE:%1").arg(result.address);
|
2018-04-27 22:04:50 +00:00
|
|
|
qDebug() << "paired BLE device type" << btType << "with address" << result.address;
|
2017-07-03 09:16:18 +00:00
|
|
|
btPairedDevices.append(result);
|
|
|
|
}
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BTDiscovery::checkException(const char* method, const QAndroidJniObject *obj)
|
|
|
|
{
|
|
|
|
static QAndroidJniEnvironment env;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
qCritical("Exception in %s", method);
|
|
|
|
env->ExceptionDescribe();
|
|
|
|
env->ExceptionClear();
|
|
|
|
result=true;
|
|
|
|
}
|
|
|
|
if (!(obj == NULL || obj->isValid())) {
|
|
|
|
qCritical("Invalid object returned by %s", method);
|
|
|
|
result=true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // Q_OS_ANDROID
|
2017-06-10 12:22:28 +00:00
|
|
|
|
2018-09-26 03:04:02 +00:00
|
|
|
void BTDiscovery::discoverAddress(QString address)
|
|
|
|
{
|
2019-01-23 01:28:56 +00:00
|
|
|
// if we have a discovery agent, check if we know about the address and if not
|
|
|
|
// make sure we are looking for it
|
|
|
|
// (if we don't have a discoveryAgent then likely BT is off or something else went wrong)
|
|
|
|
if (!discoveryAgent)
|
|
|
|
return;
|
|
|
|
|
2018-09-27 13:21:04 +00:00
|
|
|
// let's make sure there is no device name mixed in with the address
|
|
|
|
QString btAddress;
|
|
|
|
btAddress = extractBluetoothAddress(address);
|
|
|
|
|
2018-09-26 03:04:02 +00:00
|
|
|
if (!btDeviceInfo.keys().contains(address) && !discoveryAgent->isActive()) {
|
|
|
|
qDebug() << "restarting discovery agent";
|
|
|
|
discoveryAgent->start();
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 03:19:41 +00:00
|
|
|
|
2020-08-21 22:42:39 +00:00
|
|
|
void BTDiscovery::stopAgent()
|
|
|
|
{
|
|
|
|
if (!discoveryAgent)
|
|
|
|
return;
|
|
|
|
qDebug() << "---> stopping the discovery agent";
|
|
|
|
discoveryAgent->stop();
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:39:33 +00:00
|
|
|
bool isBluetoothAddress(const QString &address)
|
2018-09-27 13:21:04 +00:00
|
|
|
{
|
|
|
|
return extractBluetoothAddress(address) != QString();
|
|
|
|
}
|
|
|
|
QString extractBluetoothAddress(const QString &address)
|
2018-09-25 00:39:33 +00:00
|
|
|
{
|
|
|
|
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);
|
2018-09-27 13:21:04 +00:00
|
|
|
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();
|
2018-09-25 00:39:33 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 23:17:33 +00:00
|
|
|
void saveBtDeviceInfo(const QString &devaddr, QBluetoothDeviceInfo deviceInfo)
|
2017-09-17 03:19:41 +00:00
|
|
|
{
|
|
|
|
btDeviceInfo[devaddr] = deviceInfo;
|
|
|
|
}
|
|
|
|
|
2018-02-24 23:17:33 +00:00
|
|
|
QBluetoothDeviceInfo getBtDeviceInfo(const QString &devaddr)
|
2017-09-17 03:19:41 +00:00
|
|
|
{
|
2020-08-21 22:42:39 +00:00
|
|
|
if (btDeviceInfo.contains(devaddr)) {
|
|
|
|
BTDiscovery::instance()->stopAgent();
|
2017-09-17 03:19:41 +00:00
|
|
|
return btDeviceInfo[devaddr];
|
2020-08-21 22:42:39 +00:00
|
|
|
}
|
2018-09-26 03:06:05 +00:00
|
|
|
if(!btDeviceInfo.keys().contains(devaddr)) {
|
|
|
|
qDebug() << "still looking scan is still running, we should just wait for a few moments";
|
|
|
|
// wait for a maximum of 30 more seconds
|
|
|
|
// yes, that seems crazy, but on my Mac I see this take more than 20 seconds
|
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
|
|
|
do {
|
2020-08-21 22:42:39 +00:00
|
|
|
if (btDeviceInfo.keys().contains(devaddr)) {
|
|
|
|
BTDiscovery::instance()->stopAgent();
|
2018-09-26 03:06:05 +00:00
|
|
|
return btDeviceInfo[devaddr];
|
2020-08-21 22:42:39 +00:00
|
|
|
}
|
2018-09-26 03:06:05 +00:00
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
|
|
QThread::msleep(100);
|
|
|
|
} while (timer.elapsed() < 30000);
|
|
|
|
}
|
|
|
|
qDebug() << "notify user that we can't find" << devaddr;
|
2017-09-17 03:19:41 +00:00
|
|
|
return QBluetoothDeviceInfo();
|
|
|
|
}
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif // BT_SUPPORT
|