diff --git a/QuadcopterController.pro b/QuadcopterController.pro index fe5638c..182f58f 100644 --- a/QuadcopterController.pro +++ b/QuadcopterController.pro @@ -6,6 +6,11 @@ DEPLOYMENTFOLDERS = # file1 dir1 symbian:TARGET.UID3 = 0xEFDD9717 +INCLUDEPATH += C:/QtSDK/Symbian/SDKs/Symbian3Qt473_12b/src/connectivity/bluetooth +DEPENDPATH += C:/QtSDK/Symbian/SDKs/Symbian3Qt473_12b/src/connectivity/bluetooth +INCLUDEPATH += C:/QtSDK/Symbian/SDKs/Symbian3Qt473_12b/src/sensors +DEPENDPATH += C:/QtSDK/Symbian/SDKs/Symbian3Qt473_12b/src/sensors + # Allow network access on Symbian #symbian:TARGET.CAPABILITY += NetworkServices @@ -13,18 +18,37 @@ symbian:TARGET.UID3 = 0xEFDD9717 # the following lines and add the respective components to the # MOBILITY variable. CONFIG += mobility -MOBILITY += sensors +MOBILITY += sensors connectivity SOURCES += main.cpp mainwindow.cpp \ reseterdial.cpp \ reseterslider.cpp \ - signalmanager.cpp + signalmanager.cpp \ + bluetoothmanager.cpp HEADERS += mainwindow.h \ reseterdial.h \ reseterslider.h \ - signalmanager.h + signalmanager.h \ + bluetoothmanager.h FORMS += mainwindow.ui # Please do not modify the following two lines. Required for deployment. include(deployment.pri) qtcAddDeployment() + +symbian { + TARGET.CAPABILITY = ReadDeviceData LocalServices WriteDeviceData +} + +# QBluetooth +#symbian{ +# INCLUDEPATH += /epoc32/include/QBluetooth +# LIBS += -lQBluetooth + +# customrules.pkg_prerules = \ +# ";QBluetooth" \ +# "@\"$$(EPOCROOT)Epoc32/InstallToDevice/QBluetooth_selfsigned.sisx\",(0xA003328D)"\ +# " " + +# DEPLOYMENT += customrules +#} diff --git a/bluetoothmanager.cpp b/bluetoothmanager.cpp new file mode 100644 index 0000000..e09b8b2 --- /dev/null +++ b/bluetoothmanager.cpp @@ -0,0 +1,72 @@ +#include "bluetoothmanager.h" +#include +#include +#include +#include + +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); +} diff --git a/bluetoothmanager.h b/bluetoothmanager.h new file mode 100644 index 0000000..61551e8 --- /dev/null +++ b/bluetoothmanager.h @@ -0,0 +1,34 @@ +#ifndef BLUETOOTHMANAGER_H +#define BLUETOOTHMANAGER_H + +#include +#include +#include +#include +#include + +// 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 diff --git a/mainwindow.ui b/mainwindow.ui index f80c64c..e693583 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -62,7 +62,35 @@ - + + + Scan + + + + + + + + + Bluetooth status: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Off + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + @@ -233,9 +261,12 @@ xRotationChanged(int) yRotationChanged(int) zRotationChanged(int) + bluetoothStatusChanged(QString) + scanEnabled(bool) rotationDialMoved(int) verticalMovementSliderMoved(int) updateReading() + scanForBluetoothDevice() @@ -264,8 +295,8 @@ rotationDialMoved(int) - 271 - 170 + 568 + 222 342 @@ -284,8 +315,8 @@ 342 - 400 - 133 + 357 + 139 @@ -300,24 +331,56 @@ 346 - 448 - 207 + 357 + 223 TabWidget - zRotationChanged(int) - AccelerometerPageZLCD - display(int) + bluetoothStatusChanged(QString) + BluetoothStatusLabel + setText(QString) + + + 351 + 50 + + + 303 + 306 + + + + + BluetoothScanButton + clicked() + TabWidget + scanForBluetoothDevice() + + + 229 + 241 + + + 359 + 47 + + + + + TabWidget + scanEnabled(bool) + BluetoothScanButton + setEnabled(bool) - 466 - 343 + 205 + 44 - 505 - 297 + 209 + 241 diff --git a/signalmanager.cpp b/signalmanager.cpp index dfdeda1..326758f 100644 --- a/signalmanager.cpp +++ b/signalmanager.cpp @@ -1,31 +1,49 @@ #include "signalmanager.h" +#include 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); +} diff --git a/signalmanager.h b/signalmanager.h index 3aa5c46..a57f87b 100644 --- a/signalmanager.h +++ b/signalmanager.h @@ -1,11 +1,14 @@ #ifndef PAGESTACK_H #define PAGESTACK_H +#include "bluetoothmanager.h" #include -#include +#include + +#define DEVICE_NAME "OLAV-LAPTOP" // add Qt Mobility Project Namespace -QTM_USE_NAMESPACE +//QTM_USE_NAMESPACE class SignalManager : public QTabWidget { @@ -16,12 +19,18 @@ class SignalManager : public QTabWidget void xRotationChanged(int value); void yRotationChanged(int value); void zRotationChanged(int value); + void bluetoothStatusChanged(QString status); + void scanEnabled(bool); public slots: void rotationDialMoved(int value); void verticalMovementSliderMoved(int value); void updateReading(); + void scanForBluetoothDevice(); + void changeBluetoothStatus(QString status); + void enableScan(bool busy); private: QRotationSensor * rotationSensor; + BluetoothManager * btManager; }; #endif // PAGESTACK_H