summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth/btchat/chat.cpp
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-16 09:58:31 +0100
commitadcfc156f437b766b7a922d0350d7f03da5c36bd (patch)
treeb17f649f1ec86aa3c0830991413fc99e98262b53 /examples/bluetooth/btchat/chat.cpp
parent3753c53fc810c292db93fcfbafffbc097afb0ed1 (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 Pick-to: 6.2 Change-Id: Iaa81465cf6be2febf20526c11909667fd60728b3 Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples/bluetooth/btchat/chat.cpp')
-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);