Skip to content

Commit

Permalink
Avoid creating a bluetooth discovery agent until a scan is triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Apr 24, 2023
1 parent d8dc367 commit 3313e48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
35 changes: 22 additions & 13 deletions src/core/positioning/bluetoothdevicemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,49 @@

BluetoothDeviceModel::BluetoothDeviceModel( QObject *parent )
: QAbstractListModel( parent )
, mLocalDevice( std::make_unique<QBluetoothLocalDevice>() )
{
connect( &mServiceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, this, &BluetoothDeviceModel::serviceDiscovered );
}

void BluetoothDeviceModel::initiateDiscoveryAgent()
{
mLocalDevice = std::make_unique<QBluetoothLocalDevice>();
mServiceDiscoveryAgent = std::make_unique<QBluetoothServiceDiscoveryAgent>();

connect( mServiceDiscoveryAgent.get(), &QBluetoothServiceDiscoveryAgent::serviceDiscovered, this, &BluetoothDeviceModel::serviceDiscovered );
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
connect( &mServiceDiscoveryAgent, qOverload<QBluetoothServiceDiscoveryAgent::Error>( &QBluetoothServiceDiscoveryAgent::error ), this, [=]( QBluetoothServiceDiscoveryAgent::Error error ) {
connect( mServiceDiscoveryAgent.get(), qOverload<QBluetoothServiceDiscoveryAgent::Error>( &QBluetoothServiceDiscoveryAgent::error ), this, [=]( QBluetoothServiceDiscoveryAgent::Error error ) {
#else
connect( &mServiceDiscoveryAgent, qOverload<QBluetoothServiceDiscoveryAgent::Error>( &QBluetoothServiceDiscoveryAgent::errorOccurred ), this, [=]( QBluetoothServiceDiscoveryAgent::Error error ) {
connect( mServiceDiscoveryAgent.get(), qOverload<QBluetoothServiceDiscoveryAgent::Error>( &QBluetoothServiceDiscoveryAgent::errorOccurred ), this, [=]( QBluetoothServiceDiscoveryAgent::Error error ) {
#endif
if ( error != QBluetoothServiceDiscoveryAgent::NoError )
{
setLastError( mServiceDiscoveryAgent.errorString() );
setLastError( mServiceDiscoveryAgent->errorString() );
setScanningStatus( Failed );
}
} );
connect( &mServiceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::finished, [=]() {
setScanningStatus( mServiceDiscoveryAgent.error() == QBluetoothServiceDiscoveryAgent::NoError ? Succeeded : Failed );
connect( mServiceDiscoveryAgent.get(), &QBluetoothServiceDiscoveryAgent::finished, [=]() {
setScanningStatus( mServiceDiscoveryAgent->error() == QBluetoothServiceDiscoveryAgent::NoError ? Succeeded : Failed );
} );
connect( &mServiceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::canceled, [=]() {
connect( mServiceDiscoveryAgent.get(), &QBluetoothServiceDiscoveryAgent::canceled, [=]() {
setScanningStatus( Canceled );
} );
}

void BluetoothDeviceModel::startServiceDiscovery( const bool fullDiscovery )
{
if ( mServiceDiscoveryAgent.isActive() )
mServiceDiscoveryAgent.stop();
if ( !mServiceDiscoveryAgent )
initiateDiscoveryAgent();

if ( mServiceDiscoveryAgent->isActive() )
mServiceDiscoveryAgent->stop();

mServiceDiscoveryAgent.setUuidFilter( QBluetoothUuid( QBluetoothUuid::ServiceClassUuid::SerialPort ) );
mServiceDiscoveryAgent->setUuidFilter( QBluetoothUuid( QBluetoothUuid::ServiceClassUuid::SerialPort ) );
QBluetoothServiceDiscoveryAgent::DiscoveryMode discoveryMode = fullDiscovery ? QBluetoothServiceDiscoveryAgent::FullDiscovery : QBluetoothServiceDiscoveryAgent::MinimalDiscovery;

// set scanning status _prior to_ start as start itself can error and then we get a broken status sequence
setScanningStatus( Scanning );
mServiceDiscoveryAgent.clear();
mServiceDiscoveryAgent.start( discoveryMode );
mServiceDiscoveryAgent->clear();
mServiceDiscoveryAgent->start( discoveryMode );
}

void BluetoothDeviceModel::serviceDiscovered( const QBluetoothServiceInfo &service )
Expand Down
4 changes: 3 additions & 1 deletion src/core/positioning/bluetoothdevicemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ class BluetoothDeviceModel : public QAbstractListModel
void serviceDiscovered( const QBluetoothServiceInfo &service );

private:
void initiateDiscoveryAgent();

std::unique_ptr<QBluetoothLocalDevice> mLocalDevice;
QBluetoothServiceDiscoveryAgent mServiceDiscoveryAgent;
std::unique_ptr<QBluetoothServiceDiscoveryAgent> mServiceDiscoveryAgent;
QList<QPair<QString, QString>> mDiscoveredDevices;
ScanningStatus mScanningStatus = NoStatus;
QString mLastError;
Expand Down

1 comment on commit 3313e48

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.