Some more fixes in the BT discovery

pull/507/head
Michael Zanetti 2021-01-14 14:58:50 +01:00
parent e6c7e34f39
commit 6a6e60009d
4 changed files with 22 additions and 11 deletions

View File

@ -68,6 +68,11 @@ QVariant SortFilterProxyModel::data(int row, const QString &role) const
return QSortFilterProxyModel::data(index(row, 0), roleId);
}
int SortFilterProxyModel::mapToSource(int index) const
{
return QSortFilterProxyModel::mapToSource(this->index(index, 0)).row();
}
bool SortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (!m_filterList.isEmpty() && !m_filterRoleName.isEmpty()) {

View File

@ -27,6 +27,7 @@ public:
void setSortOrder(Qt::SortOrder sortOrder);
Q_INVOKABLE QVariant data(int row, const QString &role) const;
Q_INVOKABLE int mapToSource(int index) const;
signals:
void filterRoleNameChanged();

View File

@ -124,7 +124,7 @@ void BluetoothDiscovery::setDiscoveryEnabled(bool discoveryEnabled)
return;
}
m_discoveryEnabled = discoveryEnabled;
emit discoveringChanged();
emit discoveryEnabledChanged(m_discoveryEnabled);
if (m_discoveryEnabled) {
start();
@ -176,8 +176,7 @@ void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo
{
if (!deviceInfo.isValid()
|| !deviceInfo.coreConfigurations().testFlag(QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
|| deviceInfo.name().isEmpty()
|| deviceInfo.isCached()) {
|| deviceInfo.name().isEmpty()) {
return;
}
@ -235,7 +234,7 @@ void BluetoothDiscovery::start()
m_discoveryAgent->stop();
}
// m_deviceInfos->clearModel();
m_deviceInfos->clearModel();
qDebug() << "BluetoothDiscovery: Starting discovery.";
m_discoveryAgent->start();

View File

@ -30,7 +30,7 @@ BtWiFiSetup::BtWiFiSetup(QObject *parent) : QObject(parent)
void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device)
{
qDebug() << "device" << device;
qDebug() << "Connecting to device" << device->address() << device->name();
if (m_btController) {
delete m_btController;
m_currentConnection = nullptr;
@ -38,7 +38,6 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device)
m_accessPoints->clearModel();
m_bluetoothStatus = BluetoothStatusDisconnected;
emit bluetoothStatusChanged(m_bluetoothStatus);
}
m_btController = QLowEnergyController::createCentral(device->bluetoothDeviceInfo(), this);
@ -47,7 +46,7 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device)
m_btController->discoverServices();
m_bluetoothStatus = BluetoothStatusConnectedToBluetooth;
emit bluetoothStatusChanged(m_bluetoothStatus);
});
}, Qt::QueuedConnection);
connect(m_btController, &QLowEnergyController::disconnected, this, [this](){
qDebug() << "Bluetooth disconnected";
@ -58,13 +57,13 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device)
m_currentConnection = nullptr;
emit currentConnectionChanged();
m_accessPoints->clearModel();
});
}, Qt::QueuedConnection);
typedef void (QLowEnergyController::*errorsSignal)(QLowEnergyController::Error);
connect(m_btController, static_cast<errorsSignal>(&QLowEnergyController::error), this, [this](QLowEnergyController::Error error){
qDebug() << "Bluetooth error:" << error;
emit this->bluetoothConnectionError();
});
}, Qt::QueuedConnection);
connect(m_btController, &QLowEnergyController::discoveryFinished, this, [this](){
qDebug() << "Bluetooth service discovery finished";
@ -85,8 +84,9 @@ void BtWiFiSetup::disconnectFromDevice()
void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid, const QString &password)
{
if (m_bluetoothStatus != BluetoothStatusConnectedToBluetooth) {
if (m_bluetoothStatus < BluetoothStatusConnectedToBluetooth) {
qWarning() << "Cannot connect to wifi in state" << m_bluetoothStatus;
return;
}
QVariantMap request;
@ -219,7 +219,12 @@ void BtWiFiSetup::setupServices()
if (!m_wifiService || !m_deviceInformationService || !m_networkService) {
qWarning() << "Required services not found on remote device.";
m_btController->disconnectFromDevice();
if (m_btController->property("retries").toInt() < 3) {
m_btController->discoverServices();
m_btController->setProperty("retries", m_btController->property("retries").toInt() + 1);
} else {
m_btController->disconnectFromDevice();
}
return;
}
@ -322,6 +327,7 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data)
WirelessServiceResponse responseCode = (WirelessServiceResponse)data.value("r").toInt();
if (responseCode != WirelessServiceResponseSuccess) {
qWarning() << "Error in wifi command" << command << ":" << responseCode;
emit wifiSetupError();
return;
}