From 92bea4cac7b0f8a3c93ba6b6dfb53218bcc8d4df Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 31 Mar 2023 14:59:45 +0200 Subject: BtChat: adapt to using new QBluetoothPermission Also add missing Info.plist files Task-number: QTBUG-109964 Change-Id: Icf1fdf1353898538ce42a27cb0b0cb01c0823e26 Reviewed-by: Juha Vuolle --- examples/bluetooth/btchat/CMakeLists.txt | 14 +++++++++++ examples/bluetooth/btchat/btchat.pro | 3 +++ examples/bluetooth/btchat/chat.cpp | 41 +++++++++++++++++++++++++++----- examples/bluetooth/btchat/chat.h | 2 ++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/examples/bluetooth/btchat/CMakeLists.txt b/examples/bluetooth/btchat/CMakeLists.txt index ef578b88..6d2bd0b9 100644 --- a/examples/bluetooth/btchat/CMakeLists.txt +++ b/examples/bluetooth/btchat/CMakeLists.txt @@ -33,6 +33,20 @@ target_link_libraries(btchat PRIVATE Qt::Widgets ) +if (APPLE) + # Using absolute path for shared plist files is a Ninja bug workaround + get_filename_component(SHARED_PLIST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../shared ABSOLUTE) + if (IOS) + set_target_properties(btchat PROPERTIES + MACOSX_BUNDLE_INFO_PLIST "${SHARED_PLIST_DIR}/Info.cmake.ios.plist" + ) + else() + set_target_properties(btchat PROPERTIES + MACOSX_BUNDLE_INFO_PLIST "${SHARED_PLIST_DIR}/Info.cmake.macos.plist" + ) + endif() +endif() + install(TARGETS btchat RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/bluetooth/btchat/btchat.pro b/examples/bluetooth/btchat/btchat.pro index bbf9ed18..7e0c976f 100644 --- a/examples/bluetooth/btchat/btchat.pro +++ b/examples/bluetooth/btchat/btchat.pro @@ -21,5 +21,8 @@ FORMS = \ chat.ui \ remoteselector.ui +ios: QMAKE_INFO_PLIST = ../shared/Info.qmake.ios.plist +macos: QMAKE_INFO_PLIST = ../shared/Info.qmake.macos.plist + target.path = $$[QT_INSTALL_EXAMPLES]/bluetooth/btchat INSTALLS += target diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp index a9545cf6..288f4641 100644 --- a/examples/bluetooth/btchat/chat.cpp +++ b/examples/bluetooth/btchat/chat.cpp @@ -13,6 +13,12 @@ #include #include +#if QT_CONFIG(permissions) +#include +#include +#include +#endif + using namespace Qt::StringLiterals; static constexpr auto serviceUuid = "e8e10f95-1a70-4b27-9ccf-02010264e9c8"_L1; @@ -32,6 +38,35 @@ Chat::Chat(QWidget *parent) //! [Construct UI] ui->connectButton->setFocus(); + initBluetooth(); +} + +Chat::~Chat() +{ + qDeleteAll(clients); + delete ui; +} + +void Chat::initBluetooth() +{ +#if QT_CONFIG(permissions) + QBluetoothPermission permission{}; + switch (qApp->checkPermission(permission)) { + case Qt::PermissionStatus::Undetermined: + qApp->requestPermission(permission, this, &Chat::initBluetooth); + return; + case Qt::PermissionStatus::Denied: + QMessageBox::warning(this, tr("Missing permissions"), + tr("Permissions are needed to use Bluetooth. " + "Please grant the permissions to this " + "application in the system settings.")); + qApp->quit(); + return; + case Qt::PermissionStatus::Granted: + break; // proceed to initialization + } +#endif // QT_CONFIG(permissions) + localAdapters = QBluetoothLocalDevice::allDevices(); if (localAdapters.size() < 2) { ui->localAdapterBox->setVisible(false); @@ -71,12 +106,6 @@ Chat::Chat(QWidget *parent) //! [Get local device name] } -Chat::~Chat() -{ - qDeleteAll(clients); - delete ui; -} - //! [clientConnected clientDisconnected] void Chat::clientConnected(const QString &name) { diff --git a/examples/bluetooth/btchat/chat.h b/examples/bluetooth/btchat/chat.h index 1c9d3211..d9a22ca7 100644 --- a/examples/bluetooth/btchat/chat.h +++ b/examples/bluetooth/btchat/chat.h @@ -40,6 +40,8 @@ private slots: void newAdapterSelected(); + void initBluetooth(); + private: int adapterFromUserSelection() const; int currentAdapterIndex = 0; -- cgit v1.2.1