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
|
|
|
|
2021-01-23 22:01:51 +00:00
|
|
|
struct modelPattern {
|
|
|
|
uint16_t model;
|
|
|
|
const char *vendor;
|
|
|
|
const char *product;
|
|
|
|
};
|
|
|
|
static struct modelPattern model[] = {
|
|
|
|
{ 0x4552, "Oceanic", "Pro Plus X" },
|
|
|
|
{ 0x455A, "Aqualung", "i750TC" },
|
|
|
|
{ 0x4647, "Sherwood", "Sage" },
|
|
|
|
{ 0x4648, "Aqualung", "i300C" },
|
|
|
|
{ 0x4649, "Aqualung", "i200C" },
|
2022-06-14 17:28:29 +00:00
|
|
|
{ 0x4749, "Aqualung", "i200Cv2" },
|
2021-01-23 22:01:51 +00:00
|
|
|
{ 0x4651, "Aqualung", "i770R" },
|
|
|
|
{ 0x4652, "Aqualung", "i550C" },
|
|
|
|
{ 0x4653, "Oceanic", "Geo 4.0" },
|
|
|
|
{ 0x4654, "Oceanic", "Veo 4.0" },
|
|
|
|
{ 0x4655, "Sherwood", "Wisdom 4" },
|
|
|
|
{ 0x4656, "Oceanic", "Pro Plus 4" },
|
|
|
|
{ 0x4743, "Aqualung", "i470TC" }
|
|
|
|
};
|
|
|
|
|
2021-01-23 22:26:54 +00:00
|
|
|
struct namePattern {
|
|
|
|
const char *prefix;
|
|
|
|
const char *vendor;
|
|
|
|
const char *product;
|
|
|
|
};
|
2022-06-14 18:15:53 +00:00
|
|
|
// search is in order of this array, and as a prefix search, so more specific names
|
|
|
|
// should be added before less specific names (i.e. "Perdix 2" before "Perdix")
|
2021-01-23 22:26:54 +00:00
|
|
|
static struct namePattern name[] = {
|
|
|
|
// Shearwater dive computers
|
|
|
|
{ "Predator", "Shearwater", "Predator" },
|
2022-06-14 18:15:53 +00:00
|
|
|
{ "Perdix 2", "Shearwater", "Perdix 2"},
|
|
|
|
{ "Petrel 3", "Shearwater", "Petrel 3"},
|
2021-01-23 22:26:54 +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
|
|
|
|
{ "Petrel", "Shearwater", "Petrel 2" },
|
|
|
|
{ "Perdix", "Shearwater", "Perdix" },
|
|
|
|
{ "Teric", "Shearwater", "Teric" },
|
|
|
|
{ "Peregrine", "Shearwater", "Peregrine" },
|
|
|
|
{ "NERD 2", "Shearwater", "NERD 2" },
|
|
|
|
{ "NERD", "Shearwater", "NERD" }, // order is important, test for the more specific one first
|
|
|
|
{ "Predator", "Shearwater", "Predator" },
|
|
|
|
// Suunto dive computers
|
|
|
|
{ "EON Steel", "Suunto", "EON Steel" },
|
|
|
|
{ "EON Core", "Suunto", "EON Core" },
|
|
|
|
{ "Suunto D5", "Suunto", "D5" },
|
|
|
|
// Scubapro dive computers
|
|
|
|
{ "G2", "Scubapro", "G2" },
|
|
|
|
{ "HUD", "Scubapro", "G2 HUD" },
|
|
|
|
{ "Aladin", "Scubapro", "Aladin Sport Matrix" },
|
|
|
|
{ "A1", "Scubapro", "Aladin A1" },
|
2021-07-19 23:43:14 +00:00
|
|
|
{ "A2", "Scubapro", "Aladin A2" },
|
2021-01-23 22:26:54 +00:00
|
|
|
// Mares dive computers
|
|
|
|
{ "Mares Genius", "Mares", "Genius" },
|
|
|
|
{ "Mares", "Mares", "Quad" }, // we actually don't know and just pick a common one - user needs to fix in UI
|
|
|
|
// Cress dive computers
|
|
|
|
{ "CARESIO_", "Cressi", "Cartesio" },
|
|
|
|
{ "GOA_", "Cressi", "Goa" },
|
|
|
|
// Deepblu dive computesr
|
|
|
|
{ "COSMIQ", "Deepblu", "Cosmiq+" },
|
|
|
|
// Oceans dive computers
|
|
|
|
{ "S1", "Oceans", "S1" },
|
|
|
|
// McLean dive computers
|
|
|
|
{ "McLean Extreme", "McLean", "Extreme" },
|
|
|
|
// Tecdiving dive computers
|
|
|
|
{ "DiveComputer", "Tecdiving", "DiveComputer.eu" }
|
|
|
|
};
|
|
|
|
|
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.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
|
2021-01-10 18:25:52 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^IX5M\\d{6}"))) {
|
|
|
|
// The 2021 iX3M models (square buttons) report as iX5M,
|
|
|
|
// eventhough the physical model states iX3M.
|
|
|
|
vendor = "Ratio";
|
|
|
|
product = "iX3M GPS Easy"; // we don't know which of the GPS models, so set one
|
2021-01-23 22:26:54 +00:00
|
|
|
} else if (btName.contains(QRegularExpression("^[A-Z]{2}\\d{6}"))) {
|
|
|
|
// try the Pelagic/Aqualung name patterns
|
2021-01-23 22:01:51 +00:00
|
|
|
// the source of truth for this data is in libdivecomputer/src/descriptor.c
|
|
|
|
// we'd prefer to use the filter functions there but current design makes that really challenging
|
|
|
|
// The Pelagic dive computers (generally branded as Oceanic, Aqualung, or Sherwood)
|
|
|
|
// 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).
|
|
|
|
for (uint16_t i = 0; i < sizeof(model) / sizeof(struct modelPattern); i++) {
|
|
|
|
QString pattern = QString("^%1%2\\d{6}$").arg(QChar(model[i].model >> 8)).arg(QChar(model[i].model & 0xFF));
|
|
|
|
if (btName.contains(QRegularExpression(pattern))) {
|
|
|
|
vendor = model[i].vendor;
|
|
|
|
product = model[i].product;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 22:26:54 +00:00
|
|
|
} else { // finally try all the string prefix based ones
|
|
|
|
for (uint16_t i = 0; i < sizeof(name) / sizeof(struct namePattern); i++) {
|
|
|
|
if (btName.startsWith(name[i].prefix)) {
|
|
|
|
vendor = name[i].vendor;
|
|
|
|
product = name[i].product;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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);
|
2022-02-10 00:13:20 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred,
|
|
|
|
#else
|
2020-09-29 21:50:05 +00:00
|
|
|
connect(discoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error),
|
2022-02-10 00:13:20 +00:00
|
|
|
#endif
|
2020-09-29 21:50:05 +00:00
|
|
|
[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;
|
|
|
|
|
2021-01-13 22:33:12 +00:00
|
|
|
QString msg;
|
|
|
|
msg = QString("%1 device: '%2' [%3]: ").arg(fromPaired ? "Paired" : "Discovered new").arg(newDevice).arg(device.address);
|
2017-07-17 19:40:30 +00:00
|
|
|
if (newDC) {
|
|
|
|
QString vendor = dc_descriptor_get_vendor(newDC);
|
2021-01-13 22:33:12 +00:00
|
|
|
qDebug() << msg << "this could be a " + vendor;
|
2017-07-17 19:40:30 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-01-13 22:33:12 +00:00
|
|
|
qDebug() << msg << "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
|