Index: sources/bluetooth/BluetoothInterface.cpp =================================================================== diff -u -rc9f8f8cf3c6c37fc6460d8675c62c9442c4d4263 -r9dd6d10ac1164c651e8d536673641d1e90981207 --- sources/bluetooth/BluetoothInterface.cpp (.../BluetoothInterface.cpp) (revision c9f8f8cf3c6c37fc6460d8675c62c9442c4d4263) +++ sources/bluetooth/BluetoothInterface.cpp (.../BluetoothInterface.cpp) (revision 9dd6d10ac1164c651e8d536673641d1e90981207) @@ -1,4 +1,4 @@ -/*! +/*! * * Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. * \copyright @@ -413,6 +413,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 +426,8 @@ */ void BluetoothInterface::ondoScan() { - startScan(); + // Re-scanning, opting to unpair to last device + startScan(true); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -476,7 +481,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 +541,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 +608,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 +828,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 +841,11 @@ } NOTIFY_SCAN_START - quitDevice(); + + if (vQuitDevice) { + quitDevice(); + } + _agent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); return true; }