From df289a72abe1fc46b5b029b2a6e27f80a198dc9f Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 21 Mar 2023 16:24:28 +0100 Subject: Remove PingPong example The example contains a lot of rather complicated code, which is not directly related to the demonstrated Bluetooth features. Remove it in favor of BtChat example, which demonstrates the same Bluetooth features in a much more compact and understandable way. Task-number: QTBUG-111972 Pick-to: 6.5 6.5.0 Change-Id: Iccb2598fa6e88816697286b2252858f35a635ee5 Reviewed-by: Juha Vuolle --- examples/bluetooth/CMakeLists.txt | 1 - examples/bluetooth/bluetooth.pro | 3 +- examples/bluetooth/pingpong/CMakeLists.txt | 54 --- examples/bluetooth/pingpong/assets/Board.qml | 147 ------- examples/bluetooth/pingpong/assets/Dialog.qml | 24 -- examples/bluetooth/pingpong/assets/Menu.qml | 84 ---- examples/bluetooth/pingpong/assets/main.qml | 26 -- examples/bluetooth/pingpong/doc/images/intro1.png | Bin 11080 -> 0 bytes .../pingpong/doc/images/qmlpingpong_intro.png | Bin 36557 -> 0 bytes examples/bluetooth/pingpong/doc/src/pingpong.qdoc | 58 --- examples/bluetooth/pingpong/main.cpp | 20 - examples/bluetooth/pingpong/pingpong.cpp | 440 --------------------- examples/bluetooth/pingpong/pingpong.h | 93 ----- examples/bluetooth/pingpong/pingpong.pro | 19 - examples/bluetooth/pingpong/resource.qrc | 8 - 15 files changed, 1 insertion(+), 976 deletions(-) delete mode 100644 examples/bluetooth/pingpong/CMakeLists.txt delete mode 100644 examples/bluetooth/pingpong/assets/Board.qml delete mode 100644 examples/bluetooth/pingpong/assets/Dialog.qml delete mode 100644 examples/bluetooth/pingpong/assets/Menu.qml delete mode 100644 examples/bluetooth/pingpong/assets/main.qml delete mode 100644 examples/bluetooth/pingpong/doc/images/intro1.png delete mode 100644 examples/bluetooth/pingpong/doc/images/qmlpingpong_intro.png delete mode 100644 examples/bluetooth/pingpong/doc/src/pingpong.qdoc delete mode 100644 examples/bluetooth/pingpong/main.cpp delete mode 100644 examples/bluetooth/pingpong/pingpong.cpp delete mode 100644 examples/bluetooth/pingpong/pingpong.h delete mode 100644 examples/bluetooth/pingpong/pingpong.pro delete mode 100644 examples/bluetooth/pingpong/resource.qrc (limited to 'examples') diff --git a/examples/bluetooth/CMakeLists.txt b/examples/bluetooth/CMakeLists.txt index de0996cc..6582092b 100644 --- a/examples/bluetooth/CMakeLists.txt +++ b/examples/bluetooth/CMakeLists.txt @@ -6,7 +6,6 @@ if(TARGET Qt::Widgets) qt_internal_add_example(btchat) endif() if(TARGET Qt::Quick) - qt_internal_add_example(pingpong) qt_internal_add_example(lowenergyscanner) qt_internal_add_example(heartrate-game) endif() diff --git a/examples/bluetooth/bluetooth.pro b/examples/bluetooth/bluetooth.pro index 73b78477..7b2484da 100644 --- a/examples/bluetooth/bluetooth.pro +++ b/examples/bluetooth/bluetooth.pro @@ -6,6 +6,5 @@ qtHaveModule(widgets) { SUBDIRS += btchat } -qtHaveModule(quick): SUBDIRS += pingpong \ - lowenergyscanner \ +qtHaveModule(quick): SUBDIRS += lowenergyscanner \ heartrate-game diff --git a/examples/bluetooth/pingpong/CMakeLists.txt b/examples/bluetooth/pingpong/CMakeLists.txt deleted file mode 100644 index cf2c4cba..00000000 --- a/examples/bluetooth/pingpong/CMakeLists.txt +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(btpingpong LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/bluetooth/pingpong") - -find_package(Qt6 REQUIRED COMPONENTS Bluetooth Core Gui Quick) - -qt_add_executable(btpingpong - main.cpp - pingpong.cpp pingpong.h -) - -set_target_properties(btpingpong PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE - OUTPUT_NAME "pingpong" -) - -target_link_libraries(btpingpong PUBLIC - Qt::Bluetooth - Qt::Core - Qt::Gui - Qt::Quick -) - -# Resources: -set(resource_resource_files - "assets/Board.qml" - "assets/Dialog.qml" - "assets/Menu.qml" - "assets/main.qml" -) - -qt6_add_resources(btpingpong "resource" - PREFIX - "/" - FILES - ${resource_resource_files} -) - -install(TARGETS btpingpong - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/bluetooth/pingpong/assets/Board.qml b/examples/bluetooth/pingpong/assets/Board.qml deleted file mode 100644 index 8d0d99c7..00000000 --- a/examples/bluetooth/pingpong/assets/Board.qml +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import QtQuick 2.0 -import QtQuick.Window 2.1 - -Rectangle { - id: fullWindow - anchors.fill: parent - color: "black" - - property double scaleFactor: Math.min(width, height) - - Rectangle { - id: board - width: scaleFactor - height: scaleFactor - anchors.centerIn: parent - - // Left pedal - server role - Rectangle { - id: leftblock - y: (parent.height/2) - width: (parent.width/27) - height: (parent.height/5) - anchors.left: parent.left - color: "#363636" - radius: width/2 - - MouseArea { - id: leftMouse - width: (board.width/2) - height: parent.height - anchors.horizontalCenter: parent.horizontalCenter - acceptedButtons: Qt.LeftButton - drag.target: leftblock - drag.axis: Drag.YAxis - drag.minimumY: 0 - drag.maximumY: (board.height - leftblock.height) - } - } - - // Right pedal - client role - Rectangle { - id: rightblock - y: (parent.height/2) - width: (parent.width/27) - height: (parent.height/5) - anchors.right: parent.right - color: "#363636" - radius: width/2 - - MouseArea { - id: rightMouse - width: (board.width/2) - height: parent.height - anchors.horizontalCenter: parent.horizontalCenter - acceptedButtons: Qt.LeftButton - drag.target: rightblock - drag.axis: Drag.YAxis - drag.minimumY: 0 - drag.maximumY: (board.height - rightblock.height) - } - } - - Rectangle { - id: splitter - color: "#363636" - anchors.horizontalCenter: parent.horizontalCenter - height: parent.height - width: parent.width/100 - } - - Text { - id: leftResult - text: pingPong.leftResult - font.bold: true - font.pixelSize: 30 - anchors.right: splitter.left - anchors.top: parent.top - anchors.margins: 15 - } - - Text { - id: rightResult - text: pingPong.rightResult - font.bold: true - font.pixelSize: 30 - anchors.left: splitter.right - anchors.top: parent.top - anchors.margins: 15 - } - - Rectangle { - id: ball - width: leftblock.width - height: leftblock.width - radius: width/2 - color: "#363636" - x: pingPong.ballX * scaleFactor - y: pingPong.ballY * scaleFactor - } - } - - // 1 - server role; left pedal - // 2 - client role; right pedal - property int roleSide: pingPong.role - onRoleSideChanged: { - if (pingPong.role == 1) { - rightMouse.opacity = 0.7 - rightMouse.enabled = false - } - else if (pingPong.role == 2) { - leftMouse.opacity = 0.7 - leftMouse.enabled = false - } - } - - property bool deviceMessage: pingPong.showDialog - onDeviceMessageChanged: { - if (pingPong.showDialog) { - info.visible = true; - board.opacity = 0.5; - } else { - info.visible = false; - board.opacity = 1; - } - } - - property double leftBlockY: leftblock.y - onLeftBlockYChanged: pingPong.updateLeftBlock(leftblock.y / scaleFactor) - - property double leftBlockUpdate: pingPong.leftBlockY - onLeftBlockUpdateChanged: leftblock.y = pingPong.leftBlockY * scaleFactor - - property double rightBlockY: rightblock.y - onRightBlockYChanged: pingPong.updateRightBlock(rightblock.y / scaleFactor) - - property double rightBlockUpdate: pingPong.rightBlockY - onRightBlockUpdateChanged: rightblock.y = pingPong.rightBlockY * scaleFactor - - - Component.onCompleted: { - pingPong.updateLeftBlock(leftblock.y / scaleFactor) - pingPong.updateRightBlock(rightblock.y / scaleFactor) - } -} diff --git a/examples/bluetooth/pingpong/assets/Dialog.qml b/examples/bluetooth/pingpong/assets/Dialog.qml deleted file mode 100644 index 02575036..00000000 --- a/examples/bluetooth/pingpong/assets/Dialog.qml +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import QtQuick 2.0 - -Rectangle { - width: parent.width/2 - height: message.implicitHeight*2 - z: 50 - border.width: 2 - border.color: "#363636" - radius: 10 - - Text { - id: message - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - anchors.fill: parent - wrapMode: Text.WordWrap - elide: Text.ElideMiddle - text: pingPong.message - color: "#363636" - } -} diff --git a/examples/bluetooth/pingpong/assets/Menu.qml b/examples/bluetooth/pingpong/assets/Menu.qml deleted file mode 100644 index c5cd6d9f..00000000 --- a/examples/bluetooth/pingpong/assets/Menu.qml +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import QtQuick 2.0 - -Rectangle { - anchors.fill: parent - - - Rectangle { - width: parent.width - height: headerText.implicitHeight *1.2 - border.width: 1 - border.color: "#363636" - radius: 5 - - Text { - id: headerText - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - anchors.fill: parent - text: "Welcome to PingPong Game \n Please select an option" - font.pointSize: 20 - elide: Text.ElideMiddle - color: "#363636" - } - } - - Rectangle { - id: startServer - anchors.centerIn: parent - width: parent.width/2 - height: startServerText.implicitHeight*5 - color: "#363636" - - Text { - id: startServerText - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - anchors.fill: parent - font.bold: true - text: "Start PingPong server" - color: "#E3E3E3" - wrapMode: Text.WordWrap - } - - MouseArea { - anchors.fill: parent - onClicked: { - pageLoader.source = "Board.qml"; - pingPong.startServer(); - } - } - } - - Rectangle { - id: startClient - anchors.horizontalCenter: parent.horizontalCenter - anchors.top: startServer.bottom - anchors.topMargin: 10 - width: parent.width/2 - height: startClientText.implicitHeight*5 - color: "#363636" - - Text { - id: startClientText - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - anchors.fill: parent - font.bold: true - text: "Start PingPong client" - color: "#E3E3E3" - wrapMode: Text.WordWrap - } - - MouseArea { - anchors.fill: parent - onClicked: { - pageLoader.source = "Board.qml"; - pingPong.startClient() - } - } - } -} diff --git a/examples/bluetooth/pingpong/assets/main.qml b/examples/bluetooth/pingpong/assets/main.qml deleted file mode 100644 index 77cf723b..00000000 --- a/examples/bluetooth/pingpong/assets/main.qml +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -import QtQuick 2.1 -import QtQuick.Window 2.1 - -Window { - id: menulist - width: 600 - height: 600 - visibility: (Qt.platform.os === "android" || Qt.platform.os === "ios") ? Window.FullScreen : Window.Windowed - visible: true - - Dialog { - id: info - anchors.centerIn: parent - visible: false - } - - Component.onCompleted: pageLoader.source = "Menu.qml" - - Loader { - id: pageLoader - anchors.fill: parent - } -} diff --git a/examples/bluetooth/pingpong/doc/images/intro1.png b/examples/bluetooth/pingpong/doc/images/intro1.png deleted file mode 100644 index 4497b976..00000000 Binary files a/examples/bluetooth/pingpong/doc/images/intro1.png and /dev/null differ diff --git a/examples/bluetooth/pingpong/doc/images/qmlpingpong_intro.png b/examples/bluetooth/pingpong/doc/images/qmlpingpong_intro.png deleted file mode 100644 index 5655cfbf..00000000 Binary files a/examples/bluetooth/pingpong/doc/images/qmlpingpong_intro.png and /dev/null differ diff --git a/examples/bluetooth/pingpong/doc/src/pingpong.qdoc b/examples/bluetooth/pingpong/doc/src/pingpong.qdoc deleted file mode 100644 index a91b8f2e..00000000 --- a/examples/bluetooth/pingpong/doc/src/pingpong.qdoc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example pingpong - \title Bluetooth QML Ping Pong example - \brief A QML example showing Bluetooth communication. - - The Bluetooth QML Ping Pong example presents the socket communication between two - Bluetooth devices. The basic concept is the ping pong game where two players - communicate via sockets. - - \image qmlpingpong_intro.png - - \include examples-run.qdocinc - - At the beginning, the user selects the role. One device acts as a server and the second - one as a client. The server side starts a service named "PingPong server". - - \snippet pingpong/pingpong.cpp Starting the server - - On the client side, the full service discovery on the nearby Bluetooth devices is done. - - \snippet pingpong/pingpong.cpp Searching for the service - - When the ping pong service is discovered, the client connects to the server using the socket. - - \snippet pingpong/pingpong.cpp Connecting the socket - - On the server side, the connected signal is emitted initiating that the client is connected. - The necessary signals and slots on the server side are connected. - - \snippet pingpong/pingpong.cpp Initiating server socket - - The game starts after the devices are connected and the screen is adjusted. - - \snippet pingpong/pingpong.cpp Start the game - - The server updates the ball direction and coordinates. The coordinates of pedals are sent - to each other every 10ms. - - \snippet pingpong/pingpong.cpp Updating coordinates - - The coordinates are updated and exchanged via sockets. As presented, the server sends its - pedal's y coordinate and the ball coordinates whereas, the client sends only its pedal - y coordinate. - - \snippet pingpong/pingpong.cpp Checking the boundaries - - In the code above, it was shown how the server checks whether the ball has reached - the boundaries of the board. In case of a collision with a pedal, a convex surface - is simulated. The part of the velocity normal to the simulated surface is inverted - and increased by 10% to speed up the game. In the case of the goal, the server updates - the results via its socket. - - \image intro1.png - -*/ diff --git a/examples/bluetooth/pingpong/main.cpp b/examples/bluetooth/pingpong/main.cpp deleted file mode 100644 index 8df300ec..00000000 --- a/examples/bluetooth/pingpong/main.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include -#include -#include -#include -#include "pingpong.h" - - -int main(int argc, char *argv[]) -{ - // QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); - QGuiApplication app(argc, argv); - PingPong pingPong; - QQmlApplicationEngine engine; - engine.rootContext()->setContextProperty("pingPong", &pingPong); - engine.load(QUrl("qrc:/assets/main.qml")); - return app.exec(); -} diff --git a/examples/bluetooth/pingpong/pingpong.cpp b/examples/bluetooth/pingpong/pingpong.cpp deleted file mode 100644 index abea85dc..00000000 --- a/examples/bluetooth/pingpong/pingpong.cpp +++ /dev/null @@ -1,440 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "pingpong.h" -#include -#include - -PingPong::PingPong() -{ - m_timer = new QTimer(this); - connect(m_timer, &QTimer::timeout, this, &PingPong::update); -} - -PingPong::~PingPong() -{ - m_timer->stop(); - delete socket; - delete discoveryAgent; -} - -void PingPong::startGame() -{ - m_resultLeft = m_resultRight = 0; - emit resultChanged(); - m_showDialog = false; - emit showDialogChanged(); - //! [Start the game] - m_timer->start(10); - //! [Start the game] -} - -void PingPong::update() -{ - QByteArray size; - // Server is only updating the coordinates - //! [Updating coordinates] - if (m_role == 1) { - checkBoundaries(); - m_ballY += m_speedy; - m_ballX += m_speedx; - - size.setNum(m_ballX); - size.append(' '); - QByteArray size1; - size1.setNum(m_ballY); - size.append(size1); - size.append(' '); - size1.setNum(m_leftBlockY); - size.append(size1); - size.append(" \n"); - socket->write(size.constData()); - emit ballChanged(); - } - else if (m_role == 2) { - size.setNum(m_rightBlockY); - size.append(" \n"); - socket->write(size.constData()); - } - //! [Updating coordinates] -} - -void PingPong::updateBall(const float &bX, const float &bY) -{ - m_ballX = bX; - m_ballY = bY; -} - -void PingPong::updateLeftBlock(const float &lY) -{ - m_leftBlockY = lY; -} - -void PingPong::updateRightBlock(const float &rY) -{ - m_rightBlockY = rY; -} - -void PingPong::checkBoundaries() -{ - float ballWidth = 1.f / 27; - float blockSize = 1.f / 27; - float blockHeight = 1.f / 5; - - float ballCenterY = m_ballY + ballWidth / 2.; - //! [Checking the boundaries] - if (((m_ballX + ballWidth) > (1. - blockSize)) && (ballCenterY < (m_rightBlockY + blockHeight)) - && (ballCenterY > m_rightBlockY)) { - // right paddle collision - // simulating paddle surface to be a quarter of a circle - float paddlecenter = m_rightBlockY + blockHeight / 2.; - float relpos = (ballCenterY - paddlecenter) / (blockHeight / 2.); // [-1 : 1] - float surfaceangle = M_PI_4 * relpos; - - // calculation of surface normal - float normalx = -cos(surfaceangle); - float normaly = sin(surfaceangle); - - // calculation of surface tangent - float tangentx = sin(surfaceangle); - float tangenty = cos(surfaceangle); - - // calculation of tangentialspeed - float tangentialspeed = tangentx * m_speedx + tangenty * m_speedy; - // calculation of normal speed - float normalspeed = normalx * m_speedx + normaly * m_speedy; - - // speed increase of 10% - normalspeed *= 1.1f; - - if (normalspeed < 0) { // if we are coming from the left. To avoid double reflections - m_speedx = tangentialspeed * tangentx - normalspeed * normalx; - m_speedy = tangentialspeed * tangenty - normalspeed * normaly; - } - } else if ((m_ballX < blockSize) && (ballCenterY < (m_leftBlockY + blockHeight)) - && (ballCenterY > m_leftBlockY)) { - // left paddle collision - // simulating paddle surface to be a quarter of a circle - float paddlecenter = m_leftBlockY + blockHeight / 2.; - float relpos = (ballCenterY - paddlecenter) / (blockHeight / 2.); // [-1 : 1] - float surfaceangle = M_PI_4 * relpos; - - // calculation of surface normal - float normalx = cos(surfaceangle); - float normaly = sin(surfaceangle); - - // calculation of surface tangent - float tangentx = -sin(surfaceangle); - float tangenty = cos(surfaceangle); - - // calculation of tangentialspeed - float tangentialspeed = tangentx * m_speedx + tangenty * m_speedy; - // calculation of normal speed - float normalspeed = normalx * m_speedx + normaly * m_speedy; - - // speed increase of 10% - normalspeed *= 1.1f; - - if (normalspeed < 0) { // if we are coming from the left. To avoid double reflections - m_speedx = tangentialspeed * tangentx - normalspeed * normalx; - m_speedy = tangentialspeed * tangenty - normalspeed * normaly; - } - } else if (m_ballY < 0) { - m_speedy = std::abs(m_speedy); - } else if (m_ballY + ballWidth > 1.f) { - m_speedy = -std::abs(m_speedy); - } - //! [Checking the boundaries] - else if ((m_ballX + ballWidth) > 1.f) { - m_resultLeft++; - m_speedx = -0.002f; - m_speedy = -0.002f; - m_ballX = 0.5f; - m_ballY = 0.9f; - - checkResult(); - QByteArray result; - result.append("result "); - QByteArray res; - res.setNum(m_resultLeft); - result.append(res); - result.append(' '); - res.setNum(m_resultRight); - result.append(res); - result.append(" \n"); - socket->write(result); - qDebug() << result; - emit resultChanged(); - } else if (m_ballX < 0) { - m_resultRight++; - m_speedx = 0.002f; - m_speedy = -0.002f; - m_ballX = 0.5f; - m_ballY = 0.9f; - - checkResult(); - QByteArray result; - result.append("result "); - QByteArray res; - res.setNum(m_resultLeft); - result.append(res); - result.append(' '); - res.setNum(m_resultRight); - result.append(res); - result.append(" \n"); - socket->write(result); - emit resultChanged(); - } -} - -void PingPong::checkResult() -{ - if (m_resultRight < 10 && m_resultLeft < 10) - return; - - if (m_resultRight == 10 && m_role == 2) { - setMessage("Game over. You win! Next game starts in 10s"); - } - else if (m_resultRight == 10 && m_role == 1) { - setMessage("Game over. You lose! Next game starts in 10s"); - } - else if (m_resultLeft == 10 && m_role == 1) { - setMessage("Game over. You win! Next game starts in 10s"); - } - else if (m_resultLeft == 10 && m_role == 2) { - setMessage("Game over. You lose! Next game starts in 10s"); - } - m_timer->stop(); - QTimer::singleShot(10000, this, &PingPong::startGame); -} - -void PingPong::startServer() -{ - setMessage(QStringLiteral("Starting the server")); - - // make discoverable - QBluetoothLocalDevice().setHostMode(QBluetoothLocalDevice::HostDiscoverable); - - //! [Starting the server] - m_serverInfo = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); - connect(m_serverInfo, &QBluetoothServer::newConnection, - this, &PingPong::clientConnected); - connect(m_serverInfo, &QBluetoothServer::errorOccurred, this, &PingPong::serverError); - const QBluetoothUuid uuid(serviceUuid); - - m_serviceInfo = m_serverInfo->listen(uuid, QStringLiteral("PingPong server")); - //! [Starting the server] - if (m_serviceInfo.isValid()) { - setMessage(QStringLiteral("Server started, waiting for the client." - "You are the left player.")); - } - // m_role is set to 1 if it is a server - m_role = 1; - emit roleChanged(); -} - -void PingPong::startClient() -{ - //! [Searching for the service] - discoveryAgent = new QBluetoothServiceDiscoveryAgent(QBluetoothAddress()); - - connect(discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, - this, &PingPong::addService); - connect(discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished, - this, &PingPong::done); - connect(discoveryAgent, &QBluetoothServiceDiscoveryAgent::errorOccurred, this, - &PingPong::serviceScanError); -#ifdef Q_OS_ANDROID - // QTBUG-61392 - discoveryAgent->setUuidFilter(QBluetoothUuid(androidUuid)); -#else - discoveryAgent->setUuidFilter(QBluetoothUuid(serviceUuid)); -#endif - discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); - - //! [Searching for the service] - if (discoveryAgent->error() == QBluetoothServiceDiscoveryAgent::NoError) - setMessage(QStringLiteral("Starting server discovery. You are the right player")); - // m_role is set to 2 if it is a client - m_role = 2; - emit roleChanged(); -} - -void PingPong::clientConnected() -{ - //! [Initiating server socket] - if (!m_serverInfo->hasPendingConnections()) { - setMessage("FAIL: expected pending server connection"); - return; - } - socket = m_serverInfo->nextPendingConnection(); - if (!socket) - return; - socket->setParent(this); - connect(socket, &QBluetoothSocket::readyRead, - this, &PingPong::readSocket); - connect(socket, &QBluetoothSocket::disconnected, - this, &PingPong::clientDisconnected); - connect(socket, &QBluetoothSocket::errorOccurred, this, &PingPong::socketError); - - //! [Initiating server socket] - setMessage(QStringLiteral("Client connected. Get ready!")); - QTimer::singleShot(3000, this, &PingPong::startGame); -} - -void PingPong::clientDisconnected() -{ - setMessage(QStringLiteral("Client disconnected")); - m_timer->stop(); -} - -void PingPong::socketError(QBluetoothSocket::SocketError error) -{ - Q_UNUSED(error); - m_timer->stop(); -} - -void PingPong::serverError(QBluetoothServer::Error error) -{ - setMessage(QStringLiteral("Server error occurred: ") + QVariant::fromValue(error).toString()); - m_timer->stop(); -} - -void PingPong::done() -{ - qDebug() << "Service scan done"; - if (!m_serviceFound && discoveryAgent->error() == QBluetoothServiceDiscoveryAgent::NoError) - setMessage("PingPong service not found"); -} - -void PingPong::addService(const QBluetoothServiceInfo &service) -{ - if (m_serviceFound) - return; - m_serviceFound = true; - - setMessage("Service found. Stopping discovery and creating connection..."); - discoveryAgent->stop(); - //! [Connecting the socket] - socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); - socket->connectToService(service); - - connect(socket, &QBluetoothSocket::readyRead, this, &PingPong::readSocket); - connect(socket, &QBluetoothSocket::connected, this, &PingPong::serverConnected); - connect(socket, &QBluetoothSocket::disconnected, this, &PingPong::serverDisconnected); - //! [Connecting the socket] -} - -void PingPong::serviceScanError(QBluetoothServiceDiscoveryAgent::Error error) -{ - Q_UNUSED(error); - setMessage(QStringLiteral("Scanning error: ") + discoveryAgent->errorString()); -} - -bool PingPong::showDialog() const -{ - return m_showDialog; -} - -QString PingPong::message() const -{ - return m_message; -} - -void PingPong::serverConnected() -{ - setMessage("Server Connected. Get ready!"); - QTimer::singleShot(3000, this, &PingPong::startGame); -} - -void PingPong::serverDisconnected() -{ - setMessage("Server Disconnected"); - m_timer->stop(); -} - -void PingPong::readSocket() -{ - if (!socket) - return; - const char sep = ' '; - QByteArray line; - while (socket->canReadLine()) { - line = socket->readLine(); - //qDebug() << QString::fromUtf8(line.constData(), line.length()); - if (line.contains("result")) { - QList result = line.split(sep); - if (result.size() > 2) { - QByteArray leftSide = result.at(1); - QByteArray rightSide = result.at(2); - m_resultLeft = leftSide.toInt(); - m_resultRight = rightSide.toInt(); - emit resultChanged(); - checkResult(); - } - } - } - if (m_role == 1) { - QList boardSize = line.split(sep); - if (boardSize.size() > 1) { - QByteArray rightBlockY = boardSize.at(0); - m_rightBlockY = rightBlockY.toFloat(); - emit rightBlockChanged(); - } - } else if (m_role == 2) { - QList boardSize = line.split(sep); - if (boardSize.size() > 2) { - QByteArray ballX = boardSize.at(0); - QByteArray ballY = boardSize.at(1); - QByteArray leftBlockY = boardSize.at(2); - m_ballX = ballX.toFloat(); - m_ballY = ballY.toFloat(); - m_leftBlockY = leftBlockY.toFloat(); - emit leftBlockChanged(); - emit ballChanged(); - } - } -} - -void PingPong::setMessage(const QString &message) -{ - m_showDialog = true; - m_message = message; - emit showDialogChanged(); -} - -int PingPong::role() const -{ - return m_role; -} - -int PingPong::leftResult() const -{ - return m_resultLeft; -} - -int PingPong::rightResult() const -{ - return m_resultRight; -} - -float PingPong::ballX() const -{ - return m_ballX; -} - -float PingPong::ballY() const -{ - return m_ballY; -} - - -float PingPong::leftBlockY() const -{ - return m_leftBlockY; -} - -float PingPong::rightBlockY() const -{ - return m_rightBlockY; -} diff --git a/examples/bluetooth/pingpong/pingpong.h b/examples/bluetooth/pingpong/pingpong.h deleted file mode 100644 index 71fdf12b..00000000 --- a/examples/bluetooth/pingpong/pingpong.h +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (C) 2014 BlackBerry Limited. All rights reserved. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef PINGPONG_H -#define PINGPONG_H - -#include -#include -#include -#include -#include -#include - -static QString serviceUuid(QStringLiteral("e8e10f95-1a70-4b27-9ccf-02010264e9c9")); -static QString androidUuid(QStringLiteral("c9e96402-0102-cf9c-274b-701a950fe1e8")); - -class PingPong: public QObject -{ - Q_OBJECT - Q_PROPERTY(float ballX READ ballX NOTIFY ballChanged) - Q_PROPERTY(float ballY READ ballY NOTIFY ballChanged) - Q_PROPERTY(float leftBlockY READ leftBlockY NOTIFY leftBlockChanged) - Q_PROPERTY(float rightBlockY READ rightBlockY NOTIFY rightBlockChanged) - Q_PROPERTY(bool showDialog READ showDialog NOTIFY showDialogChanged) - Q_PROPERTY(QString message READ message NOTIFY showDialogChanged) - Q_PROPERTY(int role READ role NOTIFY roleChanged) - Q_PROPERTY(int leftResult READ leftResult NOTIFY resultChanged) - Q_PROPERTY(int rightResult READ rightResult NOTIFY resultChanged) -public: - PingPong(); - ~PingPong(); - float ballX() const; - float ballY() const; - float leftBlockY() const; - float rightBlockY() const; - void checkBoundaries(); - bool showDialog() const; - QString message() const; - void setMessage(const QString &message); - int role() const; - int leftResult() const; - int rightResult() const; - void checkResult(); - -public slots: - void startGame(); - void update(); - void updateBall(const float &bX, const float &bY); - void updateLeftBlock(const float &lY); - void updateRightBlock(const float &rY); - void startServer(); - void startClient(); - void clientConnected(); - void clientDisconnected(); - void serverConnected(); - void serverDisconnected(); - void socketError(QBluetoothSocket::SocketError); - void serverError(QBluetoothServer::Error); - void serviceScanError(QBluetoothServiceDiscoveryAgent::Error); - void done(); - void addService(const QBluetoothServiceInfo &); - void readSocket(); - -Q_SIGNALS: - void ballChanged(); - void leftBlockChanged(); - void rightBlockChanged(); - void showDialogChanged(); - void roleChanged(); - void resultChanged(); - -private: - QBluetoothServer *m_serverInfo = nullptr; - QBluetoothServiceInfo m_serviceInfo; - QBluetoothSocket *socket = nullptr; - QBluetoothServiceDiscoveryAgent *discoveryAgent = nullptr; - - float m_ballX = 0.5f; - float m_ballY = 0.9f; - float m_leftBlockY = 0.4f; - float m_rightBlockY = 0.4f; - QTimer *m_timer = nullptr; - float m_speedy = 0.002f; - float m_speedx = 0.002f; - int m_resultLeft = 0; - int m_resultRight = 0; - bool m_showDialog = false; - QString m_message; - int m_role = 0; - bool m_serviceFound = false; -}; - -#endif // PINGPONG_H diff --git a/examples/bluetooth/pingpong/pingpong.pro b/examples/bluetooth/pingpong/pingpong.pro deleted file mode 100644 index 07675f27..00000000 --- a/examples/bluetooth/pingpong/pingpong.pro +++ /dev/null @@ -1,19 +0,0 @@ -TEMPLATE = app -TARGET = pingpong - -QT += quick bluetooth - -# Input -SOURCES += main.cpp \ - pingpong.cpp - -OTHER_FILES += assets/*.qml - -RESOURCES += \ - resource.qrc - -HEADERS += \ - pingpong.h - -target.path = $$[QT_INSTALL_EXAMPLES]/bluetooth/pingpong -INSTALLS += target diff --git a/examples/bluetooth/pingpong/resource.qrc b/examples/bluetooth/pingpong/resource.qrc deleted file mode 100644 index 1475a7f3..00000000 --- a/examples/bluetooth/pingpong/resource.qrc +++ /dev/null @@ -1,8 +0,0 @@ - - - assets/main.qml - assets/Board.qml - assets/Dialog.qml - assets/Menu.qml - - -- cgit v1.2.3