summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-11-15 15:07:46 +0100
committerIvan Solovev <ivan.solovev@qt.io>2021-11-25 06:21:03 +0000
commit22658a768a6db420830eb4020232c853f88bbb02 (patch)
tree1c0c042c12543fb45bfe6368faf35be87f82af34
parentc7b918a52c86bb4f51cf6ac9bde99a28699a6bf2 (diff)
BtChat: fix crash when no adapter is found
The example was assuming that QBluetoothLocalDevice::allDevices() always returns a non-empty list. This is not true for the cases when desktop has no bluetooth adapters, as well as for WinRT implementation (it always returns empty list for now). This patch fixes the crash and provides some information messages to the console. Note that WinRT implementation has an extended warning, because it returns an empty list even if the adapter actually exists. Fixes: QTBUG-98323 Change-Id: Iaa81465cf6be2febf20526c11909667fd60728b3 Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi> Reviewed-by: Alex Blasche <alexander.blasche@qt.io> (cherry picked from commit adcfc156f437b766b7a922d0350d7f03da5c36bd) Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
-rw-r--r--examples/bluetooth/btchat/chat.cpp14
1 files changed, 12 insertions, 2 deletions
diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp
index 5df2ed67..c3846319 100644
--- a/examples/bluetooth/btchat/chat.cpp
+++ b/examples/bluetooth/btchat/chat.cpp
@@ -91,8 +91,18 @@ Chat::Chat(QWidget *parent)
}
// make discoverable
- QBluetoothLocalDevice adapter(localAdapters.at(0).address());
- adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
+ if (!localAdapters.isEmpty()) {
+ QBluetoothLocalDevice adapter(localAdapters.at(0).address());
+ adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
+ } else {
+ qWarning("Local adapter is not found! The application might work incorrectly.");
+#ifdef Q_OS_WIN
+ // WinRT implementation does not support adapter information yet. So it
+ // will always return an empty list.
+ qWarning("If the adapter exists, make sure to pair the devices manually before launching"
+ " the chat.");
+#endif
+ }
//! [Create Chat Server]
server = new ChatServer(this);