From 65e0713bc31ac5d1889dbf7b75e540f314f67775 Mon Sep 17 00:00:00 2001 From: Jonathan Bagg Date: Mon, 22 May 2023 15:48:52 -0400 Subject: [PATCH] Android: Call nsd.stopServiceDiscovery() when going to sleep If Android is on it's way to suspend when stopBrowser() is called, we need to call nsd.stopServiceDiscovery() synchronously to force it to run before the device goes to sleep. Was an issue on Android 10 on Levono Tab M10 FHD Plus TB-X606FA_S300554-220630_BMP --- androidnsd.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/androidnsd.cpp b/androidnsd.cpp index 3933daf..2fd25e8 100644 --- a/androidnsd.cpp +++ b/androidnsd.cpp @@ -123,9 +123,15 @@ void QZeroConfPrivate::startBrowser(QString type, QAbstractSocket::NetworkLayerP void QZeroConfPrivate::stopBrowser() { QAndroidJniObject ref(nsdManager); - QtAndroid::runOnAndroidThread([ref]() { + // If Android is on it's way to suspend when stopBrowser() is called, we need to call nsd.stopServiceDiscovery() synchronously + // to force it to run before the device goes to sleep. + if (qGuiApp->applicationState() == Qt::ApplicationSuspended) { ref.callMethod("stopServiceDiscovery"); - }); + } else { + QtAndroid::runOnAndroidThread([ref]() { + ref.callMethod("stopServiceDiscovery"); + }); + } } // Callbacks will come in from the android thread. So we're never accessing any of our members directly but instead