-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINALLY bluetooth pairing is working o/
- Loading branch information
Showing
6 changed files
with
246 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef BLUETOOTHMANAGER_H | ||
#define BLUETOOTHMANAGER_H | ||
|
||
#include <QString> | ||
#include <QObject> | ||
#include <qbluetooth.h> | ||
#include <qbluetoothdevicediscoveryagent.h> | ||
#include <qbluetoothlocaldevice.h> | ||
|
||
// add Qt Mobility Project Namespace | ||
QTM_USE_NAMESPACE | ||
|
||
class BluetoothManager : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit BluetoothManager(QString targetDeviceName, QObject *parent = 0); | ||
signals: | ||
void status(QString); | ||
void busy(bool); | ||
public slots: | ||
void startScan(); | ||
void deviceFound(const QBluetoothDeviceInfo&); | ||
void pairingDone(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing); | ||
private slots: | ||
void scanFinished(); | ||
void hostModeStateChanged(QBluetoothLocalDevice::HostMode); | ||
private: | ||
QString targetDeviceName; | ||
QBluetoothDeviceDiscoveryAgent *discoveryAgent; | ||
QBluetoothLocalDevice *localDevice; | ||
}; | ||
|
||
#endif // BLUETOOTHMANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,49 @@ | ||
#include "signalmanager.h" | ||
#include <QDebug> | ||
|
||
SignalManager::SignalManager(QWidget *parent) : | ||
QTabWidget(parent) | ||
QTabWidget(parent), rotationSensor(new QRotationSensor(this)), btManager(new BluetoothManager(DEVICE_NAME)) | ||
{ | ||
rotationSensor = new QRotationSensor(this); | ||
qDebug() << "Starting rotation sensor"; | ||
rotationSensor->start(); | ||
if (!rotationSensor->isActive()) { | ||
qWarning("RotationSensor didn't start!"); | ||
qWarning() << "RotationSensor didn't start!"; | ||
} | ||
|
||
connect(rotationSensor, SIGNAL(readingChanged()), this, SLOT(updateReading())); | ||
|
||
connect(btManager, SIGNAL(status(QString)), this, SLOT(changeBluetoothStatus(QString))); | ||
connect(btManager, SIGNAL(busy(bool)), this, SLOT(enableScan(bool))); | ||
} | ||
|
||
void SignalManager::rotationDialMoved(int value) | ||
{ | ||
qDebug("Dial moved: %d", value); | ||
qDebug() << "Dial moved:" << value; | ||
} | ||
|
||
void SignalManager::verticalMovementSliderMoved(int value) | ||
{ | ||
qDebug("Vertical slider moved: %d", value); | ||
qDebug() << "Vertical slider moved:" << value; | ||
} | ||
|
||
void SignalManager::updateReading() | ||
{ | ||
qDebug("Rotation changed"); | ||
// qDebug() << "Rotation changed"; | ||
emit xRotationChanged(rotationSensor->reading()->x()); | ||
emit yRotationChanged(rotationSensor->reading()->y()); | ||
emit zRotationChanged(rotationSensor->reading()->z()); | ||
} | ||
|
||
void SignalManager::scanForBluetoothDevice() | ||
{ | ||
btManager->startScan(); | ||
} | ||
|
||
void SignalManager::changeBluetoothStatus(QString status) | ||
{ | ||
emit bluetoothStatusChanged(status); | ||
} | ||
|
||
void SignalManager::enableScan(bool busy) | ||
{ | ||
emit scanEnabled(!busy); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters