Index: sources/view/VBluetooth.cpp =================================================================== diff -u -rd00fa04f905b2abdac357a850d737a5f9d5e38df -r5600d2133dd0ea6dc1f733aa78bd26e2a4892a38 --- sources/view/VBluetooth.cpp (.../VBluetooth.cpp) (revision d00fa04f905b2abdac357a850d737a5f9d5e38df) +++ sources/view/VBluetooth.cpp (.../VBluetooth.cpp) (revision 5600d2133dd0ea6dc1f733aa78bd26e2a4892a38) @@ -42,8 +42,8 @@ this, SLOT(onDisconnectedFromDevice(QBluetoothDeviceInfo))); // outgoing - connect(this, SIGNAL(didSelectDevice(const QString)), - &_BLEScanner, SLOT(doSelectDevice(const QString))); + connect(this, SIGNAL(didSelectDevice(const QString, const QString)), + &_BLEScanner, SLOT(doSelectDevice(const QString, const QString))); connect(this, SIGNAL(didRequestScanForDevices()), &_BLEScanner, SLOT(doScanForDevices())); @@ -66,6 +66,7 @@ void VBluetooth::doScanForDevices() { notifyStatusUpdate(tr("Scanning...")); + scanInProgress(true); _unpairedDevices.clear(); emit didDevicesChanged(); emit didRequestScanForDevices(); @@ -79,6 +80,7 @@ */ void VBluetooth::onScanForDevicesError(QBluetoothDeviceDiscoveryAgent::Error error) { + scanInProgress(false); switch (error) { case QBluetoothDeviceDiscoveryAgent::PoweredOffError: notifyStatusUpdate(tr("Error: Bluetooth module is powered off.")); @@ -109,6 +111,7 @@ */ void VBluetooth::onScanForDevicesFinished() { + scanInProgress(false); emit didScanFinished(); notifyStatusUpdate(tr("Scan Finished.")); } @@ -137,18 +140,20 @@ * \brief VBluetooth::onSelectedDevice * Emits a signal that the device was selected * \param addr - the selected BLE mac address + * \param name - the selected BLE device name */ -void VBluetooth::doSelectDevice(const QString &addr) +void VBluetooth::doSelectDevice(const QString &addr, const QString &name) { - for (QObject *device : _unpairedDevices) { - VBluetoothDeviceInfo *d = static_cast(device); - if (addr == d->getAddress()) - { - delete _lastSelectedDevice; - _lastSelectedDevice = new VBluetoothDeviceInfo(addr, d->getName()); - emit didSelectDevice(addr); - } + if (_lastSelectedDevice != nullptr && _lastSelectedDevice->getAddress() != addr) + { + QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(QBluetoothAddress(addr), name, + QBluetoothDeviceInfo::HealthBloodPressureMonitor); + onDisconnectedFromDevice(deviceInfo); } + + delete _lastSelectedDevice; + _lastSelectedDevice = new VBluetoothDeviceInfo(addr, name); + emit didSelectDevice(addr, name); } /*! @@ -334,6 +339,8 @@ */ void VBluetooth::onLoadAndConnectToLastSelectedDevice(const QJsonObject &obj) { + // TODO: Use QSettings instead of QJsonObject + // TODO: Use QSettings::IniFormat and don't set the path so it is loaded from the application folder if (obj.keys().contains(_lastSelectedDeviceAddrKey)) { QJsonObject lastSelectDeviceObj = obj.value(_lastSelectedDeviceAddrKey).toObject(); if (!lastSelectDeviceObj.isEmpty()) { @@ -354,9 +361,11 @@ */ void VBluetooth::onLoadMyDevices(const QString &path) { + // TODO: Use QSettings instead of QJsonObject + // TODO: Use QSettings::IniFormat and don't set the path so it is loaded from the application folder _pairedDevices.clear(); QJsonObject obj; - if (!FileHandler::readJSON(path, obj)) + if (!FileHandler::read(path, obj)) { LOG_EVENT(tr("Could not load my bluetooth devices from %1").arg(path)); return;