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;
|
|
|
|
}
|
2017-06-06 02:41:57 +00:00
|
|
|
BTDiscovery *BTDiscovery::m_instance = NULL;
|
|
|
|
|
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";
|
2017-06-10 12:22:28 +00:00
|
|
|
// all OSTCs are HW_FAMILY_OSTC_3, so when we do not know,
|
|
|
|
// just try this
|
|
|
|
else product = "OSTC 3"; // all OSTCs are HW_FAMILY_OSTC_3
|
|
|
|
}
|
|
|
|
|
2018-09-09 00:54:25 +00:00
|
|
|
if (btName.startsWith("Predator") ||
|
2018-09-30 05:39:07 +00:00
|
|
|
btName.startsWith("Petrel") ||
|
|
|
|
btName.startsWith("Perdix") ||
|
2018-09-09 00:54:25 +00:00
|
|
|
btName.startsWith("Teric")) {
|
2017-06-12 18:29:19 +00:00
|
|
|
vendor = "Shearwater";
|
|
|
|
if (btName.startsWith("Petrel")) product = "Petrel"; // or petrel 2?
|
|
|
|
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";
|
2017-06-12 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 04:29:34 +00:00
|
|
|
if (btName.startsWith("EON Steel")) {
|
|
|
|
vendor = "Suunto";
|
|
|
|
product = "EON Steel";
|
|
|
|
}
|
|
|
|
|
2018-05-12 05:18:16 +00:00
|
|
|
if (btName.startsWith("EON Core")) {
|
|
|
|
vendor = "Suunto";
|
|
|
|
product = "EON Core";
|
|
|
|
}
|
|
|
|
|
2017-11-05 10:47:36 +00:00
|
|
|
if (btName.startsWith("G2") || btName.startsWith("Aladin")) {
|
2017-07-03 01:31:40 +00:00
|
|
|
vendor = "Scubapro";
|
2017-11-05 10:47:36 +00:00
|
|
|
if (btName.startsWith("G2")) product = "G2";
|
|
|
|
if (btName.startsWith("Aladin")) product = "Aladin Sport Matrix";
|
2017-07-03 01:31:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 05:18:16 +00:00
|
|
|
if (btName == "Mares bluelink pro") {
|
|
|
|
vendor = "Mares";
|
|
|
|
// we don't know which of the dive computers it is,
|
|
|
|
// so let's just randomly pick one
|
|
|
|
product = "Quad";
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:10:03 +00:00
|
|
|
if (btName.contains(QRegularExpression("^FQ\\d{6}$"))) {
|
|
|
|
vendor = "Aqualung";
|
|
|
|
product = "i770R";
|
|
|
|
}
|
|
|
|
|
2018-12-21 03:03:09 +00:00
|
|
|
if (btName.contains(QRegularExpression("^ER\\d{6}$"))) {
|
|
|
|
vendor = "Oceanic";
|
|
|
|
product = "Pro Plus X";
|
|
|
|
}
|
|
|
|
|
2017-06-10 12:22:28 +00:00
|
|
|
if (!vendor.isEmpty() && !product.isEmpty())
|
2018-02-17 20:21:16 +00:00
|
|
|
return descriptorLookup.value(vendor + product);
|
2017-06-10 12:22:28 +00:00
|
|
|
|
2018-02-17 20:21:16 +00:00
|
|
|
return NULL;
|
2017-06-10 12:22:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:36:04 +00:00
|
|
|
BTDiscovery::BTDiscovery(QObject*) : m_btValid(false),
|
2018-09-25 00:28:55 +00:00
|
|
|
discoveryAgent(nullptr)
|
2017-06-06 02:41:57 +00:00
|
|
|
{
|
|
|
|
if (m_instance) {
|
|
|
|
qDebug() << "trying to create an additional BTDiscovery object";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_instance = this;
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTDiscovery::BTDiscoveryReDiscover()
|
|
|
|
{
|
2017-07-18 21:49:14 +00:00
|
|
|
#if !defined(Q_OS_IOS)
|
2017-06-06 02:41:57 +00:00
|
|
|
if (localBtDevice.isValid() &&
|
|
|
|
localBtDevice.hostMode() == QBluetoothLocalDevice::HostConnectable) {
|
|
|
|
btPairedDevices.clear();
|
|
|
|
qDebug() << "localDevice " + localBtDevice.name() + " is valid, 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;
|
2018-09-25 00:28:55 +00:00
|
|
|
#if !defined(Q_OS_ANDROID)
|
|
|
|
if (discoveryAgent == nullptr) {
|
|
|
|
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
|
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BTDiscovery::btDeviceDiscovered);
|
|
|
|
}
|
2017-07-18 21:49:14 +00:00
|
|
|
qDebug() << "starting BLE discovery";
|
2017-06-06 02:41:57 +00:00
|
|
|
discoveryAgent->start();
|
2018-09-25 00:28:55 +00:00
|
|
|
#else
|
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++)
|
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
|
|
|
btDeviceDiscoveredMain(btPairedDevices[i]);
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif
|
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;
|
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()
|
|
|
|
{
|
|
|
|
m_instance = NULL;
|
|
|
|
#if defined(BT_SUPPORT)
|
|
|
|
free(discoveryAgent);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BTDiscovery *BTDiscovery::instance()
|
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new BTDiscovery();
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
QList<QBluetoothUuid> serviceUuids = device.serviceUuids();
|
|
|
|
foreach (QBluetoothUuid id, serviceUuids) {
|
|
|
|
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
|
|
|
|
|
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
|
|
|
btDeviceDiscoveredMain(this_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTDiscovery::btDeviceDiscoveredMain(const btPairedDevice &device)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
qDebug() << "Found 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;
|
|
|
|
}
|
2017-07-17 04:49:11 +00:00
|
|
|
connectionListModel.addAddress(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)
|
|
|
|
{
|
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:56:56 +00:00
|
|
|
#if defined(Q_OS_MACOS)
|
|
|
|
// macOS appears to need a fresh scan if we want to switch devices
|
|
|
|
static QString lastAddress;
|
|
|
|
if (lastAddress != address) {
|
|
|
|
btDeviceInfo.clear();
|
|
|
|
discoveryAgent->stop();
|
|
|
|
lastAddress = address;
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
if (btDeviceInfo.contains(devaddr))
|
|
|
|
return btDeviceInfo[devaddr];
|
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 {
|
|
|
|
if (btDeviceInfo.keys().contains(devaddr))
|
|
|
|
return btDeviceInfo[devaddr];
|
|
|
|
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
|