2022-06-08 11:30:04 +00:00
|
|
|
// Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-12-09 14:27:12 +00:00
|
|
|
|
|
|
|
#include "chatserver.h"
|
|
|
|
|
2018-08-14 08:38:48 +00:00
|
|
|
#include <QtBluetooth/qbluetoothserver.h>
|
|
|
|
#include <QtBluetooth/qbluetoothsocket.h>
|
2011-12-09 14:27:12 +00:00
|
|
|
|
|
|
|
//! [Service UUID]
|
|
|
|
static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
|
|
|
|
//! [Service UUID]
|
|
|
|
|
|
|
|
ChatServer::ChatServer(QObject *parent)
|
2018-08-14 08:38:48 +00:00
|
|
|
: QObject(parent)
|
2011-12-09 14:27:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatServer::~ChatServer()
|
|
|
|
{
|
|
|
|
stopServer();
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:52:35 +00:00
|
|
|
void ChatServer::startServer(const QBluetoothAddress& localAdapter)
|
2011-12-09 14:27:12 +00:00
|
|
|
{
|
|
|
|
if (rfcommServer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//! [Create the server]
|
2013-09-18 15:23:16 +00:00
|
|
|
rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
|
2018-08-14 08:38:48 +00:00
|
|
|
connect(rfcommServer, &QBluetoothServer::newConnection,
|
|
|
|
this, QOverload<>::of(&ChatServer::clientConnected));
|
2013-09-23 11:52:35 +00:00
|
|
|
bool result = rfcommServer->listen(localAdapter);
|
|
|
|
if (!result) {
|
|
|
|
qWarning() << "Cannot bind chat server to" << localAdapter.toString();
|
|
|
|
return;
|
|
|
|
}
|
2011-12-09 14:27:12 +00:00
|
|
|
//! [Create the server]
|
|
|
|
|
2013-10-01 12:20:26 +00:00
|
|
|
//serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);
|
2011-12-09 14:27:12 +00:00
|
|
|
|
2018-10-11 15:31:54 +00:00
|
|
|
QBluetoothServiceInfo::Sequence profileSequence;
|
2011-12-09 14:27:12 +00:00
|
|
|
QBluetoothServiceInfo::Sequence classId;
|
2021-02-26 13:54:33 +00:00
|
|
|
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort));
|
2018-10-11 15:31:54 +00:00
|
|
|
classId << QVariant::fromValue(quint16(0x100));
|
|
|
|
profileSequence.append(QVariant::fromValue(classId));
|
2013-10-01 12:20:26 +00:00
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
|
2018-10-11 15:31:54 +00:00
|
|
|
profileSequence);
|
2013-10-01 12:20:26 +00:00
|
|
|
|
2018-10-11 15:31:54 +00:00
|
|
|
classId.clear();
|
|
|
|
classId << QVariant::fromValue(QBluetoothUuid(serviceUuid));
|
2021-02-26 13:54:33 +00:00
|
|
|
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::SerialPort));
|
2013-10-23 15:33:36 +00:00
|
|
|
|
2011-12-09 14:27:12 +00:00
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
|
|
|
|
|
|
|
|
//! [Service name, description and provider]
|
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server"));
|
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
|
|
|
|
tr("Example bluetooth chat server"));
|
2013-09-23 11:52:35 +00:00
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("qt-project.org"));
|
2011-12-09 14:27:12 +00:00
|
|
|
//! [Service name, description and provider]
|
|
|
|
|
|
|
|
//! [Service UUID set]
|
|
|
|
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
|
|
|
|
//! [Service UUID set]
|
|
|
|
|
|
|
|
//! [Service Discoverability]
|
2015-01-19 11:15:18 +00:00
|
|
|
QBluetoothServiceInfo::Sequence publicBrowse;
|
2021-02-26 13:54:33 +00:00
|
|
|
publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ServiceClassUuid::PublicBrowseGroup));
|
2011-12-09 14:27:12 +00:00
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
|
2015-01-19 11:15:18 +00:00
|
|
|
publicBrowse);
|
2011-12-09 14:27:12 +00:00
|
|
|
//! [Service Discoverability]
|
|
|
|
|
|
|
|
//! [Protocol descriptor list]
|
|
|
|
QBluetoothServiceInfo::Sequence protocolDescriptorList;
|
|
|
|
QBluetoothServiceInfo::Sequence protocol;
|
2021-02-26 13:54:33 +00:00
|
|
|
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::L2cap));
|
2011-12-09 14:27:12 +00:00
|
|
|
protocolDescriptorList.append(QVariant::fromValue(protocol));
|
|
|
|
protocol.clear();
|
2021-02-26 13:54:33 +00:00
|
|
|
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::Rfcomm))
|
2011-12-09 14:27:12 +00:00
|
|
|
<< QVariant::fromValue(quint8(rfcommServer->serverPort()));
|
|
|
|
protocolDescriptorList.append(QVariant::fromValue(protocol));
|
|
|
|
serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
|
|
|
|
protocolDescriptorList);
|
|
|
|
//! [Protocol descriptor list]
|
|
|
|
|
|
|
|
//! [Register service]
|
2013-09-23 11:52:35 +00:00
|
|
|
serviceInfo.registerService(localAdapter);
|
2011-12-09 14:27:12 +00:00
|
|
|
//! [Register service]
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [stopServer]
|
|
|
|
void ChatServer::stopServer()
|
|
|
|
{
|
|
|
|
// Unregister service
|
|
|
|
serviceInfo.unregisterService();
|
|
|
|
|
|
|
|
// Close sockets
|
|
|
|
qDeleteAll(clientSockets);
|
2021-02-12 15:45:26 +00:00
|
|
|
clientNames.clear();
|
2011-12-09 14:27:12 +00:00
|
|
|
|
|
|
|
// Close server
|
|
|
|
delete rfcommServer;
|
2018-08-14 08:38:48 +00:00
|
|
|
rfcommServer = nullptr;
|
2011-12-09 14:27:12 +00:00
|
|
|
}
|
|
|
|
//! [stopServer]
|
|
|
|
|
|
|
|
//! [sendMessage]
|
|
|
|
void ChatServer::sendMessage(const QString &message)
|
|
|
|
{
|
|
|
|
QByteArray text = message.toUtf8() + '\n';
|
|
|
|
|
2018-08-14 09:08:18 +00:00
|
|
|
for (QBluetoothSocket *socket : qAsConst(clientSockets))
|
2011-12-09 14:27:12 +00:00
|
|
|
socket->write(text);
|
|
|
|
}
|
|
|
|
//! [sendMessage]
|
|
|
|
|
|
|
|
//! [clientConnected]
|
|
|
|
void ChatServer::clientConnected()
|
|
|
|
{
|
|
|
|
QBluetoothSocket *socket = rfcommServer->nextPendingConnection();
|
|
|
|
if (!socket)
|
|
|
|
return;
|
|
|
|
|
2018-08-14 08:38:48 +00:00
|
|
|
connect(socket, &QBluetoothSocket::readyRead, this, &ChatServer::readSocket);
|
|
|
|
connect(socket, &QBluetoothSocket::disconnected, this, QOverload<>::of(&ChatServer::clientDisconnected));
|
2011-12-09 14:27:12 +00:00
|
|
|
clientSockets.append(socket);
|
2021-02-12 15:45:26 +00:00
|
|
|
clientNames[socket] = socket->peerName();
|
2011-12-09 14:27:12 +00:00
|
|
|
emit clientConnected(socket->peerName());
|
|
|
|
}
|
|
|
|
//! [clientConnected]
|
|
|
|
|
|
|
|
//! [clientDisconnected]
|
|
|
|
void ChatServer::clientDisconnected()
|
|
|
|
{
|
|
|
|
QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
|
|
|
|
if (!socket)
|
|
|
|
return;
|
|
|
|
|
2021-02-12 15:45:26 +00:00
|
|
|
emit clientDisconnected(clientNames[socket]);
|
2011-12-09 14:27:12 +00:00
|
|
|
|
|
|
|
clientSockets.removeOne(socket);
|
2021-02-12 15:45:26 +00:00
|
|
|
clientNames.remove(socket);
|
2011-12-09 14:27:12 +00:00
|
|
|
|
|
|
|
socket->deleteLater();
|
|
|
|
}
|
|
|
|
//! [clientDisconnected]
|
|
|
|
|
|
|
|
//! [readSocket]
|
|
|
|
void ChatServer::readSocket()
|
|
|
|
{
|
|
|
|
QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
|
|
|
|
if (!socket)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (socket->canReadLine()) {
|
|
|
|
QByteArray line = socket->readLine().trimmed();
|
2021-02-12 15:45:26 +00:00
|
|
|
emit messageReceived(clientNames[socket],
|
2011-12-09 14:27:12 +00:00
|
|
|
QString::fromUtf8(line.constData(), line.length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [readSocket]
|