diff options
author | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-21 16:33:48 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-23 12:48:08 +0100 |
commit | 2aca0f85a8c3b4f0543e55b9f7aa9aa635fb5e9e (patch) | |
tree | 2092cf44325cc86d821600615f2edea8188d38cb /src/bluetooth/qlowenergycontroller_android.cpp | |
parent | 19d4509608f8f8c3a63cd77fb5c5ad598878b001 (diff) |
Workaround Android when connecting to non-existing/turned off device
In such cases a disconnect() call never receives a system callback that
BluetoothGatt is disconnected. Similarly when the timeout hits
the platform sends an unknown error code 133 and again doesn't send
a disconnected status.
This patch works around both issues by preempting the platform behavior.
It seems that subsequent connect attempts are not harmed by this.
Change-Id: I14326c9169f8c1cbbe8aa11de62ca807e79f4975
Reviewed-by: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/bluetooth/qlowenergycontroller_android.cpp')
-rw-r--r-- | src/bluetooth/qlowenergycontroller_android.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp index 868457e9..a28feaf6 100644 --- a/src/bluetooth/qlowenergycontroller_android.cpp +++ b/src/bluetooth/qlowenergycontroller_android.cpp @@ -97,9 +97,20 @@ void QLowEnergyControllerPrivate::connectToDevice() void QLowEnergyControllerPrivate::disconnectFromDevice() { + /* Catch an Android timeout bug. If the device is connecting but cannot + * physically connect it seems to ignore the disconnect call below. + * At least BluetoothGattCallback.onConnectionStateChange never + * arrives. The next BluetoothGatt.connect() works just fine though. + * */ + + QLowEnergyController::ControllerState oldState = state; setState(QLowEnergyController::ClosingState); + if (hub) hub->javaObject().callMethod<void>("disconnect"); + + if (oldState == QLowEnergyController::ConnectingState) + setState(QLowEnergyController::UnconnectedState); } void QLowEnergyControllerPrivate::discoverServices() @@ -234,8 +245,18 @@ void QLowEnergyControllerPrivate::connectionUpdated( if (errorCode != QLowEnergyController::NoError) { // ConnectionError if transition from Connecting to Connected - if (oldState == QLowEnergyController::ConnectingState) + if (oldState == QLowEnergyController::ConnectingState) { setError(QLowEnergyController::ConnectionError); + /* There is a bug in Android, when connecting to an unconnectable + * device. The connection times out and Android sends error code + * 133 (doesn't exist) and STATE_CONNECTED. A subsequent disconnect() + * call never sends a STATE_DISCONNECTED either. + * As workaround we will trigger disconnect when we encounter + * error during connect attempt. This leaves the controller + * in a cleaner state. + * */ + newState = QLowEnergyController::UnconnectedState; + } else setError(errorCode); } |