Skip to content

Commit

Permalink
FINALLY bluetooth pairing is working o/
Browse files Browse the repository at this point in the history
  • Loading branch information
olavph committed Apr 30, 2011
1 parent 6bcf1b0 commit 9c0c45b
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 26 deletions.
30 changes: 27 additions & 3 deletions QuadcopterController.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,49 @@ 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

# If your application uses the Qt Mobility libraries, uncomment
# 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
#}
72 changes: 72 additions & 0 deletions bluetoothmanager.cpp
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);
}
34 changes: 34 additions & 0 deletions bluetoothmanager.h
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
91 changes: 77 additions & 14 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,35 @@
<widget class="QLCDNumber" name="AccelerometerPageYLCD"/>
</item>
<item>
<widget class="QLCDNumber" name="AccelerometerPageZLCD"/>
<widget class="QPushButton" name="BluetoothScanButton">
<property name="text">
<string>Scan</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="BluetoothStatusTitleLabel">
<property name="text">
<string>Bluetooth status:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="BluetoothStatusLabel">
<property name="text">
<string>Off</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
Expand Down Expand Up @@ -233,9 +261,12 @@
<signal>xRotationChanged(int)</signal>
<signal>yRotationChanged(int)</signal>
<signal>zRotationChanged(int)</signal>
<signal>bluetoothStatusChanged(QString)</signal>
<signal>scanEnabled(bool)</signal>
<slot>rotationDialMoved(int)</slot>
<slot>verticalMovementSliderMoved(int)</slot>
<slot>updateReading()</slot>
<slot>scanForBluetoothDevice()</slot>
</slots>
</customwidget>
</customwidgets>
Expand Down Expand Up @@ -264,8 +295,8 @@
<slot>rotationDialMoved(int)</slot>
<hints>
<hint type="sourcelabel">
<x>271</x>
<y>170</y>
<x>568</x>
<y>222</y>
</hint>
<hint type="destinationlabel">
<x>342</x>
Expand All @@ -284,8 +315,8 @@
<y>342</y>
</hint>
<hint type="destinationlabel">
<x>400</x>
<y>133</y>
<x>357</x>
<y>139</y>
</hint>
</hints>
</connection>
Expand All @@ -300,24 +331,56 @@
<y>346</y>
</hint>
<hint type="destinationlabel">
<x>448</x>
<y>207</y>
<x>357</x>
<y>223</y>
</hint>
</hints>
</connection>
<connection>
<sender>TabWidget</sender>
<signal>zRotationChanged(int)</signal>
<receiver>AccelerometerPageZLCD</receiver>
<slot>display(int)</slot>
<signal>bluetoothStatusChanged(QString)</signal>
<receiver>BluetoothStatusLabel</receiver>
<slot>setText(QString)</slot>
<hints>
<hint type="sourcelabel">
<x>351</x>
<y>50</y>
</hint>
<hint type="destinationlabel">
<x>303</x>
<y>306</y>
</hint>
</hints>
</connection>
<connection>
<sender>BluetoothScanButton</sender>
<signal>clicked()</signal>
<receiver>TabWidget</receiver>
<slot>scanForBluetoothDevice()</slot>
<hints>
<hint type="sourcelabel">
<x>229</x>
<y>241</y>
</hint>
<hint type="destinationlabel">
<x>359</x>
<y>47</y>
</hint>
</hints>
</connection>
<connection>
<sender>TabWidget</sender>
<signal>scanEnabled(bool)</signal>
<receiver>BluetoothScanButton</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>466</x>
<y>343</y>
<x>205</x>
<y>44</y>
</hint>
<hint type="destinationlabel">
<x>505</x>
<y>297</y>
<x>209</x>
<y>241</y>
</hint>
</hints>
</connection>
Expand Down
32 changes: 25 additions & 7 deletions signalmanager.cpp
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);
}
13 changes: 11 additions & 2 deletions signalmanager.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#ifndef PAGESTACK_H
#define PAGESTACK_H

#include "bluetoothmanager.h"
#include <QTabWidget>
#include <QRotationSensor>
#include <qrotationsensor.h>

#define DEVICE_NAME "OLAV-LAPTOP"

// add Qt Mobility Project Namespace
QTM_USE_NAMESPACE
//QTM_USE_NAMESPACE

class SignalManager : public QTabWidget
{
Expand All @@ -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

0 comments on commit 9c0c45b

Please sign in to comment.