-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwindow.cpp
74 lines (58 loc) · 2.42 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "multimeter.h"
#include <QListWidgetItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000);
QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, [this](const QBluetoothDeviceInfo &device) {
qInfo() << "DEVICE DISCOVERED:" << device.address() << device.serviceUuids();
if (!(device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration))
return;
if (!device.serviceUuids().contains(QBluetoothUuid(QString("0bd51666-e7cb-469b-8e4d-2742f1ba77cc"))))
return;
detectedDevices << device;
new QListWidgetItem(device.name() + " (" + device.address().toString() + ")", ui->listWidget);
});
QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &MainWindow::scanEnded);
QObject::connect(deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &MainWindow::scanEnded);
QObject::connect(ui->scanButton, &QPushButton::clicked, this, &MainWindow::scan);
QObject::connect(ui->listWidget, &QListWidget::doubleClicked, [this](const QModelIndex &index) {
QBluetoothDeviceInfo device = detectedDevices.at(index.row());
foreach (Multimeter *multimeter, multimeters) {
if (multimeter->getDeviceInfo() == device) {
multimeter->activateWindow();
multimeter->show();
return;
}
}
Multimeter *multimeter = new Multimeter(device);
multimeters << multimeter;
multimeter->show();
QObject::connect(multimeter, &Multimeter::destroyed, [this]() {
Multimeter *multimeter = (Multimeter *) sender();
multimeters.removeAll(multimeter);
});
});
scan();
}
void MainWindow::scan()
{
detectedDevices.clear();
ui->listWidget->clear();
ui->scanButton->setEnabled(false);
ui->scanButton->setText("Scanning ...");
deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
void MainWindow::scanEnded() {
ui->scanButton->setEnabled(true);
ui->scanButton->setText("Scan for BLE multimeters");
}
MainWindow::~MainWindow()
{
delete ui;
}