Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////// main.cpp //////////////////////////////////////////
- #include "btechoserver.h"
- #include <QDebug>
- #include <QBluetoothLocalDevice>
- #include <QBluetoothHostInfo>
- #include <QGuiApplication>
- int main(int argc, char *argv[])
- {
- QGuiApplication app(argc, argv);
- qDebug() << Q_FUNC_INFO;
- auto localAdapters = QBluetoothLocalDevice::allDevices();
- QBluetoothLocalDevice adapter(localAdapters.at(0).address());
- adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
- BtEchoServer *m_pServer = new BtEchoServer();
- m_pServer->startServer();
- return app.exec();
- }
- ////////////////////////////////////////// btechoserver.h //////////////////////////////////////////
- #ifndef BTECHOSERVER_H
- #define BTECHOSERVER_H
- #include <QObject>
- #include <QBluetoothAddress>
- #include <QBluetoothServiceInfo>
- #include <QBluetoothSocket>
- #include <QBluetoothHostInfo>
- class QBluetoothServer;
- class BtEchoServer : public QObject
- {
- Q_OBJECT
- public:
- explicit BtEchoServer(QObject *parent = 0);
- ~BtEchoServer();
- void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress());
- void stopServer();
- public slots:
- void onClientConnected();
- void onClientDisconnected();
- void readIncomingData();
- void onBtSocketError(QBluetoothSocket::SocketError error);
- private:
- QBluetoothServer *m_pRfcommServer;
- QBluetoothServiceInfo m_serviceInfo;
- QList<QBluetoothSocket *> m_pClients;
- QList<QBluetoothHostInfo> m_localAdapters;
- };
- #endif // BTECHOSERVER_H
- ////////////////////////////////////////// btechoserver.cpp //////////////////////////////////////////
- #include "btechoserver.h"
- #include <QBluetoothServer>
- #include <QBluetoothSocket>
- #include <QBluetoothLocalDevice>
- #include <QDebug>
- #include <QString>
- static const QLatin1String btServiceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
- BtEchoServer::BtEchoServer(QObject *parent)
- : QObject(parent)
- , m_pRfcommServer(nullptr)
- {
- }
- BtEchoServer::~BtEchoServer()
- {
- qDebug() << Q_FUNC_INFO;
- stopServer();
- }
- void BtEchoServer::startServer(const QBluetoothAddress &localAdapter)
- {
- qDebug() << Q_FUNC_INFO;
- if (m_pRfcommServer)
- return;
- m_pRfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
- connect(m_pRfcommServer, SIGNAL(newConnection()), this, SLOT(onClientConnected()));
- m_pRfcommServer->listen(localAdapter);
- QBluetoothServiceInfo::Sequence classId;
- classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId);
- classId.prepend(QVariant::fromValue(QBluetoothUuid(btServiceUuid)));
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId);
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("My Dummy Bt Server"));
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Dummy Description"));
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("bla-service.com"));
- m_serviceInfo.setServiceUuid(QBluetoothUuid(btServiceUuid));
- QBluetoothServiceInfo::Sequence publicBrowse;
- publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse);
- QBluetoothServiceInfo::Sequence protocolDescriptorList;
- QBluetoothServiceInfo::Sequence protocol;
- protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
- protocolDescriptorList.append(QVariant::fromValue(protocol));
- protocol.clear();
- protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
- << QVariant::fromValue(quint8(m_pRfcommServer->serverPort()));
- protocolDescriptorList.append(QVariant::fromValue(protocol));
- m_serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList);
- qDebug() << Q_FUNC_INFO << m_serviceInfo.registerService(localAdapter);
- qDebug() << Q_FUNC_INFO << "Server Started!";
- }
- void BtEchoServer::stopServer()
- {
- qDebug() << Q_FUNC_INFO;
- // Unregister service
- m_serviceInfo.unregisterService();
- // Close server
- delete m_pRfcommServer;
- m_pRfcommServer = 0;
- }
- void BtEchoServer::onClientConnected()
- {
- qDebug() << Q_FUNC_INFO;
- QBluetoothSocket *socket = m_pRfcommServer->nextPendingConnection();
- if (!socket)
- return;
- connect(socket, SIGNAL(readyRead()), this, SLOT(readIncomingData()));
- connect(socket, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
- }
- void BtEchoServer::onClientDisconnected()
- {
- qDebug() << Q_FUNC_INFO;
- QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
- if (!socket)
- return;
- socket->deleteLater();
- }
- void BtEchoServer::readIncomingData()
- {
- QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
- if (!socket)
- return;
- while (socket->canReadLine()) {
- QByteArray line = socket->readLine().trimmed();
- qDebug() << Q_FUNC_INFO << QString::fromUtf8(line.constData(), line.length());
- }
- }
- void BtEchoServer::onBtSocketError(QBluetoothSocket::SocketError error)
- {
- qDebug() << "ERROR:" << error;
- }
- ////////////////////////////////////////// btServer.pro //////////////////////////////////////////
- QT += core bluetooth
- QT += gui //this should be QT -= gui when using QCoreApplication
- CONFIG += c++11
- TARGET = btServer
- #CONFIG += console
- CONFIG += app_bundle
- TEMPLATE = app
- SOURCES += main.cpp \
- btechoserver.cpp
- HEADERS += \
- btechoserver.h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement