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 "chatclient.h"
|
|
|
|
|
|
2024-01-05 07:12:46 +00:00
|
|
|
#include <QMetaEnum>
|
|
|
|
|
#include <QMetaObject>
|
2011-12-09 14:27:12 +00:00
|
|
|
|
2024-01-05 07:12:46 +00:00
|
|
|
#include <QBluetoothServiceInfo>
|
2023-03-16 17:26:38 +00:00
|
|
|
|
|
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
|
2011-12-09 14:27:12 +00:00
|
|
|
ChatClient::ChatClient(QObject *parent)
|
2018-08-14 08:38:48 +00:00
|
|
|
: QObject(parent)
|
2011-12-09 14:27:12 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChatClient::~ChatClient()
|
|
|
|
|
{
|
|
|
|
|
stopClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! [startClient]
|
|
|
|
|
void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
|
|
|
|
|
{
|
|
|
|
|
if (socket)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Connect to service
|
2013-09-18 15:23:16 +00:00
|
|
|
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
|
2011-12-09 14:27:12 +00:00
|
|
|
qDebug() << "Create socket";
|
|
|
|
|
socket->connectToService(remoteService);
|
2013-09-23 11:52:35 +00:00
|
|
|
qDebug() << "ConnectToService done";
|
2011-12-09 14:27:12 +00:00
|
|
|
|
2018-08-14 08:38:48 +00:00
|
|
|
connect(socket, &QBluetoothSocket::readyRead, this, &ChatClient::readSocket);
|
|
|
|
|
connect(socket, &QBluetoothSocket::connected, this, QOverload<>::of(&ChatClient::connected));
|
|
|
|
|
connect(socket, &QBluetoothSocket::disconnected, this, &ChatClient::disconnected);
|
2021-03-16 19:09:11 +00:00
|
|
|
connect(socket, &QBluetoothSocket::errorOccurred, this, &ChatClient::onSocketErrorOccurred);
|
2011-12-09 14:27:12 +00:00
|
|
|
}
|
|
|
|
|
//! [startClient]
|
|
|
|
|
|
|
|
|
|
//! [stopClient]
|
|
|
|
|
void ChatClient::stopClient()
|
|
|
|
|
{
|
|
|
|
|
delete socket;
|
2018-08-14 08:38:48 +00:00
|
|
|
socket = nullptr;
|
2011-12-09 14:27:12 +00:00
|
|
|
}
|
|
|
|
|
//! [stopClient]
|
|
|
|
|
|
|
|
|
|
//! [readSocket]
|
|
|
|
|
void ChatClient::readSocket()
|
|
|
|
|
{
|
|
|
|
|
if (!socket)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
while (socket->canReadLine()) {
|
2021-02-12 15:14:25 +00:00
|
|
|
QByteArray line = socket->readLine().trimmed();
|
2011-12-09 14:27:12 +00:00
|
|
|
emit messageReceived(socket->peerName(),
|
|
|
|
|
QString::fromUtf8(line.constData(), line.length()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//! [readSocket]
|
|
|
|
|
|
|
|
|
|
//! [sendMessage]
|
|
|
|
|
void ChatClient::sendMessage(const QString &message)
|
|
|
|
|
{
|
|
|
|
|
QByteArray text = message.toUtf8() + '\n';
|
|
|
|
|
socket->write(text);
|
|
|
|
|
}
|
|
|
|
|
//! [sendMessage]
|
|
|
|
|
|
2018-08-14 08:38:48 +00:00
|
|
|
void ChatClient::onSocketErrorOccurred(QBluetoothSocket::SocketError error)
|
|
|
|
|
{
|
2021-03-02 11:36:01 +00:00
|
|
|
if (error == QBluetoothSocket::SocketError::NoSocketError)
|
2018-08-14 08:38:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketError>();
|
2023-03-16 17:26:38 +00:00
|
|
|
QString errorString = socket->peerName() + ' '_L1
|
|
|
|
|
+ metaEnum.valueToKey(static_cast<int>(error)) + " occurred"_L1;
|
2018-08-14 08:38:48 +00:00
|
|
|
|
|
|
|
|
emit socketErrorOccurred(errorString);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-09 14:27:12 +00:00
|
|
|
//! [connected]
|
|
|
|
|
void ChatClient::connected()
|
|
|
|
|
{
|
|
|
|
|
emit connected(socket->peerName());
|
|
|
|
|
}
|
|
|
|
|
//! [connected]
|