Yeah ofcourse I can! But I do got to say this codes are made for a single device, if you want to connect to variable devices you need to extend the connection function, however I saw example codes of that where I based my codes on
)
Please do note I am not an official developer so this codes can be optimized and the functions displayState needs to be removed.
1. make a new .h file (I named this one bluetoothcontroller.h)
Code:
#ifndef BLUETOOTHCONTROLLER_H
#define BLUETOOTHCONTROLLER_H
#include <QtCore/QObject>
#include <QIODevice>
#include <QDataStream>
#include <QBluetoothSocket>
#include <QBluetoothAddress>
#include <QBluetoothUuid>
#include <QDateTime>
#include <qmdisplaystate.h>
#include "qmlapplicationviewer.h"
QTM_BEGIN_NAMESPACE
class QBluetoothSocket;
QTM_END_NAMESPACE
QTM_USE_NAMESPACE
class BTController : public QObject
{
Q_OBJECT
public:
explicit BTController(QObject *parent = 0);
~BTController();
MeeGo::QmDisplayState displayState;
Q_INVOKABLE void printDisplayState();
Q_INVOKABLE void turnOffDisplay();
Q_INVOKABLE void turnOnDisplay();
Q_INVOKABLE void startConnection();
Q_INVOKABLE void stopConnection();
Q_INVOKABLE void send(const QString &message);
signals:
void retrieve(const QString &sender, const QString &message);
void btConnected(const QString &name);
void btDisconnected();
void btError(const QString &errorMessage);
private slots:
void readSocket();
void connectSocket();
void disconnectSocket();
void socketError(QBluetoothSocket::SocketError errorMessage);
private:
QDataStream *stream;
QBluetoothSocket *socket;
BTController *bt;
};
#endif // BLUETOOTHCONTROLLER_H
Make an new .cpp file (I named this one bluetoothcontroller.cpp)
Code:
#include <QtGui/QApplication>
#include <QCoreApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QIODevice>
#include <QDataStream>
#include <QBluetoothSocket>
#include <QBluetoothAddress>
#include <QBluetoothUuid>
#include <qplatformdefs.h> // MEEGO_EDITION_HARMATTAN
#ifdef HARMATTAN_BOOSTER
#include <MDeclarativeCache>
#endif
#include <bluetoothcontroller.h>
BTController::BTController(QObject *parent)
: QObject(parent), socket(0) {}
BTController::~BTController() {
startConnection();
}
void BTController::printDisplayState() {
if (displayState.get() == MeeGo::QmDisplayState::On) {
qDebug() << "The display is on!";
}
else if(displayState.get() == MeeGo::QmDisplayState::Off) {
qDebug() << "The display is off!";
}
}
void BTController::turnOffDisplay() {
qDebug() << "Turning off display";
displayState.set(MeeGo::QmDisplayState::Off);
}
void BTController::turnOnDisplay() {
qDebug() << "Turning on display";
displayState.set(MeeGo::QmDisplayState::On);
}
void BTController::startConnection() {
const QBluetoothUuid uuid = QBluetoothUuid::SerialPort;
//qDebug() << "C: uuid.toString" << uuid.toString();
const QString myMacAdress = "YOUR:MAC:ADRESS";
const QBluetoothAddress service(myMacAdress);
//qDebug() << "C: service.toString" << service.toString();
socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
qDebug() << "C: Create socket";
socket->connectToService(service,uuid,QIODevice::ReadWrite);
qDebug() << "C: ConnecttoService done";
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
connect(socket, SIGNAL(connected()), this, SLOT(connectSocket()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnectSocket()));
connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socketError(QBluetoothSocket::SocketError)));
}
void BTController::stopConnection() {
delete socket;
socket = 0;
}
void BTController::readSocket() {
if (!socket)
return;
while (socket->canReadLine()) {
QByteArray line = socket->readLine();
emit retrieve(socket->peerName(),
QString::fromUtf8(line.constData(), line.length()));
}
}
void BTController::send(const QString &message) {
if (!socket)
return;
QByteArray text = message.toUtf8();
socket->write(text);
qDebug() << "C: Wrote: '"+text+"'";
}
void BTController::connectSocket() {
qDebug() << "C: "+socket->peerName()+" connected";
emit btConnected(socket->peerName());
}
void BTController::disconnectSocket() {
qDebug() << "C: Disconnected";
emit btDisconnected();
}
void BTController::socketError(QBluetoothSocket::SocketError errorMessage) {
qDebug() << "C: Error: "+ errorMessage;
emit btError(socket->errorString());
}
Now comes the tricky part, you want to add the C++ class to your QtQuick document
First this is my main.cpp file:
Code:
#include <QtGui/QApplication>
#include <QCoreApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QIODevice>
#include <QDataStream>
#include <QBluetoothSocket>
#include "bluetoothcontroller.h"
#include "qmlapplicationviewer.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
QDeclarativeContext *ctxt = viewer->rootContext();
BTController bt;
ctxt->setContextProperty("blueTooth", &bt);
viewer->setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer->setMainQmlFile(QLatin1String("qml/ZoomerDashboard/main.qml"));
viewer->showExpanded();
bt.startConnection();//call function for auto-start BT
return app->exec();
}
However, to make setContextProperty work we need to update qmlapplicationviewer.h and qmlapplicationviewer.cpp
This is my qmlapplicationviewer.h file (note that all that was add'd is the QDeclarativeContext)
Code:
// checksum 0x82ed version 0x60010
/*
This file was generated by the Qt Quick Application wizard of Qt Creator.
QmlApplicationViewer is a convenience class containing mobile device specific
code such as screen orientation handling. Also QML paths and debugging are
handled here.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#ifndef QMLAPPLICATIONVIEWER_H
#define QMLAPPLICATIONVIEWER_H
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeComponent>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>
class QmlApplicationViewer : public QDeclarativeView
{
Q_OBJECT
public:
QDeclarativeContext *rootContext();
enum ScreenOrientation {
ScreenOrientationLockPortrait,
ScreenOrientationLockLandscape,
ScreenOrientationAuto
};
explicit QmlApplicationViewer(QWidget *parent = 0);
virtual ~QmlApplicationViewer();
static QmlApplicationViewer *create();
void setMainQmlFile(const QString &file);
void addImportPath(const QString &path);
// Note that this will only have an effect on Symbian and Fremantle.
void setOrientation(ScreenOrientation orientation);
void showExpanded();
private:
explicit QmlApplicationViewer(QDeclarativeView *view, QWidget *parent);
class QmlApplicationViewerPrivate *d;
};
QApplication *createApplication(int &argc, char **argv);
#endif // QMLAPPLICATIONVIEWER_H
This is my qmlapplicationviewer.cpp file (note that all that was add'd is the QDeclarativeContext)
Code:
....default codes....
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtDeclarative/QDeclarativeComponent>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>
#include <QtGui/QApplication>
....default codes....
class QmlApplicationViewerPrivate
{
QmlApplicationViewerPrivate(QDeclarativeView *view_) : view(view_) {}
QString mainQmlFile;
QDeclarativeView *view;
friend class QmlApplicationViewer;
QString adjustPath(const QString &path);
};
....default codes....
QDeclarativeContext *QmlApplicationViewer::rootContext()
{
return d->view->rootContext();
}
...... default codes....
With that, you can access "blueTooth" within your QtQuick document, for example the Connections:
Code:
Connections {
target: blueTooth
onBtConnected: {
console.log("QT: The connection changed: connected!");
}
onBtDisconnected: {
console.log("QT: The connection changed: disconnected!");
}
onRetrieve: {
var messageNew = message;
messageNew = messageNew.replace("\n", "");
messageNew = messageNew.replace("\r", "");
console.log("dataRetrieved message: "+messageNew);
}
onBtError: {
console.log("QT: Error: " + errorMessage);
}
}
or manually connect by using 'blueTooth.startConnection();' or send by 'blueTooth.send(STRING);'
One issue I still haven't resolved:
* The error message within the onBtError will never show the right error, its always empty.
Another note:
* Once you changed the 'qmlapplicationviewer' files, QtCreator will check this files on startup, once you press 'Yes' in the revert files dialog, you will loose your rootContext function. So once you have it working make a backup of those files!