chatserver.cpp Example File
btchat/chatserver.cpp
 
 #include "chatserver.h"
 #include <qrfcommserver.h>
 #include <qbluetoothsocket.h>
 static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
 ChatServer::ChatServer(QObject *parent)
 :   QObject(parent), rfcommServer(0)
 {
 }
 ChatServer::~ChatServer()
 {
     stopServer();
 }
 void ChatServer::startServer()
 {
     if (rfcommServer)
         return;
     //! [Create the server]
     rfcommServer = new QRfcommServer(this);
     connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
     rfcommServer->listen();
     //! [Create the server]
     serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);
     //! [Class Uuuid must contain at least 1 entry]
     QBluetoothServiceInfo::Sequence classId;
     classId << QVariant::fromValue(QBluetoothUuid(serviceUuid));
     serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
     //! [Class Uuuid must contain at least 1 entry]
     //! [Service name, description and provider]
     serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server"));
     serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
                              tr("Example bluetooth chat server"));
     serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("Nokia, QtDF"));
     //! [Service name, description and provider]
     //! [Service UUID set]
     serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
     //! [Service UUID set]
     //! [Service Discoverability]
     serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
                              QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
     //! [Service Discoverability]
     //! [Protocol descriptor list]
     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(rfcommServer->serverPort()));
     protocolDescriptorList.append(QVariant::fromValue(protocol));
     serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                              protocolDescriptorList);
     //! [Protocol descriptor list]
     //! [Register service]
     serviceInfo.registerService();
     //! [Register service]
 }
 void ChatServer::stopServer()
 {
     
     serviceInfo.unregisterService();
     
     qDeleteAll(clientSockets);
     
     delete rfcommServer;
     rfcommServer = 0;
 }
 void ChatServer::sendMessage(const QString &message)
 {
     QByteArray text = message.toUtf8() + '\n';
     foreach (QBluetoothSocket *socket, clientSockets)
         socket->write(text);
 }
 void ChatServer::clientConnected()
 {
     QBluetoothSocket *socket = rfcommServer->nextPendingConnection();
     if (!socket)
         return;
     connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
     connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
     clientSockets.append(socket);
     emit clientConnected(socket->peerName());
 }
 void ChatServer::clientDisconnected()
 {
     QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
     if (!socket)
         return;
     emit clientDisconnected(socket->peerName());
     clientSockets.removeOne(socket);
     socket->deleteLater();
 }
 void ChatServer::readSocket()
 {
     QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
     if (!socket)
         return;
     while (socket->canReadLine()) {
         QByteArray line = socket->readLine().trimmed();
         emit messageReceived(socket->peerName(),
                              QString::fromUtf8(line.constData(), line.length()));
     }
 }