161 lines
4.8 KiB
C++
161 lines
4.8 KiB
C++
/***************************************************************************
|
|
|
|
source::worx raDIYo
|
|
Copyright © 2020-2022 c.holzheuer
|
|
chris@sourceworx.org
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
#include <QDebug>
|
|
#include <swbluetoothcontrol.h>
|
|
|
|
#include <QBluetoothLocalDevice>
|
|
#include <QBluetoothAddress>
|
|
|
|
using QBTState = QBluetoothLocalDevice::Pairing;
|
|
using QBTLocDev = QBluetoothLocalDevice;
|
|
using QBTAgent = QBluetoothDeviceDiscoveryAgent;
|
|
using QBTDevInfo = QBluetoothDeviceInfo;
|
|
|
|
SWBlueToothControl::SWBlueToothControl( QWidget* parent, QSettings* settings )
|
|
: SWListControl( parent, settings )
|
|
{
|
|
|
|
Ui_SWBlueToothControl::setupUi( this );
|
|
setHeaderText( "Bluetooth " );
|
|
_contentView->resize( 790, 130 );
|
|
|
|
_localDevice.powerOn();
|
|
_localDevice.setHostMode( QBTLocDev::HostDiscoverable );
|
|
|
|
/*
|
|
// Get connected devices
|
|
QList<QBluetoothAddress> remotes;
|
|
remotes = _localDevice.connectedDevices();
|
|
for( const auto& entry : remotes )
|
|
qDebug() << "remote entry: " << entry;
|
|
*/
|
|
|
|
//_discoveryAgent.setInquiryType( QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry );
|
|
|
|
connect( _buttonScan, &QPushButton::toggled, this,
|
|
[=](bool enabled )
|
|
{
|
|
_buttonScan->setChecked( enabled );
|
|
//_buttonScan->setEnabled( enabled );
|
|
enabled ? _discoveryAgent.start() : _discoveryAgent.stop();
|
|
_buttonScan->setText( enabled ? "scanning..." : "start scan" );
|
|
} );
|
|
|
|
connect( &_discoveryAgent, &QBTAgent::finished, this,
|
|
[=]
|
|
{
|
|
_buttonScan->setChecked( false );
|
|
//_buttonScan->setEnabled( true );
|
|
_buttonScan->setText( "start scan" );
|
|
} );
|
|
|
|
connect( &_discoveryAgent, SIGNAL( deviceDiscovered(QBluetoothDeviceInfo) ),
|
|
this, SLOT( addDevice(QBluetoothDeviceInfo) )
|
|
);
|
|
|
|
connect( _contentView, SIGNAL( itemActivated(QListWidgetItem*) ),
|
|
this, SLOT( onItemActivated(QListWidgetItem*) )
|
|
);
|
|
|
|
connect( &_localDevice, SIGNAL( pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing)),
|
|
this, SLOT(pairingDone(QBluetoothAddress,QBluetoothLocalDevice::Pairing)));
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Wird beim Einbblenden des Controls automatisch
|
|
* aufgerufen. Überladene Funktion, inm diesem Fall ein Dummy
|
|
* ohne Funktion.
|
|
*/
|
|
|
|
void SWBlueToothControl::onShow()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Wird beim Ausblenden des Controls automatisch aufgerufen.
|
|
* Überladene Funktion, stoppt in diesem Fall einen eventuell
|
|
* noch aktiven Scan-Process.
|
|
*/
|
|
|
|
void SWBlueToothControl::onHide()
|
|
{
|
|
_discoveryAgent.stop();
|
|
}
|
|
|
|
|
|
/**
|
|
* Fügt ein BlueTooth-Device in die Device-Liste hinzu.
|
|
*/
|
|
|
|
void SWBlueToothControl::addDevice( const QBluetoothDeviceInfo& info )
|
|
{
|
|
|
|
QString label = info.name(); //QString("%1 %2").arg(info.address().toString()).arg(info.name());
|
|
QList<QListWidgetItem*> items = _contentView->findItems( label, Qt::MatchExactly );
|
|
if( !items.empty() )
|
|
return;
|
|
|
|
QListWidgetItem& item = createItem( label, info.address().toString() );
|
|
QBTState pairingStatus = _localDevice.pairingStatus( info.address() );
|
|
bool paired = ( pairingStatus == QBTLocDev::Paired || pairingStatus == QBTLocDev::AuthorizedPaired );
|
|
QColor bg = paired ? Qt::green : Qt::white ;
|
|
item.setForeground( bg );
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Überladene Funktion, wird ein beim aktivieren eines Eintrag der Device-Liste
|
|
* aufgerufen und bindet das Device als Soud-Sink ein.
|
|
*/
|
|
|
|
void SWBlueToothControl::onItemActivated( QListWidgetItem* item )
|
|
{
|
|
|
|
QString addrStr= item->data( SWListControlRole ).toString();
|
|
qDebug() << "BT Pair device: " << addrStr;
|
|
QBluetoothAddress address( addrStr );
|
|
|
|
QBTState pairingStatus = _localDevice.pairingStatus( address );
|
|
bool paired = ( pairingStatus == QBTState::Paired || pairingStatus == QBTState::AuthorizedPaired );
|
|
QBTState newState = paired ? QBTState::Paired : QBTState::Unpaired;
|
|
_localDevice.requestPairing( address, newState );
|
|
}
|
|
|
|
|
|
/**
|
|
* SIGNAL, wird nach dem Erfolgreichen Einbinden eines BlueTooth Device aufgerufen.
|
|
* Passt die Listenabzeige der Devices entsprechend der Verbindungszustands der Devices
|
|
* an.
|
|
*/
|
|
|
|
void SWBlueToothControl::pairingDone( const QBluetoothAddress &address, QBTState pairing )
|
|
{
|
|
|
|
QList<QListWidgetItem*> items = _contentView->findItems( address.toString(), Qt::MatchContains );
|
|
QColor foreground = Qt::red;
|
|
if( pairing == QBTLocDev::Paired || pairing == QBTLocDev::AuthorizedPaired )
|
|
foreground = Qt::green;
|
|
|
|
for (int var = 0; var < items.count(); ++var)
|
|
items.at(var)->setForeground( foreground );
|
|
|
|
}
|
|
|