From 090bf551308e68b1b2a996c959b608cabd025c5c Mon Sep 17 00:00:00 2001 From: Sanket Agarwal Date: Thu, 21 Apr 2016 14:10:55 -0700 Subject: [PATCH] While turning OFF do not honor ON requests. Native stack does not handle being put from OFF -> ON state without doing a complete cleanup. Hence instead of going from start -> ON -> OFF -> cleanup it goes start -> ON -> OFF ->ON -> ... usually leads to race conditions down the road in native. This patch is a workaround so that we can throw away the requests if we are in currently "turning off" phase. The side-effect would be that user will need to turn it ON again. The race happens when the turn OFF time is longer but usually it is found to be close to order of seconds hence the wait should be bounded. Bug: b/28318203 Change-Id: I14f6633f31311e5b561e1dcbc8a9d6d2a5dd6fdc --- .../bluetooth/LocalBluetoothAdapter.java | 3 ++- .../android/server/BluetoothManagerService.java | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothAdapter.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothAdapter.java index 9c5abf3a00dde..26836099c9de5 100755 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothAdapter.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothAdapter.java @@ -208,7 +208,7 @@ public final class LocalBluetoothAdapter { return false; } - public void setBluetoothEnabled(boolean enabled) { + public boolean setBluetoothEnabled(boolean enabled) { boolean success = enabled ? mAdapter.enable() : mAdapter.disable(); @@ -225,6 +225,7 @@ public final class LocalBluetoothAdapter { syncBluetoothState(); } + return success; } public BluetoothDevice getRemoteDevice(String address) { diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java index 0a814ab579d39..5e8687a904c1c 100644 --- a/services/core/java/com/android/server/BluetoothManagerService.java +++ b/services/core/java/com/android/server/BluetoothManagerService.java @@ -613,7 +613,20 @@ class BluetoothManagerService extends IBluetoothManager.Stub { "Need BLUETOOTH ADMIN permission"); if (DBG) { Slog.d(TAG,"enable(): mBluetooth =" + mBluetooth + - " mBinding = " + mBinding); + " mBinding = " + mBinding + " mState = " + mState); + } + // We do not honor ON requests when the adapter is already turned ON or in the process of + // turning ON. + // As a protective mechanism to make sure that the native stack gets cleaned up properly + // before turning it back ON we ignore requests while the bluetooth is turning OFF. + // Bug: b/28318203 + if (mState == BluetoothAdapter.STATE_BLE_TURNING_OFF || + mState == BluetoothAdapter.STATE_TURNING_OFF || + mState == BluetoothAdapter.STATE_ON || + mState == BluetoothAdapter.STATE_BLE_ON || + mState == BluetoothAdapter.STATE_TURNING_ON || + mState == BluetoothAdapter.STATE_BLE_TURNING_ON) { + return false; } synchronized(mReceiver) {