Index: sources/bluetooth/BluetoothInterface.cpp =================================================================== diff -u -ra5c75192af33a9284e4b4886df0c337ba6fcc9f9 -r2ef03b2ce51b4dc507f66e9671953a8e0824bde9 --- sources/bluetooth/BluetoothInterface.cpp (.../BluetoothInterface.cpp) (revision a5c75192af33a9284e4b4886df0c337ba6fcc9f9) +++ sources/bluetooth/BluetoothInterface.cpp (.../BluetoothInterface.cpp) (revision 2ef03b2ce51b4dc507f66e9671953a8e0824bde9) @@ -1,13 +1,13 @@ /*! * - * Copyright (c) 2021-2022 Diality Inc. - All Rights Reserved. + * Copyright (c) 2021-2024 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file BluetoothInterface.cpp - * \author (last) Behrouz NematiPour - * \date (last) 13-Oct-2022 + * \author (last) Vy + * \date (last) 05-Sep-2023 * \author (original) Behrouz NematiPour * \date (original) 24-Aug-2021 * @@ -183,7 +183,8 @@ _device->remoteName(), _tempBatt )); #define NOTIFY_DEVICE_CONNECT notifyStateChange(MBluetooth(MBluetooth::eIS_Device_Connect , \ _device->remoteAddress().toString() , \ - _device->remoteName() )); + _device->remoteName(), 0 , \ + _local->pairingStatus(_device->remoteAddress()) )); #define NOTIFY_DEVICE_DONE notifyStateChange(MBluetooth(MBluetooth::eIS_Device_Done , \ _device->remoteAddress().toString() , \ _device->remoteName() )); @@ -193,7 +194,8 @@ 0,0, vError, false )); #define NOTIFY_DEVICE_DISCONNECT notifyStateChange(MBluetooth(MBluetooth::eIS_Device_Disconnect , \ _device->remoteAddress().toString() , \ - _device->remoteName() )); + _device->remoteName(), 0 , \ + _local->pairingStatus(_device->remoteAddress()) )); // ~~~~~~~~~~ Service #define NOTIFY_SERVICE_START notifyStateChange(MBluetooth(MBluetooth::eIS_Service_Start )); #define NOTIFY_SERVICE_DISCOVER notifyStateChange(MBluetooth(MBluetooth::eIS_Service_Discover )); @@ -413,6 +415,10 @@ initConnections(); NOTIFY_LOCAL_INIT NOTIFY_IDLE + + // The initial scan to populate the bt device list + // opting to not quitDevice to hold previous paired status + startScan(false); } /*! @@ -422,7 +428,8 @@ */ void BluetoothInterface::ondoScan() { - startScan(); + // Re-scanning, opting to unpair to last device + startScan(true); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -476,7 +483,18 @@ BluetoothDeviceData data; data.addr = vInfo.address().toString(); data.name = vInfo.name(); - data.pair = _local->pairingStatus(vInfo.address()); + + bool isPaired = _local->pairingStatus(vInfo.address()) == QBluetoothLocalDevice::Paired; + bool isAuthPaired = _local->pairingStatus(vInfo.address()) == QBluetoothLocalDevice::AuthorizedPaired; + + data.pair = isPaired || isAuthPaired; + + if ( data.pair ) { + // If a previously paired device is found, sdo initial connecting to it + _temp = QBluetoothDeviceInfo(vInfo.address(), vInfo.name(), QBluetoothDeviceInfo::HealthBloodPressureMonitor); + connectToDevice(); + } + emit didDeviceChange(data); NOTIFY_SCAN_FOUND } @@ -525,6 +543,18 @@ */ void BluetoothInterface::onDeviceSelect(const BluetoothDeviceData &vDevice) { stopScan(); + + if ( isLocalValid() && isDeviceValid() ){ + // Check pair status, if connected to a bt device already, disconnect before connecting to selected + // bt device to avoid "device connection error" + bool isPaired = _local->pairingStatus(_device->remoteAddress()) == QBluetoothLocalDevice::Paired; + bool isAuthPaired = _local->pairingStatus(_device->remoteAddress()) == QBluetoothLocalDevice::AuthorizedPaired; + if ( isPaired || isAuthPaired ) { + quitDevice(); + } + } + + // Proceed to connect to selected device _temp = QBluetoothDeviceInfo(QBluetoothAddress(vDevice.addr), vDevice.name, QBluetoothDeviceInfo::HealthBloodPressureMonitor); connectToDevice(); } @@ -580,6 +610,21 @@ */ void BluetoothInterface::onDeviceError(QLowEnergyController::Error vError) { + //DEBUG: qDebug()<< "Device Error " << vError; + bool isErrorUnknown = (vError == QLowEnergyController::UnknownError); + if ( isErrorUnknown ) { + // When polling to reconnect to the remote device, the application encounters an unknown + // error occasionally when the connection fails, but does not report it as a connection error + + bool isLocalAndDeviceValid = isLocalValid() && isDeviceValid(); + if ( isLocalAndDeviceValid && ( _local->pairingStatus(_device->remoteAddress()) != QBluetoothLocalDevice::Unpaired ) ) { + // Code is opting to set _reconnectionActive to true to allow polling to continue since the device + // is still paired and the error encountered is due to not being able to connect to the remote device. + _reconnectionActive = true; + //DEBUG: qDebug()<< "unknown error : reconnection set to true"; + } + } + // if the device is trying to connect to the previously paired device and the device was not on at this moment it should go to the reconnection state. if ( _tryingrepairActive ) _reconnectionActive = true; @@ -785,9 +830,10 @@ /*! * \brief BluetoothInterface::startScan * \details Stars the agent device scan + * \param vQuitDevice - true if quitting device, else scan only * \return true if the agent can successfully start the scan. */ -bool BluetoothInterface::startScan() +bool BluetoothInterface::startScan(bool vQuitDevice) { if ( ! isValid() ) return false; // POST failed. @@ -797,7 +843,11 @@ } NOTIFY_SCAN_START - quitDevice(); + + if (vQuitDevice) { + quitDevice(); + } + _agent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); return true; } @@ -1146,7 +1196,7 @@ { UIBloodPressureData data = vModel.data(); emit didActionReceive(data); - LOG_APPED(vModel.toString()); + LOG_APPED_UI(vModel.toString()); } /*!