-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.cpp
115 lines (96 loc) · 3.29 KB
/
bluetooth.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "bluetooth.h"
#include "device.h"
Bluetooth::Bluetooth(QObject *parent)
: QObject(parent), mbtLocalDevice(new QBluetoothLocalDevice)
{
mbtDeviceAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(mbtDeviceAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Bluetooth::addScannedDevice);
connect(mbtDeviceAgent, static_cast<void (QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error),
this, &Bluetooth::scanError);
connect(mbtDeviceAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Bluetooth::scanFinished);
connect(mbtDeviceAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &Bluetooth::scanFinished);
connect(mbtLocalDevice, &QBluetoothLocalDevice::pairingFinished, this, &Bluetooth::pairingFinished);
connect(mbtLocalDevice, &QBluetoothLocalDevice::error, this, &Bluetooth::pairingError);
}
Bluetooth::~Bluetooth()
{
qDeleteAll(mDevices);
delete mbtDeviceAgent;
delete mbtLocalDevice;
}
void Bluetooth::startScanning()
{
qDeleteAll(mDevices);
mDevices.clear();
emit devicesChanged();
mbtDeviceAgent->start(QBluetoothDeviceDiscoveryAgent::ClassicMethod);
emit scanningChanged();
qDebug() << "Scanning for devices...";
}
void Bluetooth::addScannedDevice(const QBluetoothDeviceInfo &device)
{
if (device.coreConfigurations()) {
mDevices.append(new Device(device));
emit devicesChanged();
}
}
void Bluetooth::scanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
qDebug() << "Please turn on the Bluetooth adaptor.";
else
qDebug() << "An unknown error has occurred.";
}
void Bluetooth::scanFinished()
{
if (mDevices.isEmpty())
qDebug() << "No devices found.";
else
qDebug() << "Scanning done.";
emit scanningChanged();
emit devicesChanged();
}
bool Bluetooth::scanning() const
{
return mbtDeviceAgent->isActive();
}
void Bluetooth::refresh()
{
startScanning();
}
void Bluetooth::pairDevice(const QBluetoothAddress &address)
{
for (QObject *entry : qAsConst(mDevices)) {
auto device = qobject_cast<Device *>(entry);
if (device && device->getAddress() == address ) {
mCurrentDevice = device;
if (mCurrentDevice->isPaired()) {
mbtLocalDevice->requestPairing(mCurrentDevice->getAddress(), QBluetoothLocalDevice::Unpaired);
} else {
mbtLocalDevice->requestPairing(mCurrentDevice->getAddress(), QBluetoothLocalDevice::Paired);
}
break;
}
}
}
void Bluetooth::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing status)
{
if (status == QBluetoothLocalDevice::Unpaired)
{
mCurrentDevice->setIsPaired(false);
} else {
mCurrentDevice->setIsPaired(true);
}
emit devicesChanged();
}
void Bluetooth::pairingError(QBluetoothLocalDevice::Error error)
{
if (error != QBluetoothLocalDevice::PairingError)
return;
qDebug() << "Error while trying to pair device: " << error;
pairingFinished(mCurrentDevice->getAddress(), QBluetoothLocalDevice::Unpaired);
}
QVariant Bluetooth::devices()
{
return QVariant::fromValue(mDevices);
}