-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetoothmanager.cpp
More file actions
72 lines (62 loc) · 2.58 KB
/
Copy pathbluetoothmanager.cpp
File metadata and controls
72 lines (62 loc) · 2.58 KB
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
#include "bluetoothmanager.h"
#include <qbluetoothaddress.h>
#include <qbluetoothdevicediscoveryagent.h>
#include <qbluetoothlocaldevice.h>
#include <QDebug>
BluetoothManager::BluetoothManager(QString targetDeviceName, QObject *parent)
: QObject(parent), targetDeviceName(targetDeviceName), discoveryAgent(new QBluetoothDeviceDiscoveryAgent), localDevice(new QBluetoothLocalDevice)
{
qDebug() << "Starting bluetooth manager with target" << targetDeviceName;
connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)),
this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode)));
localDevice->setHostMode(QBluetoothLocalDevice::HostConnectable);
//TODO change to limited
discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry);
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
this, SLOT(deviceFound(const QBluetoothDeviceInfo&)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
connect(localDevice, SIGNAL(pairingFinished(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing)),
this, SLOT(pairingDone(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing)));
}
void BluetoothManager::deviceFound(const QBluetoothDeviceInfo &info)
{
qDebug() << "Bluetooth device found:" << info.name();
if (info.name() == targetDeviceName) {
qDebug() << "Pairing with device:" << info.name();
emit status("Pairing");
localDevice->requestPairing(info.address(), QBluetoothLocalDevice::Paired);
discoveryAgent->stop();
}
}
void BluetoothManager::startScan()
{
qDebug() << "Scanning for bluetooth device" << targetDeviceName;
emit status("Scanning");
emit busy(true);
discoveryAgent->start();
}
void BluetoothManager::scanFinished()
{
qDebug() << "Device not found";
emit status("Device not found");
emit busy(false);
}
void BluetoothManager::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode)
{
if (mode == QBluetoothLocalDevice::HostPoweredOff) {
qDebug() << "Bluetooth is off";
emit status("Off");
emit busy(false);
}
}
void BluetoothManager::pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
{
if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired ) {
qDebug() << "Successfully paired";
emit status("Paired");
} else {
qDebug() << "Failed pairing with device";
emit status("Pairing failed");
}
emit busy(false);
}