I am creating an application that will search for Bluetooth devices and then connect to one sensor of mine and display the sensor data. I have my main widget, and the search of the devices happens in a second widget. I built my application first with MSVC2019 and in my Computer (Windows 11) it can do everything (I have the Qt6.7 version).
Now, I tried to run it in my Android device (With Clang arm64-v8a), but there are some features that are not working as they were in the desktop app. I also tried setting a list in the MainWidget, but I have the same problems. The main one is the QList:
- When I press on my button, nothing happens, it should be clearing the list and adding a new item.
- I simply can not select any items.
- My layout makes everything go to the left after opening the widget
I get the following warning:
(:-1: warning: Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.)
scanwindow.h
:
#ifndef SCANWINDOW_H
#define SCANWINDOW_H
#include <QMainWindow>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QLowEnergyController>
#include <QListWidgetItem>
namespace Ui {
class ScanWindow;
}
class ScanWindow : public QMainWindow
{
Q_OBJECT
public:
explicit ScanWindow(QWidget *parent = nullptr);
~ScanWindow();
private slots:
void on_startScanButton_clicked();
void deviceDiscovered(const QBluetoothDeviceInfo &device);
void on_deviceList_itemClicked(QListWidgetItem *item);
void on_connectButton_clicked();
void on_clearButton_clicked();
signals:
void characteristicValueChanged(const QString &value);
void deviceSelected(const QBluetoothDeviceInfo &device);
private:
Ui::ScanWindow *ui;
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
QBluetoothDeviceInfo selectedDevice;
QList<QLowEnergyService*> services;
};
#endif
scanwindow.cpp:
#include "scanwindow.h"
#include "ui_scanwindow.h"
#include <QMessageBox>
#include <QPermissions>
#include <QCoreApplication>
ScanWindow::ScanWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ScanWindow)
, discoveryAgent(new QBluetoothDeviceDiscoveryAgent(this))
{
ui->setupUi(this);
ui->connectButton->setDisabled(true);
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &ScanWindow::deviceDiscovered);
}
ScanWindow::~ScanWindow()
{
qDeleteAll(services);
services.clear();
delete ui;
}
void ScanWindow::on_startScanButton_clicked()
{
ui->deviceList->clear();
#if QT_CONFIG(permissions)
QBluetoothPermission permission;
permission.setCommunicationModes(QBluetoothPermission::Access);
switch (qApp->checkPermission((permission))) {
case Qt::PermissionStatus::Undetermined:
qDebug() << "Undetermined";
qApp->requestPermission(permission, this, &ScanWindow::on_startScanButton_clicked);
break;
case Qt::PermissionStatus::Denied:
qDebug() << "Denied";
break;
case Qt::PermissionStatus::Granted:
qDebug() << "Granted";
break;
}
#endif
discoveryAgent->setLowEnergyDiscoveryTimeout(15000);
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
void ScanWindow::deviceDiscovered(const QBluetoothDeviceInfo &device){
if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
qDebug() << "Added";
QString label = device.name();
QListWidgetItem *item = new QListWidgetItem(label, ui->deviceList);
item->setData(Qt::UserRole,QVariant::fromValue(device));
}
}
void ScanWindow::on_deviceList_itemClicked(QListWidgetItem *item) {
QBluetoothDeviceInfo device = item->data(Qt::UserRole).value<QBluetoothDeviceInfo>();
selectedDevice = device;
ui->connectButton->setEnabled(true);
qDebug() << "Selected device:" << device.name() << device.address().toString();
}
void ScanWindow::on_connectButton_clicked()
{
if(!selectedDevice.isValid()){
qDebug() << "No device selected or device info is invalid";
return;
}
emit deviceSelected(selectedDevice);
}
void ScanWindow::on_clearButton_clicked()
{
ui->deviceList2->clear();
new QListWidgetItem(tr("Oak2"), ui->deviceList2);
ui->deviceList2->repaint();
ui->deviceList2->updateGeometry();
ui->deviceList2->update();
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
I tried to repaint the widget, process the events, and to update. None of these work.