Advertisement
Guest User

BtServer QCoreApplication issue

a guest
Jul 4th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////// main.cpp //////////////////////////////////////////
  2. #include "btechoserver.h"
  3.  
  4. #include <QDebug>
  5. #include <QBluetoothLocalDevice>
  6. #include <QBluetoothHostInfo>
  7. #include <QGuiApplication>
  8.  
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     QGuiApplication app(argc, argv);
  13.  
  14.     qDebug() << Q_FUNC_INFO;
  15.     auto localAdapters = QBluetoothLocalDevice::allDevices();
  16.     QBluetoothLocalDevice adapter(localAdapters.at(0).address());
  17.     adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
  18.  
  19.     BtEchoServer *m_pServer = new BtEchoServer();
  20.     m_pServer->startServer();
  21.  
  22.     return app.exec();
  23. }
  24.  
  25. ////////////////////////////////////////// btechoserver.h //////////////////////////////////////////
  26.  
  27. #ifndef BTECHOSERVER_H
  28. #define BTECHOSERVER_H
  29.  
  30. #include <QObject>
  31. #include <QBluetoothAddress>
  32. #include <QBluetoothServiceInfo>
  33. #include <QBluetoothSocket>
  34. #include <QBluetoothHostInfo>
  35.  
  36. class QBluetoothServer;
  37.  
  38. class BtEchoServer : public QObject
  39. {
  40.     Q_OBJECT
  41.  
  42. public:
  43.     explicit BtEchoServer(QObject *parent = 0);
  44.     ~BtEchoServer();
  45.     void startServer(const QBluetoothAddress &localAdapter = QBluetoothAddress());
  46.     void stopServer();
  47.  
  48. public slots:
  49.     void onClientConnected();
  50.     void onClientDisconnected();
  51.     void readIncomingData();
  52.     void onBtSocketError(QBluetoothSocket::SocketError error);
  53.  
  54.  
  55. private:
  56.     QBluetoothServer *m_pRfcommServer;
  57.     QBluetoothServiceInfo m_serviceInfo;
  58.     QList<QBluetoothSocket *> m_pClients;
  59.     QList<QBluetoothHostInfo> m_localAdapters;
  60. };
  61.  
  62. #endif // BTECHOSERVER_H
  63.  
  64. ////////////////////////////////////////// btechoserver.cpp //////////////////////////////////////////
  65.  
  66. #include "btechoserver.h"
  67.  
  68. #include <QBluetoothServer>
  69. #include <QBluetoothSocket>
  70. #include <QBluetoothLocalDevice>
  71. #include <QDebug>
  72. #include <QString>
  73.  
  74. static const QLatin1String btServiceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
  75.  
  76. BtEchoServer::BtEchoServer(QObject *parent)
  77.     : QObject(parent)
  78.     , m_pRfcommServer(nullptr)
  79. {
  80. }
  81.  
  82. BtEchoServer::~BtEchoServer()
  83. {
  84.     qDebug() << Q_FUNC_INFO;
  85.     stopServer();
  86. }
  87.  
  88. void BtEchoServer::startServer(const QBluetoothAddress &localAdapter)
  89. {
  90.     qDebug() << Q_FUNC_INFO;
  91.  
  92.     if (m_pRfcommServer)
  93.         return;
  94.  
  95.     m_pRfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
  96.     connect(m_pRfcommServer, SIGNAL(newConnection()), this, SLOT(onClientConnected()));
  97.  
  98.     m_pRfcommServer->listen(localAdapter);
  99.  
  100.     QBluetoothServiceInfo::Sequence classId;
  101.     classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
  102.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId);
  103.  
  104.     classId.prepend(QVariant::fromValue(QBluetoothUuid(btServiceUuid)));
  105.  
  106.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
  107.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId);
  108.  
  109.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("My Dummy Bt Server"));
  110.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Dummy Description"));
  111.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("bla-service.com"));
  112.     m_serviceInfo.setServiceUuid(QBluetoothUuid(btServiceUuid));
  113.  
  114.     QBluetoothServiceInfo::Sequence publicBrowse;
  115.     publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
  116.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse);
  117.  
  118.     QBluetoothServiceInfo::Sequence protocolDescriptorList;
  119.     QBluetoothServiceInfo::Sequence protocol;
  120.     protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
  121.     protocolDescriptorList.append(QVariant::fromValue(protocol));
  122.     protocol.clear();
  123.  
  124.     protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
  125.              << QVariant::fromValue(quint8(m_pRfcommServer->serverPort()));
  126.     protocolDescriptorList.append(QVariant::fromValue(protocol));
  127.     m_serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList);
  128.  
  129.     qDebug() << Q_FUNC_INFO << m_serviceInfo.registerService(localAdapter);
  130.     qDebug() << Q_FUNC_INFO << "Server Started!";
  131.  
  132. }
  133.  
  134. void BtEchoServer::stopServer()
  135. {
  136.     qDebug() << Q_FUNC_INFO;
  137.     // Unregister service
  138.     m_serviceInfo.unregisterService();
  139.  
  140.     // Close server
  141.     delete m_pRfcommServer;
  142.     m_pRfcommServer = 0;
  143. }
  144.  
  145. void BtEchoServer::onClientConnected()
  146. {
  147.     qDebug() << Q_FUNC_INFO;
  148.     QBluetoothSocket *socket = m_pRfcommServer->nextPendingConnection();
  149.     if (!socket)
  150.         return;
  151.  
  152.     connect(socket, SIGNAL(readyRead()), this, SLOT(readIncomingData()));
  153.     connect(socket, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
  154. }
  155.  
  156. void BtEchoServer::onClientDisconnected()
  157. {
  158.     qDebug() << Q_FUNC_INFO;
  159.     QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
  160.     if (!socket)
  161.         return;
  162.  
  163.     socket->deleteLater();
  164. }
  165.  
  166. void BtEchoServer::readIncomingData()
  167. {
  168.     QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
  169.     if (!socket)
  170.         return;
  171.  
  172.     while (socket->canReadLine()) {
  173.         QByteArray line = socket->readLine().trimmed();
  174.         qDebug() << Q_FUNC_INFO << QString::fromUtf8(line.constData(), line.length());
  175.     }
  176. }
  177.  
  178. void BtEchoServer::onBtSocketError(QBluetoothSocket::SocketError error)
  179. {
  180.     qDebug() << "ERROR:" << error;
  181. }
  182.  
  183.  
  184. ////////////////////////////////////////// btServer.pro //////////////////////////////////////////
  185.  
  186. QT += core bluetooth
  187. QT += gui //this should be QT -= gui when using QCoreApplication
  188.  
  189. CONFIG += c++11
  190.  
  191. TARGET = btServer
  192. #CONFIG += console
  193. CONFIG += app_bundle
  194.  
  195. TEMPLATE = app
  196.  
  197. SOURCES += main.cpp \
  198.     btechoserver.cpp
  199.  
  200. HEADERS += \
  201.     btechoserver.h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement