summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@insta.fi>2022-06-20 10:51:40 +0300
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-06-22 06:09:22 +0000
commit1d5d325b8c1b2c086f8a1e1bc66779e0af009ec1 (patch)
tree1c788f440c40656162be2903d1ba918d0c6ba5a5
parentd7f42f2248d883978ae4641fbcb80d5512fcfb1c (diff)
downloadqtconnectivity-1d5d325b8c1b2c086f8a1e1bc66779e0af009ec1.tar.gz
Android BT LE advertisement start to fail when bluetooth is OFF
When we start a BT LE advertisement while bluetooth is OFF, the advertisement should fail to start, and on the other hand when bluetooth is switched back ON, starting the advertisement should be possible again. This commit adds offline/online check to the startAdvertising function. In addition the creation of the advertiser is delayed until the first advertisement request. This delaying is done for the case when the bluetooth is OFF when the LE controller is created => we don't want the creation of advertisement to fail once during construction and therefore become unusable even if bluetooth is later switched ON. For completeness it should be mentioned that Android documentation mentions that the getBluetoothLeAdvertiser() returns 'null' if the bluetooth is offline. However on all the devices I've tried this, it returns a valid instance. This means that the creation delay of this commit would not be strictly speaking necessary. But we shouldn't rely on it: on some Android devices the advertiser might be returned as null when bluetooth is offline, as documented. Fixes: QTBUG-104106 Change-Id: Ia016e4534c29fac23f42785d68bc95d568c41def Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 4292b32d57f7b6dc2f644ee75403ce8b26f9e1e5) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothLEServer.java21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothLEServer.java b/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothLEServer.java
index 025c874b..f3cc3041 100644
--- a/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothLEServer.java
+++ b/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothLEServer.java
@@ -298,12 +298,7 @@ public class QtBluetoothLEServer {
return;
}
- mLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
-
- if (!mBluetoothAdapter.isMultipleAdvertisementSupported())
- Log.w(TAG, "Device does not support Bluetooth Low Energy advertisement.");
- else
- Log.w(TAG, "Let's do BTLE Peripheral.");
+ Log.w(TAG, "Let's do BTLE Peripheral.");
}
// The following functions are synchronized callback handlers. The callbacks
@@ -779,8 +774,20 @@ public class QtBluetoothLEServer {
AdvertiseData scanResponse,
AdvertiseSettings settings)
{
- if (mLeAdvertiser == null)
+ // Check that the bluetooth is on
+ if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
+ Log.w(TAG, "StartAdvertising: Bluetooth not available or offline");
return false;
+ }
+
+ // According to Android doc this check should always precede the advertiser creation
+ if (mLeAdvertiser == null && mBluetoothAdapter.isMultipleAdvertisementSupported())
+ mLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
+
+ if (mLeAdvertiser == null) {
+ Log.w(TAG, "StartAdvertising: LE advertisement not supported");
+ return false;
+ }
if (!connectServer()) {
Log.w(TAG, "Server::startAdvertising: Cannot open GATT server");