diff options
author | Andreas Buhr <andreas.buhr@qt.io> | 2021-07-09 11:20:16 +0200 |
---|---|---|
committer | Andreas Buhr <andreas.buhr@qt.io> | 2021-07-12 16:21:13 +0200 |
commit | 52fdaeec67e87250f41ecf64901f2401a9a49091 (patch) | |
tree | f1c6935ddd6360f623dda42598f7c044424e9182 | |
parent | 330439186653e3557643424cc13ab437cc01f7dd (diff) |
Improve detection of enabled location service on SDK<28
Bluetooth seems to be non-functional when location services are
disabled. This is not documented in Android's documentations,
but several bug entries exist about this which are closed
as "works as intended". See the linked bug entry for details.
Our code thus tries to detect whether location services are enabled.
This patch improves the detection of enabled location services.
While the previous code did only check for GPS and Network
location providers, the new code should detect any enabled
location provider.
Fixes: QTBUG-90760
Change-Id: I0c670296d0af62161dbc3fca40889996fa91f0ca
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
(cherry picked from commit 4fdcd2d8d2c2b47ec142b744ad291c1e134db7d4)
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp index 2ca6f881..92140ced 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp @@ -198,18 +198,15 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent if (QtAndroid::androidSdkVersion() >= 28) { locationTurnedOn = bool(locService.callMethod<jboolean>("isLocationEnabled")); } else { - // try GPS and network provider - QAndroidJniObject provider = QAndroidJniObject::getStaticObjectField( - "android/location/LocationManager", "GPS_PROVIDER", "Ljava/lang/String;"); - bool gpsTurnedOn = bool(locService.callMethod<jboolean>("isProviderEnabled", - "(Ljava/lang/String;)Z", provider.object<jstring>())); - - provider = QAndroidJniObject::getStaticObjectField( - "android/location/LocationManager", "NETWORK_PROVIDER", "Ljava/lang/String;"); - bool providerTurnedOn = bool(locService.callMethod<jboolean>("isProviderEnabled", - "(Ljava/lang/String;)Z", provider.object<jstring>())); - - locationTurnedOn = gpsTurnedOn || providerTurnedOn; + // check whether there is any enabled provider + QAndroidJniObject listOfEnabledProviders = + locService.callObjectMethod("getProviders", "(Z)Ljava/util/List;", true); + + if (listOfEnabledProviders.isValid()) { + int size = listOfEnabledProviders.callMethod<jint>("size", "()I"); + locationTurnedOn = size > 0; + qCDebug(QT_BT_ANDROID) << size << "enabled location providers detected."; + } } } |