2017-06-05 19:41:57 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "btdiscovery.h"
|
|
|
|
#include "downloadfromdcthread.h"
|
2017-06-10 14:22:28 +02:00
|
|
|
#include "core/libdivecomputer.h"
|
2017-06-05 19:41:57 -07:00
|
|
|
#include <QDebug>
|
|
|
|
|
2017-06-10 14:22:28 +02:00
|
|
|
extern QMap<QString, dc_descriptor_t *> descriptorLookup;
|
|
|
|
|
2017-06-05 19:41:57 -07:00
|
|
|
BTDiscovery *BTDiscovery::m_instance = NULL;
|
|
|
|
|
2017-06-10 14:22:28 +02: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";
|
|
|
|
if (btName.mid(4,2) == "3#") product = "OSTC 3";
|
|
|
|
else if (btName.mid(4,2) == "3+") product = "OSTC 3+";
|
|
|
|
else if (btName.mid(4,2) == "s#") product = "OSTC Sport";
|
|
|
|
else if (btName.mid(4,2) == "4-") product = "OSTC 4";
|
|
|
|
else if (btName.mid(4,2) == "2-") product = "OSTC 2N";
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vendor.isEmpty() && !product.isEmpty())
|
|
|
|
return(descriptorLookup[vendor + product]);
|
|
|
|
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2017-06-05 19:41:57 -07:00
|
|
|
BTDiscovery::BTDiscovery(QObject *parent)
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
if (m_instance) {
|
|
|
|
qDebug() << "trying to create an additional BTDiscovery object";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_instance = this;
|
|
|
|
#if defined(BT_SUPPORT)
|
|
|
|
if (localBtDevice.isValid() &&
|
|
|
|
localBtDevice.hostMode() == QBluetoothLocalDevice::HostConnectable) {
|
|
|
|
btPairedDevices.clear();
|
|
|
|
qDebug() << "localDevice " + localBtDevice.name() + " is valid, starting discovery";
|
Mobile: do not BT Discover on Android (Q_OS_ANDROID vs Q_OS_LINUX)
This seems a very trivial commit, but it is not. It appears that on an Android
build, with defined(Q_OS_ANDROID) the Q_OS_LINUX variable is also defined.
This results in a very tricky discovery process: 1) the JNI stuff pulls the paired
devices from the local BT controller, and 2) The QT discovry agent gets active
BT devices. 1) is a static list, that is, not dependent on actual
visual/discoverable BT devices; it is just cached data from the phone. 2) On
Android, this results in a list of actively visible (paired and not paired)
devices. On desktop, however (with QT/bluez BT stack) the QT discovery agent
just gets the list of paired devices, so more or less equivalent to the situation
described under 1) for Android.
Ok, a long story, but just do not do a discovery on Android at all. Basically,
we need the BT address, device name, and possibly a specific SPP service UUID. This are
fixed and known for HW and Shearwater at this point, so there is no need for a
(lengthy) discovery process, and making sure the the dive computer is discoverable
at the moment the app wants to construct its data to show in the UI. So, the
static list of paired devices is all we need.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 09:36:26 +02:00
|
|
|
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
2017-06-05 19:41:57 -07:00
|
|
|
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
|
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BTDiscovery::btDeviceDiscovered);
|
|
|
|
discoveryAgent->start();
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_ANDROID) && defined(BT_SUPPORT)
|
|
|
|
getBluetoothDevices();
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < btPairedDevices.length(); i++) {
|
|
|
|
qDebug() << "Paired =" << btPairedDevices[i].name << btPairedDevices[i].address.toString();
|
|
|
|
}
|
Mobile: do not BT Discover on Android (Q_OS_ANDROID vs Q_OS_LINUX)
This seems a very trivial commit, but it is not. It appears that on an Android
build, with defined(Q_OS_ANDROID) the Q_OS_LINUX variable is also defined.
This results in a very tricky discovery process: 1) the JNI stuff pulls the paired
devices from the local BT controller, and 2) The QT discovry agent gets active
BT devices. 1) is a static list, that is, not dependent on actual
visual/discoverable BT devices; it is just cached data from the phone. 2) On
Android, this results in a list of actively visible (paired and not paired)
devices. On desktop, however (with QT/bluez BT stack) the QT discovery agent
just gets the list of paired devices, so more or less equivalent to the situation
described under 1) for Android.
Ok, a long story, but just do not do a discovery on Android at all. Basically,
we need the BT address, device name, and possibly a specific SPP service UUID. This are
fixed and known for HW and Shearwater at this point, so there is no need for a
(lengthy) discovery process, and making sure the the dive computer is discoverable
at the moment the app wants to construct its data to show in the UI. So, the
static list of paired devices is all we need.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 09:36:26 +02:00
|
|
|
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
2017-06-05 19:41:57 -07:00
|
|
|
discoveryAgent->stop();
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
qDebug() << "localBtDevice isn't valid";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device)
|
|
|
|
{
|
|
|
|
btPairedDevice this_d;
|
|
|
|
this_d.address = device.address();
|
|
|
|
this_d.name = device.name();
|
|
|
|
btPairedDevices.append(this_d);
|
2017-06-10 10:09:56 +02:00
|
|
|
struct btVendorProduct btVP;
|
2017-06-05 19:41:57 -07:00
|
|
|
|
2017-06-10 14:22:28 +02:00
|
|
|
QString newDevice;
|
|
|
|
dc_descriptor_t *newDC = getDeviceType(device.name());
|
|
|
|
if (newDC)
|
|
|
|
newDevice = dc_descriptor_get_product(newDC);
|
|
|
|
else
|
|
|
|
newDevice = device.name();
|
2017-06-05 19:41:57 -07:00
|
|
|
|
|
|
|
QList<QBluetoothUuid> serviceUuids = device.serviceUuids();
|
|
|
|
foreach (QBluetoothUuid id, serviceUuids) {
|
|
|
|
addBtUuid(id);
|
|
|
|
qDebug() << id.toByteArray();
|
|
|
|
}
|
2017-06-10 10:09:56 +02:00
|
|
|
qDebug() << "Found new device:" << newDevice << device.address();
|
|
|
|
QString vendor;
|
2017-06-10 14:22:28 +02:00
|
|
|
if (newDC) foreach (vendor, productList.keys()) {
|
2017-06-05 19:41:57 -07:00
|
|
|
if (productList[vendor].contains(newDevice)) {
|
|
|
|
qDebug() << "this could be a " + vendor + " " +
|
|
|
|
(newDevice == "OSTC 3" ? "OSTC family" : newDevice);
|
|
|
|
btVP.btdi = device;
|
2017-06-10 14:22:28 +02:00
|
|
|
btVP.dcDescriptor = newDC;
|
2017-06-05 19:41:57 -07:00
|
|
|
btVP.vendorIdx = vendorList.indexOf(vendor);
|
|
|
|
btVP.productIdx = productList[vendor].indexOf(newDevice);
|
2017-06-10 10:09:56 +02:00
|
|
|
qDebug() << "adding new btDCs entry (detected DC)" << newDevice << btVP.vendorIdx << btVP.productIdx << btVP.btdi.address();;
|
2017-06-05 19:41:57 -07:00
|
|
|
btDCs << btVP;
|
2017-06-10 10:09:56 +02:00
|
|
|
break;
|
2017-06-05 19:41:57 -07:00
|
|
|
}
|
|
|
|
}
|
2017-06-12 10:05:55 -07:00
|
|
|
productList[QObject::tr("Paired Bluetooth Devices")].append(this_d.name + " (" + this_d.address.toString() + ")");
|
2017-06-10 10:09:56 +02:00
|
|
|
|
|
|
|
btVP.btdi = device;
|
2017-06-10 14:22:28 +02:00
|
|
|
btVP.dcDescriptor = newDC;
|
2017-06-10 10:09:56 +02:00
|
|
|
btVP.vendorIdx = vendorList.indexOf(QObject::tr("Paired Bluetooth Devices"));
|
|
|
|
btVP.productIdx = productList[QObject::tr("Paired Bluetooth Devices")].indexOf(this_d.name);
|
|
|
|
qDebug() << "adding new btDCs entry (all paired)" << newDevice << btVP.vendorIdx << btVP.productIdx << btVP.btdi.address();
|
|
|
|
btAllDevices << btVP;
|
2017-06-05 19:41:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
QList <struct btVendorProduct> BTDiscovery::getBtDcs()
|
|
|
|
{
|
|
|
|
return btDCs;
|
|
|
|
}
|
|
|
|
|
2017-06-10 10:09:56 +02:00
|
|
|
QList <struct btVendorProduct> BTDiscovery::getBtAllDevices()
|
|
|
|
{
|
|
|
|
return btAllDevices;
|
|
|
|
}
|
2017-06-05 19:41:57 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.address = QBluetoothAddress(dev.callObjectMethod("getAddress","()Ljava/lang/String;").toString());
|
|
|
|
result.name = dev.callObjectMethod("getName", "()Ljava/lang/String;").toString();
|
|
|
|
|
|
|
|
btPairedDevices.append(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 14:22:28 +02:00
|
|
|
|
2017-06-05 19:41:57 -07:00
|
|
|
#endif // BT_SUPPORT
|